Skip to content

Instantly share code, notes, and snippets.

@hopsoft
hopsoft / Dockerfile
Last active Apr 9, 2021
Dockerize your Rails app
View Dockerfile
FROM ruby:3.0-alpine
RUN apk add --no-cache --update \
ack \
bash \
build-base \
curl \
htop \
less \
libsass \
@hopsoft
hopsoft / _example.html.erb
Created Feb 25, 2021
Stimulus Controller for slim-select that supports AJAX content
View _example.html.erb
<%= 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 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 / 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 / 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 / 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 / README.md
Last active Jan 5, 2021
Query the recommended column order for PostgreSQL tables
View README.md
View gist:4944d9c9ef85c5adfa29034b90a86222
brew link libffi --force
export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
gem install ffi
@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 / prefetch.js
Last active Mar 22, 2021
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 = () => {