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 November 22, 2021 19:46
Broadcastable model concern/mixin
# 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 / README.md
Last active March 29, 2024 18:13
Descriptive messages with basic asserts

Descriptive messages with basic asserts

What if I told you that it's possible to get helpful failure messages from basic asserts using idiomatic equality == checks? No DSLs in sight.

This is a proof of concept to demonstrate that it's possible... mostly to satisfy my own curiosity. The concepts here could theoretically be expanded to provide a useful extension to existing testing frameworks or perhaps lay a foundation for an entirely new one.

This experiment created with Ruby 3.0.1. Note to self: There's probably a way to do this with TracePoint instead of monkey patching but I couldn't figure out how to get a reference to the passed variable being compared.

Examples:

@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 / 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 / Dockerfile
Last active May 17, 2023 19:58
Dockerize your Rails app
FROM ruby:3.0-alpine
RUN apk add --no-cache --update \
ack \
bash \
build-base \
curl \
git \
htop \
less \
@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 / 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 / examples_reflex.rb
Last active May 17, 2020 15:05
Surgical DOM Updates with StimulusReflex
# 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(