Skip to content

Instantly share code, notes, and snippets.

Avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / db.rake
Last active Sep 17, 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 / prefetch.js
Last active Sep 17, 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.setRequestHeader('Accept', 'text/html')
xhr.onreadystatechange = () => {
@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 / example.html.erb
Created Aug 13, 2020
StimulusReflex + SlimSelect that supports DOM mutations and reentrancy triggered by reflexes
View example.html.erb
<%= f.select :country_codes, country_select_data, {}, {multiple: true, data: {
controller: "select", action: "stimulus-reflex:after@document->select#delayedSetup", reflex: "change->Adapter#assign_attributes"}} %>
@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 / 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 / examples_reflex.rb
Last active May 17, 2020
Surgical DOM Updates with StimulusReflex
View examples_reflex.rb
# app/reflexes/examples_reflex.rb
class ExamplesReflex < ApplicationReflex
include CableReady::Broadcaster
before_reflex :surgical_update, only: [:noop]
def surgical_update
cable_ready["example"].inner_html(
selector: "#example-container",
html: ExampleController.render(
@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 / 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

You can’t perform that action at this time.