Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / README.md
Last active February 29, 2024 17:50
Rails System Test Supervisor

Rails System Test Supervisor

System tests with Rails, Capybara, and Selenium (with the Chrome driver) may go rogue and spike the CPU to 100% on the spawned browser depending on how heavily you've monkey patched these libraries and/or have complex JavaScript that mutates the DOM (morphs etc.).

This supervisor class monitors the driver process and its children and will kill any rogue process that persistently spikes to >=99% CPU utilization for an extended period of time. If and when this occurs, you lose control of the browser and your tests will hang until they eventually time out or error.

@hopsoft
hopsoft / README.md
Last active January 14, 2024 11:58
Smart Heroku Review Apps managed by GitHub Actions

Smart Heroku Review Apps managed by GitHub Actions

This gist aims to provide a simple solution for managing Heroku Review Apps with GitHub Actions due to the security incident that continues to disrupt Heroku's GitHub integration. Watch the demo to learn more.

Demo Video

.github
├── workflows
│   ├── heroku_review_app_create.yml
@hopsoft
hopsoft / README.md
Last active January 13, 2024 13:46
Cron Cheat Sheet

Cron Cheat Sheet

sudo crontab -e
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
@hopsoft
hopsoft / prefetch.js
Last active December 27, 2023 02:45
Turbolinks Prefetching
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 / gist:3147080
Created July 19, 2012 21:48
Divide an Array equally
class Array
# Divides the Array into equally sized chunks.
#
# @example
# list = ["a", "b", "c", "d", "e", "f"]
# list.divide(3) # => [["a", "b"], ["c", "d"], ["e", "f"]]
#
# @param [Integer] count A recommended number of chunks (sub lists) to create.
# The calculated chunk size is rounded up,
@hopsoft
hopsoft / resourceful.rb
Created November 30, 2011 21:23
Resourceful Rails actions for free with a Sinatra like DSL for action callbacks
module Resourceful
def self.actions
[:index, :show, :new, :edit, :create, :update, :destroy]
end
def self.callbacks
[:before, :after, :respond_to]
end
@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_job.rb
Last active September 9, 2022 06:00
Render views outside of the standard request context (i.e. ActiveJob) with Devise/Warden
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 August 8, 2022 20:13
Heroku Review Apps managed by GitHub Actions

Heroku Review Apps managed by GitHub Actions

This gist aims to provide a simple solution for managing Heroku Review Apps with GitHub Actions due to the security incident that continues to disrupt Heroku's GitHub integration.

.github
├── workflows
│   ├── heroku_review_app_create.yml
│   └── heroku_review_app_destroy.yml
@hopsoft
hopsoft / application_reflex.rb
Created December 1, 2021 22:51
ApplicationReflex setting ActiveSupport::CurrentAttributes via lifecycle callbacks
# frozen_string_literal: true
class ApplicationReflex < StimulusReflex::Reflex
# Put application-wide Reflex behavior and callbacks in this file.
#
# Example:
#
# # If your ActionCable connection is: `identified_by :current_user`
# delegate :current_user, to: :connection
#