Skip to content

Instantly share code, notes, and snippets.

Nate Hopkins hopsoft

Block or report user

Report or block hopsoft

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@hopsoft
hopsoft / prefetch.js
Last active Feb 21, 2020
Turbolinks Prefetching
View prefetch.js
const hoverTime = 400
const fetchers = {}
const doc = document.implementation.createHTMLDocument('prefetch')
function fetchPage (url, success) {
const xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.setRequestHeader('VND.PREFETCH', 'true')
xhr.onreadystatechange = () => {
if (xhr.readyState !== XMLHttpRequest.DONE) return
@hopsoft
hopsoft / db.rake
Last active Jan 6, 2020
Rails rake tasks for dump & restore of PostgreSQL databases
View db.rake
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to db/APP_NAME.dump"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"
end
puts cmd
@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 / README.md
Last active Aug 8, 2019
Create and publish an NPM package cheat sheet
View README.md
  1. Create an account on NPM https://www.npmjs.com

  2. Init your project

    mkdir example
    cd example
    yarn init
    
  3. Create a JavaScript file

@hopsoft
hopsoft / benchmarks.rb
Last active May 28, 2019
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 / install-ruby.sh
Created May 21, 2019
Install ruby with rbenv and jemalloc on ubuntu
View install-ruby.sh
sudo apt-get update
sudo apt-get install libjemalloc-dev
RUBY_CONFIGURE_OPTS='--with-jemalloc' rbenv install 2.6.3
# test (look for jemalloc warnings)
MALLOC_CONF=invalid_flag:foo ruby -v
@hopsoft
hopsoft / model_supports_bulk_updates.rb
Last active Apr 19, 2019
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 / 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
You can’t perform that action at this time.