Skip to content

Instantly share code, notes, and snippets.

Avatar

Nate Hopkins hopsoft

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

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
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
hopsoft / README.md
Last active Apr 19, 2019
Query the recommended column order for PostgreSQL tables
View README.md
@hopsoft
hopsoft / README.md
Last active Apr 19, 2019
Eliminate unauthorized ActionCable connection attempts
View README.md

Problem

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 127.0.0.1 at 2018-11-16 08:32:24 -0700
An unauthorized connection attempt was rejected
@hopsoft
hopsoft / readme2ghpage.rb
Created Jun 21, 2012
Convert your README.md on master to index.md on gh-pages
View readme2ghpage.rb
#!/usr/bin/env ruby
# checkout the readme from the master branch
`git checkout gh-pages; git checkout master README.md`
path = `pwd`.gsub(/\n/, "")
readme_path = File.join(path, "README.md")
index_path = File.join(path, "index.md")
# 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
hopsoft / Gemfile
Last active Aug 17, 2018
Add profiling to your rake tasks.
View Gemfile
group :development do
gem "ruby-prof"
end
@hopsoft
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[Arel.star].count)
where "(#{subquery.to_sql}) > 0" # TODO: move to Arel & remove string interpolation
end
end
@hopsoft
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: <%= Faker::Internet.email %>
phone: <%= Faker::PhoneNumber.cell_phone %>
<% end %>
@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}"
You can’t perform that action at this time.