Skip to content

Instantly share code, notes, and snippets.

@odlp
odlp / table_support.rb
Last active August 13, 2020 14:37
Capybara table helper
module TableSupport
def column_names(table_selector = "table")
find(table_selector).all("th").map(&:text)
end
def values_for_column(column_name, table_selector = "table")
table = find(table_selector)
index = table.all("th").map(&:text).find_index(column_name)
expect(index).to be_present, "Unable to find column named: #{column_name}"
@odlp
odlp / 1_pg_enum_migrator.rb
Last active June 14, 2019 15:07
Postgresql Enum Migrator - from integers to PG enum values
# frozen_string_literal: true
class PgEnumMigrator
def initialize(migration:, table:, column:, enum_name:, mapping:, new_default: nil, old_default: nil)
@migration = migration
@table = table
@column = column
@enum_name = enum_name
@mapping = mapping
@new_default = new_default
@odlp
odlp / has_string_enum.rb
Last active June 14, 2019 14:41
Rails shorthand string enum values
# frozen_string_literal: true
# app/models/concerns
module HasStringEnum
extend ActiveSupport::Concern
class_methods do
def string_enum(name, values, **options)
enum(name => values.zip(values).to_h, **options)
end
@odlp
odlp / cache_spec.rb
Created April 10, 2020 18:25
Test caching mechanism
it "doesn't make further requests when the cache is warm" do
stub = stub_request(:get, "www.amazon.com")
Amazon::API.new.item_prices("id")
Amazon::API.new.item_prices("id")
# https://github.com/bblimke/webmock#setting-expectations-in-rspec-on-the-stub
expect(stub).to have_been_requested.once
end
@odlp
odlp / hit.rb
Last active January 21, 2021 17:57
Hit a spec
# spec/support/hit.rb
module HitSpec
def hit(*args, &block)
n = 100
if args.last.is_a?(Hash) && args.last.key?(:n)
n = args.last.delete(:n)
end