Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@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(
@hopsoft
hopsoft / README.md
Last active August 8, 2019 23:02
Create and publish an NPM package cheat sheet
  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 / api_tokens.md
Last active April 19, 2019 14:28
Simple API security with Rails

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 April 19, 2019 14:28
Rails presenter base class.
# 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 April 19, 2019 09:31
Eliminate unauthorized ActionCable connection attempts

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 / readme2ghpage.rb
Created June 21, 2012 17:09
Convert your README.md on master to index.md on gh-pages
#!/usr/bin/env ruby
# checkout the readme from the master branch
`git checkout gh-pages; git checkout master README.md`
path = `pwd`.gsub(/\n/, "")
readme_path = File.join(path, "README.md")
index_path = File.join(path, "index.md")
# write the index readme file
@hopsoft
hopsoft / gist:4944d9c9ef85c5adfa29034b90a86222
Created December 30, 2018 15:23
Install ffi ruby gem on mac osx
brew link libffi --force
export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
gem install ffi
@hopsoft
hopsoft / Gemfile
Last active August 17, 2018 20:40
Add profiling to your rake tasks.
group :development do
gem "ruby-prof"
end
@hopsoft
hopsoft / assignable_job.rb
Last active May 15, 2018 14:08
Arel subquery condition
class AssignableJob < ApplicationRecord
has_many :assignments, as: :record, dependent: :destroy
scope :assigned, -> do
subquery = Assignment.where(record_type: name).where(Assignment.arel_table[:record_id].eq(arel_table[:id])).select(Assignment.arel_table[Arel.star].count)
where "(#{subquery.to_sql}) > 0" # TODO: move to Arel & remove string interpolation
end
end
@hopsoft
hopsoft / users.yml
Last active February 9, 2018 23:19
User Fixture
<% 100.times do |i| %>
user_<%= i %>:
id: <%= SecureRandom.uuid %>
first_name: <%= Faker::Name.first_name %>
last_name: <%= Faker::Name.last_name %>
email: <%= Faker::Internet.email %>
phone: <%= Faker::PhoneNumber.cell_phone %>
<% end %>