Skip to content

Instantly share code, notes, and snippets.

@rlogwood
rlogwood / example_controller_strategy_pattern.rb
Last active May 14, 2021 21:08
Rails controller strategy pattern via controller.instance_eval
class MyController < ApplicationController
# ...
def search_all
return unless (@search_phrase = search_phrase_or_redirect)
doc_types = %w[Episode ForumPost Series]
notification_payload = { action: "search_all", search_phrase: @search_phrase,
doc_types: doc_types, pagy_limit: RESULT_PAGE_SIZE }
ActiveSupport::Notifications.instrument('search_render', notification_payload) do
@rlogwood
rlogwood / contact_request.rb
Created May 6, 2021 23:40
Rails ActiveModel::Model Example, an alternative to ActiveRecord when you don't need the DB
# NOTE: this a partial copy of some working code from a working Rails app
# *However* it hasn't been tested on it's own, shown here as an example of
# using ActiveModel::Model as an alternative to ActiveRecord when you don't
# need the DB
class ContactRequest
include ActiveModel::Model
def self.define_sanitized_attr(attr)
define_method(attr) do
@rlogwood
rlogwood / react_state_fn_upd_explore.js
Last active February 21, 2019 18:05
Explore React setState idiom of using a function instead of an object to update state
/*
Simple javascript to explore the React stateState function paradigm
node react_state_fn_upd_explore.js
Explore the second form of setState() that accepts a function rather than an
object. This is needed because React can batch state updates and
asynchronous state updates mean you need a function that receives its
current value before using values from it to set the next state
values.