Skip to content

Instantly share code, notes, and snippets.

View jaydorsey's full-sized avatar
💊
Ruby on Rails

Jay Dorsey jaydorsey

💊
Ruby on Rails
View GitHub Profile
@jaydorsey
jaydorsey / flakey_spec.rb
Created August 21, 2024 13:33
Rescueing a flakey spec to debug
RSpec.describe MyClass do
let(:my_value) { 1 }
let(:other_value) { 2 }
it 'does a thing' do
expect(my_value).to eq(other_value) # failure condition, something flakey though
rescue RSpec::Expectations::ExpectationNotMetError
$ERROR_INFO.message << <<~MSG
@jaydorsey
jaydorsey / ruby_hash_error.md
Created August 16, 2024 13:30
unexpected '\n', expecting =>

Ruby is expecting a Hash, but you forgot something (maybe a key name?)

Example:

# wrong
foo: { # Start of the new hash
  :bar # You're missing your key OR value
}
@jaydorsey
jaydorsey / flakey_spec.rb
Created July 30, 2024 19:28
How to write flakey specs with rails, rspec, factory_bot, and faker
# frozen_string_literal: true
# 1. Put this in your spec folder as spec/flakey_spec.rb
# 2. Run it with `rspec spec/flakey_spec.rb`
#
# Assumptions:
#
# 1. You're using rails, rspec, factory_bot, faker
# 2. You have a standard-ish User model (has create_at, updated_at, etc. integer ID primary key, etc)
# 3. User has a last_name field/column/attribute
@jaydorsey
jaydorsey / factory_example.rb
Last active July 30, 2024 14:48
FactoryBot factories that share a relationship
# frozen_string_literal: true
# I often find myself wanting to codify relationships/associations in a factory by default, so the
# factory doesn't require myself or other engineers to retain the knowledge about the complex relationships
# and define those relationships in every part of the test
#
# In this contrived example, our models are tenant-aware via a company_id FK on each model:
# - Widget belongs_to a company
# - User belongs to a company
# - A widget has a creator; the creator of the widget and the widget itself must belong to the same company
@jaydorsey
jaydorsey / app_models_user.rb
Created March 25, 2024 12:44
Add a Trait to skip callbacks in a FactoryBot factory
# app/models/user.rb
#
class User
after_create :foo
private
def foo
# ..something we don't do very often
end
@jaydorsey
jaydorsey / gist:b56529c2403bf4162cd9e51a025be045
Created February 26, 2024 20:34
Remove annotate gem annotations
For some reason, I couldn't get the annotate gem to remove the file annotations
I used SublimeText w/ the following regex:
Find: `(?s)# == Schema Information\n(.*?)\n((# :nodoc:\n)?class)`
Replace: `class` (just adds back what I grabbed above)
@jaydorsey
jaydorsey / nokogiri.md
Last active February 26, 2024 14:37
WARNING: Nokogiri was built against libxml version

My setup

For reference only:

  1. rtx-cli
  2. Ruby 3.2.0
  3. Most dependencies installed via Homebrew
  4. macOS 14.2.1
  5. M3 Max chip
@jaydorsey
jaydorsey / example.rb
Created February 16, 2024 14:39
Storing images as base64 representations
require 'base64'
file = File.open('path/to/file.ext', 'rb') # Read the image as binary
base64 = Base64.strict_encode64(file.read) # This gives you a string you can assign to a constant/variable
tmpfile = Tempfile.new(Base64.strict_decode64(base64)) # Decode and create a temporary file
Digest::MD5.file(tmpfile).base64digest # This is one way to create a checksum on the file
File.size(tmpfile) # You can also get a file size
@jaydorsey
jaydorsey / my_stuff.rb
Created January 26, 2024 14:06
RSpec Rails.logger block test
# frozen_string_literal: true
class MyStuff
OOPS = 'Oops'
def self.call
Rails.logger.warn { OOPS }
end
end
@jaydorsey
jaydorsey / example_spec.rb
Created November 15, 2022 13:47
Using stub_const to hijack a constant
# Lines marked with pry are just for testing in a rails console
require 'rspec/mocks' # pry
include RSpec::Mocks::ExampleMethods # pry
# This bypasses the warning that the method isn't called inside of a test
RSpec::Mocks.with_temporary_scope do # pry
stub_const(
'MyClass',
instance_double(