Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / broadcastable.rb
Created August 27, 2021 13:21
Broadcastable model concern for CableReady/CableCar
# frozen_string_literal: true
module Broadcastable
extend ActiveSupport::Concern
def prepend_operation(options = {})
operation = {html: ApplicationController.render(partial: to_partial_path, locals: locals)}
operation.merge options
end
@hopsoft
hopsoft / example_spec.rb
Created June 30, 2021 13:21
Asserts with equality check and detailed expected/got failure messages
module ThreadAwareEquality
refine BasicObject do
alias_method :original_equals, :==
def ==(other)
Thread.current[:actual] = self
Thread.current[:expected] = other
original_equals other
end
end
@hopsoft
hopsoft / keybase.md
Created June 8, 2021 18:00
Keybase proof

Keybase proof

I hereby claim:

  • I am hopsoft on github.
  • I am hopsoft (https://keybase.io/hopsoft) on keybase.
  • I have a public key ASCUrC24sEgI229zKeQ2yFjvNYN6NEHRunnG0LMQD050Iwo

To claim this, I am signing this object:

@hopsoft
hopsoft / example.html.erb
Created August 13, 2020 14:35
StimulusReflex + SlimSelect that supports DOM mutations and reentrancy triggered by reflexes
<%= 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 / README.md
Last active May 27, 2021 18:15
Docker proxyconnect connection refused error

How to "fix" Docker's proxyconnect connection refused error

This worked for me on an Apple M1.

Problem

docker pull docker/getting-started
Error response from daemon: Get https://registry-1.docker.io/v2/: proxyconnect tcp: dial tcp 192.168.64.1:3128: connect: connection refused
@hopsoft
hopsoft / action_controller_parameters.rb
Created January 19, 2018 19:04
Manually & Recursively Filter Rails Params
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 / README.md
Last active April 17, 2021 02:27
Query the recommended column order for PostgreSQL tables
@hopsoft
hopsoft / _example.html.erb
Created February 25, 2021 12:29
Stimulus Controller for slim-select that supports AJAX content
<%= select_tag column_name,
options_for_select(...), {
multiple: true,
placeholder: "Select multiple...",
data: {
controller: "select",
action: "stimulus-reflex:after@document->select#delayedSetup",
options_url: typeahead_path(...)
}} %>
@hopsoft
hopsoft / model_supports_bulk_updates.rb
Last active December 8, 2020 17:08
ActiveRecord Bulk / Batch Update
# 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 / 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