Skip to content

Instantly share code, notes, and snippets.

Avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
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
end
private
def filterer
@filterer ||= ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters)
end
@hopsoft
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
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: https://github.com/rails/rails/blob/5-0-4/activerecord/lib/active_record/collection_cache_key.rb
# 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
hopsoft / assignment.md
Last active Mar 27, 2017
Programming Assignment
View assignment.md

Overview

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
hopsoft / readme.md
Created Nov 11, 2016
TypeScript -vs- CoffeeScript "Bake Off"
View readme.md

This is a contrived syntax comparison of CoffeeScript & TypeScript.

I know which style I prefer to write.

@hopsoft
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 = ::ApplicationController.renderer.new
renderer_env = renderer.instance_eval { @env }
@hopsoft
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"
Benchmark.bm do |x|
count = 1_000_000
label_size = 20
x.report "check for nil:".rjust(label_size) do
count.times { nil && nil.length }
end
@hopsoft
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
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
hopsoft / main.js
Last active Aug 29, 2015
Modular JavaScript in Rails
View main.js
// app/assets/javascripts/main.js
require([modules.lodash,], function (_) {
console.log(_);
});
You can’t perform that action at this time.