Skip to content

Instantly share code, notes, and snippets.

@jameskerr
jameskerr / compose_attrs.rb
Created August 27, 2025 19:38
Compose Attributes
def compose_attrs(a, b)
result = a.dup
b.each do |key, val|
x = result[key]
y = val
pair = [ x, y ]
if pair.any?(&:nil?)
result[key] = x || y
elsif pair.all? Hash
result[key] = compose_attrs(x, y)
@jameskerr
jameskerr / component.rb
Created August 15, 2025 17:31
View Component Generator Script
#!/usr/bin/env ruby
# Check for -d flag
if ARGV[0] == "-d"
name = ARGV[1]
delete_mode = true
else
name = ARGV[0]
delete_mode = false
end
@jameskerr
jameskerr / multi_select_controller.js
Last active July 29, 2025 12:35
Mutli Select Stimulus Controller
import Base from "controllers/base";
export default class extends Base {
static targets = ["checkbox", "toggler", "count", "menu", "hide"];
mount() {
this.on("turbo:morph", document, this.update);
this.on("click", this.togglerTarget, this.toggle);
}
@jameskerr
jameskerr / react-debut.tsx
Created September 9, 2023 03:11
React Debut: A component for animating elements in an out as they mount and unmount.
import {CSSTransition} from "react-transition-group"
import {forwardRef, useImperativeHandle, useRef, useState} from "react"
import {call} from "src/util/call"
export function useDebut(args: {enter?: () => any; exit: () => any}) {
const ref = useRef<any>()
const api = {
props: {
ref,
onExit: args.exit,
@jameskerr
jameskerr / reduxModel.ts
Last active November 23, 2020 19:16
Redux Model
/* This could be a way to reduce (haha) the redux boilerplate. */
import {createStore} from "redux"
function createReduxModel(prefix, config) {
const actionType = (name) => `${prefix}_${name.toUpperCase()}_SET`
class Model {
constructor(store) {
for (const [name] of Object.entries(config)) {
# Put this in the controller
def filters_for_this_action
_process_action_callbacks.select do |filter|
ifs = filter.options[:if].first || 'true'
unlesses = filter.options[:unless].first || 'false'
eval(ifs) && !eval(unlesses)
end.map { |filter| [filter.kind, filter.filter] }
end

Procedures for adding migrations to our Rails application.

1. Add your migration file

There is a rails tool to help you make the file. For example, if I wanted to add an age column to the users table, I would run:

>  rails g migration add_age_to_users age:integer
      invoke  active_record
@jameskerr
jameskerr / special_goals_question.txt
Created August 26, 2016 16:39
Special Goals Question
How to solve this "special" goals question.
Since we are saving the questions through the cycle model via the
`accepts_nested_attributes`, feature in Rails, we don't need to make
more than one ajax call when saving the questions. When we save all
the questions data, we will also use this "special" goals question (GQ)
to add the following fields to the save data.
* cycle.goals_question_type
* cycle.goals_question_multchoice_desc
@jameskerr
jameskerr / configure-cycle-ideas.js
Last active August 24, 2016 19:37
Ideas for Restructuring Configure Cycle View
/**
* FORM FIELD CONFIGURATION
*
* In the configure-cycle.js view, we will determine what type of
* cycle we are dealing with, and choose the correct configuration
* object. The keys of these configuration objects represent the file
* name where the subview lives. The value represents the options
* object that will be passed to the view's initialize() method.
*/
# See if an object is "complete" and give it a score. Example useage
# would look like this.
CompletenessAnalyzer.new(object: Event.find(1), criteria: [
Criterion.new({
name: 'Audience',
message: 'Whom is your event is intended for?',
condition: ->(event) { event.audiences.empty? }
}),
Criterion.new({
name: 'Contact Info',