Skip to content

Instantly share code, notes, and snippets.

@sephraim
sephraim / format_url.rb
Created March 14, 2023 16:33
[Format a URL with parameters]
ACTION_LINK_BASE_URL = URI.parse('https://example.com/service.html')
# Action Link URL
#
# @example Example usage
# action_link_url('abc-123-xyz') #=> https://example.com/service.html?id=abc-123-xyz
#
# @param account_id [String]
# @return [String]
def action_link_url(account_id)
@sephraim
sephraim / all_routes.rb
Created February 28, 2023 14:51
[Get all valid routes in Rails]
routes = Rails.application.routes.routes.map { |r| r.path.spec.to_s }
# *OR* for even more attributes
Rails.application.routes.routes.map { |r| { alias: r.name, path: r.path.spec.to_s, controller: r.defaults[:controller], action: r.defaults[:action] } }
@sephraim
sephraim / test_mail.rb
Created February 28, 2023 14:49
[Test ActionMailer in RSpec] Test the Rails mailer class
# frozen_string_literal: true
require "rails_helper"
RSpec.describe Notifications, type: :mailer do
describe '.google_places_report_ready' do
let(:mail) { Notifications.google_places_report_ready }
let(:download_path) { '/google_places/download_report' }
it 'renders the subject' do
@sephraim
sephraim / docker-compose.yml
Last active May 2, 2023 15:22
[ActiveJob / DelayedJob example class] with tests and Docker Compose setup
volumes:
postgresql:
name: automotiontv-postgresql
external: true
x-restart_policy: &default_restart_policy
deploy:
restart_policy:
condition: on-failure
delay: 5s
@sephraim
sephraim / test_download.rb
Last active March 8, 2023 20:49
[Test that a file gets downloaded in Rails] Use RSpec to test that a file gets downloaded in a Rails app
it 'downloads the file' do
expect(controller).to receive(:send_file).with(GooglePlacesApiClient::DEFAULT_CSV_FILEPATH, type: 'text/csv').and_call_original
get :download_report
end
# OR
it 'does not try to download the file' do
expect(controller).not_to receive(:send_file)
get :download_report
@sephraim
sephraim / stub_const.rb
Last active February 28, 2023 05:56
[Stub class constants] Stub class constants in RSpec tests
# Stubbing a single class constant is a little finicky and can remove all constants of a class if not done right
MyClass::MY_CONSTANT_1 #=> 'foo'
MyClass::MY_CONSTANT_2 #=> 'bar'
# METHOD 1 (PREFERRED)
before(:example) do
stub_const('MyClass', MyClass)
@sephraim
sephraim / README.md
Last active March 8, 2023 21:20
[DelayedJob / ActiveJob useful commands]

Delayed Job

Check number of queued jobs

Delayed::Job.count

Delete the first job

Delayed::Job.first.destroy
@sephraim
sephraim / info.txt
Last active March 14, 2024 14:31
[Setup & testing Rails logger] including for development, testing, and production.
# Diff between Logger and ActiveSupport::Logger
https://stackoverflow.com/q/64433146/990637
# Problem with Puma / .silence method when using Logger instead of ActiveSupport::Logger
https://github.com/rails/sprockets-rails/issues/376#issuecomment-287560399
@sephraim
sephraim / test_file_contents.rb
Last active February 28, 2023 07:47
[Test a method that writes to a file] Use RSpec to call a method that writes to a file and then test the contents of the file, all while ensuring that each created file immediately gets deleted after testing (i.e. temp file)
RSpec.describe SomeClass do
describe '.some_method_that_writes_to_a_file' do
around(:example) do |example|
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
example.run # Write file to a tmp directory that then gets deleted after each example
end
end
end
@sephraim
sephraim / ostruct_select.rb
Created February 14, 2023 23:49
[Select attributes from OpenStruct]
require 'ostruct'
os = OpenStruct(foo: 1, bar: 2, baz: 3)
os.to_h.slice(:foo, :baz) #=> { foo: 1, baz: 3 }
os.to_h.values_at(:foo, :baz) #=> [1, 3]