Skip to content

Instantly share code, notes, and snippets.

View searls's full-sized avatar
💚

Justin Searls searls

💚
View GitHub Profile
#!/usr/bin/env bash
# Useful when you want to git bisect in a rails app and know
# you need to `bin/rake db:migrate VERSION="SOMETHING"` before
# you check out the next commit so the database is in the right
# state, but you don't know what SOMETHING is.
# Usage:
#
# $ migration_version_at_ref <REF>
@searls
searls / 1_painful.rb
Last active January 18, 2023 17:43
Here's an example of a refactor I did while test-driving a class using Ruby and Mocktail
# The core problem here is mixing levels of abstraction
# This unit has two jobs. It delegates one and retains (most of) the
# other. That means it's doomed to be partially hands-off
# (simply trusting whatever result ProposesAgent sends back)
# and otherwise hopelessly in the weeds (requiring
# a test to care not only whether the save was valid but how/why)
module Proposals
class ProposesAgent
Result = Struct.new(:success?, :proposal, :error_messages, keyword_init: true)
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails'
# Use Puma as the app server
gem 'puma'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
@searls
searls / index.html
Last active August 19, 2023 18:14
Mobile styling overrides for NetlifyCMS to make it more responsive-ish.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Content Manager</title>
</head>
<body>
@searls
searls / .solargraph.yml
Last active August 2, 2023 09:14 — forked from DRBragg/.solargraph.yml
My config with steps to use solargraph for Rails projects in VS Code (WIP)
---
include:
- ".solargraph_definitions.rb"
- "app/**/*.rb"
- "config/**/*.rb"
- "lib/**/*.rb"
exclude:
- test/**/*
- vendor/**/*
- ".bundle/**/*"
@searls
searls / organization.rb
Created November 4, 2022 12:19
This is easily my least favorite Rails model validation scenario. An association exists and at least one associated item must conform to a specific condition for the owning side of the relationship to be considered valid. In this case, to avoid orphaning organizations
class Organization < ApplicationRecord
has_many :users
end
@searls
searls / form.html.erb
Last active October 21, 2022 15:32
Example Tailwind form builder for Rails
<%= form_with model: @user, url: account_path do |f| %>
<%= f.text_field :name %>
<%= f.email_field :email, disabled: true %>
<%= f.submit "Save" %>
<% end %>
<%= form_with url: login_email_path, method: :delete do |f| %>
<%= f.submit "Log out", variant: :reset %>
<% end %>
require "date"
require "mocktail"
require "minitest/autorun"
class FetchesOrdersAndItems
Result = Struct.new(:orders, :items, keyword_init: true)
def fetch(start_date, end_date)
end
end
@searls
searls / executes_command.rb
Created April 3, 2022 12:57
Silly little class for when you want the combined stdout/stderr output of a command and its success status
require "open3"
class ExecutesCommand
Result = Struct.new(:success, :output, keyword_init: true)
def call(command)
stdin, stdout_and_stderr, wait_thr = Open3.popen2e(command)
result = Result.new(
success: wait_thr.value == 0,
output: stdout_and_stderr.read
)
@searls
searls / whoops.rb
Created January 11, 2022 15:24
I mistakenly thought that re-assigning all entries on an association would cascade a save! on the owner of the relationship to the depended models if they were dirty. Turns out, nope!
class CountsInventory
def count_inventory(store)
Inventory.find_or_initialize_by(store: store).tap do |inventory|
inventory.assign_attributes(date: Time.zone.now.to_date)
inventory.items = count_items(inventory) # Updates which items are associated, does not cascade save to them
inventory.save!
end
end
def count_items(inventory)