Skip to content

Instantly share code, notes, and snippets.

View bensheldon's full-sized avatar
🏊‍♂️
Swimming through code.

Ben Sheldon [he/him] bensheldon

🏊‍♂️
Swimming through code.
View GitHub Profile
import {Controller} from "@hotwired/stimulus"
// Warns if form fields have unsaved changes before leaving the page.
// Changes are stored in Session Storage to restore un-warnable events
// like using the back button
//
// To use:
// <form data-controller="unsaved-changes">
// <input type="text" name="name" data-unsaved-changes-target="field">
export default class extends Controller {
# frozen_string_literal: true
# Reduce the amount of quote churn in the YAML files that results from
# i18n-tasks attempting to normalize the quotes itself.
# Always attempt to preserve the pre-existing quote style.
require 'psych'
class I18nTasksWrapper
def self.i18n_files
locales_path = File.expand_path('./locales', File.dirname(__FILE__))
@bensheldon
bensheldon / phone_number.rb
Created May 23, 2025 23:10
A Phone Number tiny type
# frozen_string_literal: true
class PhoneNumber
EXACT_DIGITS = 10
MAX_NON_DIGITS = 5
delegate :present?, :blank?, :encoding, to: :@value
delegate :hash, to: :strict
def initialize(value)
---
description: Rules for creating Controllers
globs:
alwaysApply: false
---
# Modifying Rails controllers
## Routes
When adding new controllers or actions, don't forget to update the [routes.rb](mdc:config/routes.rb)
# frozen_string_literal: true
require "bundler/inline"
# Inline Gemfile for dependencies
gemfile(true) do
source "https://rubygems.org"
gem "benchmark-ips"
end
# spec/support/capybara_check_choose.rb
# frozen_string_literal: true
module System
module CapybaraCheckChoose
# Speed up `allow_label_click`, which is the default, so that it
# does the label click immediately rather than waiting for the
# default strategy to fail
def _check_with_label(selector, checked, locator,
allow_label_click: session_options.automatic_label_click, **options)
class Household < ApplicationRecord
has_many :members
has_many :expenses
end
class Member < ApplicationRecord
belongs_to :household
end
class Expense < ApplicationController

My theory is that folks are hallucinating complex structure for these docs. If you read the Cursor forums, people are asking the Cursor LLM how to format them 🙃 Those shared above are the results of writing in the UI and what gets puts into version control.

My approach is that when I am annoyed by something I have to manually fix up, I go update the file.

Some of it is just impossible, like using the new Strong Params expects syntax given the training cutoffs. I’m not going to completely document the interface (cursor/rules can’t link to docs and can only link to a single file) . So I hint it, and it simply hallucinates the interface mostly correctly most of the time. Principle of Least Surprise in practice!

My .cursorrules were pretty inconsistent project to project, and I don’t believe that putting very generic things like “Use exceptions for exceptional cases, not for control flow” or “Use Active Record effectively” improves things.

# frozen_string_literal: true
# == Schema Information
#
# Table name: ai_prompt_responses
#
# id :bigint not null, primary key
# ai_model :text
# completed_at :datetime
# error_message :text

In my application every time I send a newsletter to a User, I create a NewsletterDelivery record. I frequently want to be able to query, for each user, what is the most recent newsletter delivery record. This is called a "last n per group" query and a LATERAL JOIN is the best way to do it imo. But the query I've been using (and I've told people to use, and seen blogged about) is not very good, because the conditions don't get pushed down into the subselect which means that the query ends-up lateral-joining all the records before it applies the conditions for the association.

Instead of doing subselect_table.* the better query does association.id AS assocation_id, subselect_table.id, subselect_table.title, .... and enumerates over all of the columns. This allows the association query, which Active Record tacks on at the end as WHERE association_id = $1 or WHERE association_id IN ($1, $2, $3, ...) to be pushed down c