Skip to content

Instantly share code, notes, and snippets.

View jeremysmithco's full-sized avatar

Jeremy Smith jeremysmithco

View GitHub Profile
@jeremysmithco
jeremysmithco / highlightable_controller.js
Created February 18, 2025 20:06
Stimulus.js RangeError Issue
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static values = { highlighted: Boolean };
static classes = ["highlighted"];
toggle() {
this.highlightedValue = !this.highlightedValue;
}
@jeremysmithco
jeremysmithco / stimulus_data.rb
Last active November 28, 2024 07:30
stimulus_data helper
# Example Usage
# data: stimulus_data(
# autosubmit: { controller: true },
# toggle: { controller: true, classes: { checked: "highlighted", unchecked: "unhighlighted" } },
# sortable: { target: "sortBy", actions: { click: "sort" },
# ).merge(other: "stuff")
class StimulusData
def initialize(**controllers)
@controllers = controllers
@jeremysmithco
jeremysmithco / payments_controller.rb
Created November 6, 2024 21:49
Stripe Checkout PaymentsController
class PaymentsController < ApplicationController
CHECKOUT_SESSION_ID_KEY = :checkout_session_id
before_action :authenticate_user!
def new
stripe_customer = find_or_create_stripe_customer
checkout_session = create_checkout_session(stripe_customer)
redirect_to checkout_session.url, allow_other_host: true
@jeremysmithco
jeremysmithco / _form.html.erb
Created October 24, 2024 13:44
Stimulus autosuggest with @github/combobox-nav
<%= tag.div data: { controller: "autosuggest", action: "autosuggest:click:outside->autosuggest#hide" }, class: "relative" do %>
<%= f.text_field :subject, autocomplete: "off", data: { autosuggest_target: "input", action: "input->autosuggest#search focus->autosuggest#search keydown.up->autosuggest#arrowSearch keydown.down->autosuggest#arrowSearch keydown.esc->autosuggest#hide" } %>
<%= tag.ul role: "listbox", hidden: true, data: { autosuggest_target: "list", action: "combobox-commit->autosuggest#commit" }, class: "z-40 absolute w-full max-h-60 overflow-auto mt-1 rounded border border-gray-300 shadow-lg p-1 bg-white" do %>
<% @subjects.each do |subject| %>
<%= tag.li subject.name, role: "option", id: dom_id(subject, :listbox), data: { autosuggest_target: "item" }, class: "w-full text-left cursor-pointer p-2 hover:bg-gray-100 aria-selected:bg-gray-100 aria-selected:font-semibold" %>
<% end %>
<% end %>
<% end %>
@jeremysmithco
jeremysmithco / _form.html.erb
Created October 16, 2024 17:28
Stimulus @github/session-resume
<%= form_with(model: topic_form, url: spaced_topics_path) do |form| %>
<div class="mb-4">
<%= form.label :title %>
<%= form.text_field :title, data: { session_resume_target: "resumeable" } %>
<%= form.error :title %>
</div>
<div class="mb-2">
<%= form.label :body, class: "sr-only" %>
<%= form.markdown_text_area :body, data: { session_resume_target: "resumeable" } %>
@jeremysmithco
jeremysmithco / riff1.rb
Created September 9, 2024 18:33
Riffing on Topic Subscriptions
class User
has_many :subscriptions
has_many :followings
end
class Channel
has_many :topics
has_many :subscriptions
end
@jeremysmithco
jeremysmithco / active_storage.rb
Last active August 8, 2024 21:01
Rough sketch of Active Storage calculated variants
Rails.application.config.to_prepare do
ActiveStorage::Attachment.class_eval do
private
def transform_variants_later
if record.respond_to?(:calculated_variants)
record.calculated_variants[name.to_sym]&.values.each do |transformations|
blob.preprocessed(transformations)
end
end
@jeremysmithco
jeremysmithco / mails
Created May 20, 2021 13:46
Rails bin script to listen for emails delivered to filesystem and open with Mail (on a Mac)
#!/usr/bin/env ruby
require 'listen'
APP_ROOT = File.expand_path('..', __dir__)
listener = Listen.to("#{APP_ROOT}/tmp/mails") do |_modified, added, _removed|
added.each do |added_file|
if added_file.end_with?(".eml")
system("open -a Mail #{added_file}")
else

Keybase proof

I hereby claim:

  • I am bentoncreation on github.
  • I am bentoncreation (https://keybase.io/bentoncreation) on keybase.
  • I have a public key whose fingerprint is 69F3 3662 82F5 6128 4016 61FD 93C3 7BCB 2202 47A5

To claim this, I am signing this object:

@jeremysmithco
jeremysmithco / word_counter.rb
Last active August 29, 2015 14:06
Count words in your Rails app, just pass it the path to your views directory (or a subdirectory) to get the word count from your ERB files.
#!/usr/bin/env ruby
require "action_view"
require "fileutils"
class WordCounter
include ActionView::Helpers::SanitizeHelper
def initialize(initial_directory)
@initial_directory = initial_directory
puts count_directory(@initial_directory)