Skip to content

Instantly share code, notes, and snippets.

@odlp
odlp / migration.rb
Created June 26, 2017 08:29
Case-insensitive unique constraint in Postgres
class UniqueUserEmail < ActiveRecord::Migration[5.1]
def change
enable_extension :citext
change_column :users, :email, :citext
add_index :users, :email, unique: true
end
end
@odlp
odlp / negated_matcher_spec.rb
Created July 4, 2017 09:43
Example of a negated matcher
require "rails_helper"
RSpec::Matchers.define_negated_matcher :not_be_present, :be_present
RSpec.describe "all matcher" do
it "cooperates with negated matchers" do
expect([nil, "", " "]).to all(not_be_present)
# Whereas this doesn't work:
# expect([nil, "", " "]).to_not all(be_present)
@odlp
odlp / capybara_mobile.rb
Created July 25, 2017 10:57
Capybara temporarily resize window
def with_mobile_window_size(&block)
original_size = page.current_window.size
page.current_window.resize_to(320, 480)
block.call
ensure
page.current_window.resize_to(*original_size)
end
@odlp
odlp / shared_example.rb
Last active June 12, 2018 10:27
Enforce a duck-typing interface with shared examples
require "rspec/autorun"
## A puddle of ducks
class AuthenticDuck
def squeeze
"quack"
end
end
@odlp
odlp / update.sh
Last active May 3, 2020 10:06
Rbenv update Rubygems
#!/usr/bin/env sh
# Multiple vulnerabilities have been disclosed in RubyGems:
# https://www.ruby-lang.org/en/news/2018/02/17/multiple-vulnerabilities-in-rubygems/
#
# And again in March 2019:
# https://blog.rubygems.org/2019/03/05/security-advisories-2019-03.html
#
# If you're an Rbenv user, here's any easy one-liner to upgrade to a
# safe version of Rubygems (2.7.8 / 3.0.3 or later) for each installed Ruby version:
@odlp
odlp / .gitattributes
Created March 5, 2018 14:35
Boss, it's 100% Go!
**/* linguist-language=Go
@odlp
odlp / percentage_error.sh
Last active May 19, 2021 14:57
Percentage error output in tests
#!/bin/bash
# Save stdout & stderr to individual files:
bundle exec rspec > >(tee stdout.log) 2> >(tee stderr.log >&2)
# Calculate the percentage:
wc -l stderr.log stdout.log \
| ruby -e \
'err, _, total = ARGF.map(&:to_f); puts "#{(err/total*100).round}% error output"'
@odlp
odlp / check_test_filenames.sh
Last active June 4, 2018 09:34
Check test / spec files are named correctly
#!/bin/sh
# Checks test files are named "_spec.rb" to match the RSpec globs property
# defined in .circleci/config.yml. This will fail the build if there are test
# files which don't match the required pattern.
# Ignores first-level helper files (e.g. spec_helper.rb) and the spec/support,
# spec/factories, spec/shared_examples folders
set -eu
@odlp
odlp / bundler-rspec-inline.rb
Created August 9, 2018 09:43
Inline Bundler and autorun RSpec
require "bundler/inline"
gemfile do
gem "rspec"
end
require "rspec/autorun"
RSpec.describe "inline Bundler and autorun RSpec" do
it "is convenient for self-contained examples & bug repros" do
@odlp
odlp / bullet_example.rb
Last active January 2, 2019 10:58
Conditional Bullet in RSpec tests
# Make Bullet failing tests which opt-in to 'strict' mode
#
# Based on:
# https://github.com/flyerhzm/bullet#run-in-tests
# spec/rails_helper.rb or similar
RSpec.configure do |config|
config.before(:example, :ar_strict) do
Bullet.enable = true
Bullet.bullet_logger = true