Skip to content

Instantly share code, notes, and snippets.

View leastbad's full-sized avatar
🤠
Keeping it real for the rest of us.

leastbad leastbad

🤠
Keeping it real for the rest of us.
View GitHub Profile
module IndexableByHashPatch
refine Array do
def to_proc
->(h) { length == 1 ? h[first] : h.values_at(*self) }
end
end
end
using IndexableByHashPatch
@leastbad
leastbad / sr.txt
Created October 6, 2022 06:10
Not all heroes wear capes
oooo
oooo:~:+oo
o~.~:~~.:+oo
oooooo oooooooooooo o+:...~.~:o
o+:++~+o ooo++:::::::+++oo++...:.~~o
o+:~+:.~o oo+::~~~:~~~:::~~:::~~:+:~~~o
o+~..+...+ooo+:~~::::~~:~~+++++++++++:~~~+o
o+~..... :::~~~:::+::+:~~::+:++::+:::+~~~+o
o:~.~~.~~~~:::+::~..::~:+~:::::+++++:.::o
o:~~~:+:+++++:....~...::......~:+++:.:~o
@leastbad
leastbad / channel.rb
Last active July 24, 2022 11:10
Action Cable Channels setup
module ApplicationCable
class Channel < ActionCable::Channel::Base
include CableReady::Broadcaster
delegate :render, to: :ApplicationController
end
end
@leastbad
leastbad / delegated_type.rb
Created May 11, 2022 05:05
Alias for_ delegated types in Rails 6.1
# intended to be a Rails 6.1 initializer based on @rolandstuder's https://github.com/rails/rails/pull/41506
# if you want to use it with Rails 7, you will need to add a third options: parameter to the method
module ActiveRecord
module DelegatedType
private
def define_delegated_type_methods(role, types:)
role_type = "#{role}_type"
role_id = "#{role}_id"
define_method "#{role}_class" do
@leastbad
leastbad / timer_controller.js
Created July 17, 2021 20:13
timer_controller.js WIP
import { Controller } from 'stimulus'
export default class extends Controller {
static values = {
idleTimeoutMs: 30000,
currentIdleTimeMs: Number,
checkIdleStateRateMs: 250,
isUserCurrentlyOnPage: true,
isUserCurrentlyIdle: Boolean,
currentPageName: 'default',
@leastbad
leastbad / README.md
Last active July 4, 2021 00:33
mrujs w/ cable-car plugin

"dependencies": { "cable_ready": "5.0.0-pre1", "mrujs": "^0.3.0-beta.25" }

@leastbad
leastbad / select_controller.js
Created June 10, 2021 23:53
SlimSelect Stimulus wrapper
import ApplicationController from '../application_controller'
import SlimSelect from 'slim-select'
export default class extends ApplicationController {
static values = {
limit: Number,
placeholder: String,
searchText: String,
searchingText: String,
reflex: String,
@leastbad
leastbad / Gemfile
Last active July 2, 2021 00:43
Webpacker 5 config
gem "webpacker", "~> 5.4.0"
@leastbad
leastbad / customer_reflex.rb
Last active January 4, 2022 08:21
TomSelect + StimulusReflex 3.5 (w/payload)
class CustomersReflex < ApplicationReflex
def lookup(search)
# use search string to do some kind of DB lookup here
self.payload = {} # I have NO IDEA what tom-select expects!
morph :nothing
end
end
@leastbad
leastbad / struct.rb
Created May 7, 2021 10:15 — forked from KonnorRogers/struct.rb
Using hashes for structs
# using a predefined hash
hash = { field1: "foo", field2: "bar" }
HashStruct = Struct.new(*hash.keys, keyword_init: true)
hash_struct = HashStruct.new(hash)
hash_struct.field1 # => "foo"
hash_struct.field2 # => "bar"
hash_struct.field3 # => ERROR!