Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / collection_cache_key.rb
Created August 4, 2017 05:18
Monkey patch for ActiveRecord::CollectionCacheKey
# 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 March 27, 2017 21:01
Programming Assignment

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 November 11, 2016 02:20
TypeScript -vs- CoffeeScript "Bake Off"

This is a contrived syntax comparison of CoffeeScript & TypeScript.

I know which style I prefer to write.

@hopsoft
hopsoft / example_job.rb
Last active September 9, 2022 06:00
Render views outside of the standard request context (i.e. ActiveJob) with Devise/Warden
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 June 11, 2020 14:27
Ruby 2.3 safe navigation `&.` vs Active Support's try
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 August 29, 2015 14:25
Application JavaScript Manifest
// app/assets/javascripts/application.js
//= require jquery
//= require jquery-ujs
//= require main
@hopsoft
hopsoft / example.js
Created July 18, 2015 12:13
Example Module
// app/assets/javascripts/modules/example.js
define([modules.lodash], function (_) {
return "This is an example.";
});
@hopsoft
hopsoft / main.js
Last active August 29, 2015 14:25
Modular JavaScript in Rails
// app/assets/javascripts/main.js
require([modules.lodash,], function (_) {
console.log(_);
});
@hopsoft
hopsoft / application.html.erb
Last active August 29, 2015 14:25
JavaScript Modules Layout
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
@hopsoft
hopsoft / application_helper.rb
Last active August 29, 2015 14:25
JavaScript Modules Tag Helper
# app/helpers/application_helper.rb
module ApplicationHelper
def javascript_modules_tag
paths = Dir[
Rails.root.join("app/assets/javascripts/modules/**/*"),
Rails.root.join("lib/assets/javascripts/modules/**/*"),
Rails.root.join("vendor/assets/javascripts/modules/**/*")
]