Skip to content

Instantly share code, notes, and snippets.

View brandondrew's full-sized avatar

Brandon Zylstra brandondrew

  • UC Berkeley
  • the tubes
View GitHub Profile
# In your Gemfile
gem "localhost"

# Then, depending on your desired server
gem "falcon"
gem "puma"
@kaspth
kaspth / concern.rb
Created June 4, 2024 20:37
Writing Concerns without the `extend`/`included`/`class_methods` boilerplate.
# Maybe there's a way to support ActiveSupport::Concern's dependencies too?
# Although this doesn't use the module hierarchy, so `prepend` can't work.
class Concern < Module
def initialize(&block) = @block = block
def included(klass) = klass.class_eval(&@block)
end
module Kernel
def Concern(...) = Concern.new(...)
end
@brandondrew
brandondrew / button_progress_controller.js
Created February 22, 2024 03:20 — forked from dbreunig/button_progress_controller.js
Poor man's form button progress indicator, in Stimulus, for those long processing requests.
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
addEventListener("turbo:submit-end", ({ target }) => {
clearInterval(this.interval);
this.element.innerHTML = this.originalLabel;
})
this.originalLabel = this.element.innerHTML;
}
@ivan3bx
ivan3bx / generate_previews.rb
Created February 9, 2024 17:33
Ruby script to generate screenshots for a series of URLs crawled from a sitemap.xml
require "selenium-webdriver"
require "nokogiri"
require "net/http"
BASE_URL = "http://localhost:1313"
#
# This expects several things to be true:
#
# 1. Local hugo instance running at localhosts:1313
@dbreunig
dbreunig / button_progress_controller.js
Last active February 22, 2024 03:20
Poor man's form button progress indicator, in Stimulus, for those long processing requests.
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
addEventListener("turbo:submit-end", ({ target }) => {
clearInterval(this.interval);
this.element.innerHTML = this.originalLabel;
})
this.originalLabel = this.element.innerHTML;
}
@bradgessler
bradgessler / component_helper.rb
Last active February 1, 2024 20:15
Component helper
module ComponentHelper
def component(name, *args, **kwargs, &block)
render componentize(name).new(*args, **kwargs), &block
end
private
def componentize(name)
"#{name.to_s.classify}Component".constantize
end
end
@joeldrapper
joeldrapper / example.rb
Last active January 25, 2024 06:57
Table component
render TableComponent.new(@posts) do |t|
t.column("Title", &:title)
t.column("Author") { |post| post.author.name }
end
@joeldrapper
joeldrapper / fingerprinting.rb
Created January 10, 2024 14:30
Rails request fingerprinting concern
# frozen_string_literal: true
module Fingerprinting
def full_fingerprint
generate_fingerprint(
ip_fingerprint,
browser_fingerprint
)
end
@bradgessler
bradgessler / phlex_element.rb
Last active January 3, 2024 22:21
The Phlex::Element class makes creating simple Phlex components easier
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "phlex"
end
require "phlex/testing/view_helper"
include Phlex::Testing::ViewHelper
module ArrayColumns
extend ActiveSupport::Concern
class_methods do
def array_columns_sanitize_list(values = [])
return [] if values.nil?
values.select(&:present?).map(&:to_s).uniq.sort
end
def array_columns(*column_names)