Skip to content

Instantly share code, notes, and snippets.


Nate Hopkins hopsoft

View GitHub Profile
hopsoft / action_controller_parameters.rb
Created Jan 19, 2018
Manually & Recursively Filter Rails Params
View action_controller_parameters.rb
module ActionControllerParameters
def filtered
filter_hash self.to_unsafe_hash
def filterer
@filterer ||=
hopsoft / model_supports_bulk_updates.rb
Last active Sep 3, 2020
ActiveRecord Bulk / Batch Update
View model_supports_bulk_updates.rb
# frozen_string_literal: true
module ModelSupportsBulkUpdates
extend ActiveSupport::Concern
module ClassMethods
# Performs a bulk update with an efficient single query for all the records in the list.
# Note that the records are not reloaded form the database.
# This means that ActiveRecord will still see these records as dirty after the bulk_update.
def bulk_update(records)
hopsoft / collection_cache_key.rb
Created Aug 4, 2017
Monkey patch for ActiveRecord::CollectionCacheKey
View collection_cache_key.rb
# MONKEY_PATCH: Override ActiveRecord::CollectionCacheKey#collection_cache_key
# This allows us to preserve existing selects when paginating
# thus preventing errors when ordering by custom/computed select fields
# For the original implementation SEE:
# IMPORTANT: Upgrade this file whenever upgrading rails versions
module ActiveRecord::CollectionCacheKey
def collection_cache_key(collection = all, timestamp_column = :updated_at) # :nodoc:
query_signature = Digest::MD5.hexdigest(collection.to_sql)
key = "#{collection.model_name.cache_key}/query-#{query_signature}"
hopsoft /
Last active Mar 27, 2017
Programming Assignment


Create a Rails application to manage cinematic movie information.

  • Title
  • Genre
  • Actor(s)
  • Rating

The requirements are intentionally vague to allow some creative freedom.

hopsoft /
Created Nov 11, 2016
TypeScript -vs- CoffeeScript "Bake Off"

This is a contrived syntax comparison of CoffeeScript & TypeScript.

I know which style I prefer to write.

hopsoft / example_job.rb
Last active Oct 25, 2019
Render views outside of the standard request context (i.e. ActiveJob) with Devise/Warden
View example_job.rb
class ExampleJob < ApplicationJob
queue_as :default
def perform(user)
# do some work
# HACK: get around limitations in devise/warden when rendering
# views outside the context of a formal http request
renderer =
renderer_env = renderer.instance_eval { @env }
hopsoft / benchmarks.rb
Last active Jun 11, 2020
Ruby 2.3 safe navigation `&.` vs Active Support's try
View benchmarks.rb
require "benchmark"
require "active_support/all" do |x|
count = 1_000_000
label_size = 20 "check for nil:".rjust(label_size) do
count.times { nil && nil.length }
hopsoft / application.js
Last active Aug 29, 2015
Application JavaScript Manifest
View application.js
// app/assets/javascripts/application.js
//= require jquery
//= require jquery-ujs
//= require main
hopsoft / example.js
Created Jul 18, 2015
Example Module
View example.js
// app/assets/javascripts/modules/example.js
define([modules.lodash], function (_) {
return "This is an example.";
hopsoft / main.js
Last active Aug 29, 2015
Modular JavaScript in Rails
View main.js
// app/assets/javascripts/main.js
require([modules.lodash,], function (_) {
You can’t perform that action at this time.