Skip to content

Instantly share code, notes, and snippets.


Nate Hopkins hopsoft

View GitHub Profile
hopsoft /
Last active Apr 19, 2019
Simple API security with Rails

Simple API security with Rails

API clients set an HTML header.

Authorization: Token token="SECRET_API_KEY"

Your ApplicationController can then restrict access based on the token.

hopsoft / gist:6519388
Last active Apr 19, 2019
Rails presenter base class.
View gist:6519388
# app/presenters/presenter.rb
require "delegate"
class Presenter < SimpleDelegator
attr_reader :model, :controller
def initialize(model, controller)
@controller = controller
super(@model = model)
hopsoft /
Last active Apr 19, 2019
Query the recommended column order for PostgreSQL tables
hopsoft /
Last active Apr 19, 2019
Eliminate unauthorized ActionCable connection attempts


Unauthorized ActionCable connection attempts that produce the following

Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Finished "/cable/" [WebSocket] for at 2018-11-16 08:32:24 -0700
An unauthorized connection attempt was rejected
hopsoft / readme2ghpage.rb
Created Jun 21, 2012
Convert your on master to on gh-pages
View readme2ghpage.rb
#!/usr/bin/env ruby
# checkout the readme from the master branch
`git checkout gh-pages; git checkout master`
path = `pwd`.gsub(/\n/, "")
readme_path = File.join(path, "")
index_path = File.join(path, "")
# write the index readme file
View gist:4944d9c9ef85c5adfa29034b90a86222
brew link libffi --force
export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
gem install ffi
hopsoft / Gemfile
Last active Aug 17, 2018
Add profiling to your rake tasks.
View Gemfile
group :development do
gem "ruby-prof"
hopsoft / assignable_job.rb
Last active May 15, 2018
Arel subquery condition
View assignable_job.rb
class AssignableJob < ApplicationRecord
has_many :assignments, as: :record, dependent: :destroy
scope :assigned, -> do
subquery = Assignment.where(record_type: name).where(Assignment.arel_table[:record_id].eq(arel_table[:id])).select(Assignment.arel_table[].count)
where "(#{subquery.to_sql}) > 0" # TODO: move to Arel & remove string interpolation
hopsoft / users.yml
Last active Feb 9, 2018
User Fixture
View users.yml
<% 100.times do |i| %>
user_<%= i %>:
id: <%= SecureRandom.uuid %>
first_name: <%= Faker::Name.first_name %>
last_name: <%= Faker::Name.last_name %>
email: <%= %>
phone: <%= Faker::PhoneNumber.cell_phone %>
<% end %>
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}"
You can’t perform that action at this time.