Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / nof.sh
Created June 9, 2017 11:35
No focus specs
#!/bin/sh
set -eu
failIfPresentInSpecs() {
REGEX=$1
if find spec -type f -name "*_spec.rb" -exec grep -E "$REGEX" {} +; then
printf "\nFound focused specs\n"
exit 1
fi
@odlp
odlp / ci_update_chrome
Last active May 23, 2018 14:31
Update Chrome on CI
#!/bin/sh
# Exit if any subcommand fails
set -e
# Install latest stable version of Chrome
curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome.deb
sudo sed -i 's|HERE/chrome\"|HERE/chrome\" --disable-setuid-sandbox|g' /opt/google/chrome/google-chrome
rm google-chrome.deb
@odlp
odlp / .nvmrc
Last active September 25, 2017 14:48
Modern node on CircleCI
8.5
@odlp
odlp / jasmine_seed_reporter.js
Last active March 10, 2022 20:57
Jasmine / Karma seed reporter for random test order
var SeedReporter = function(baseReporterDecorator) {
baseReporterDecorator(this);
this.onBrowserComplete = function(browser, result) {
if (result.order && result.order.random && result.order.seed) {
this.write("%s: Randomized with seed %s\n", browser, result.order.seed);
}
};
};
@odlp
odlp / detect-modernizr-use.rb
Created January 30, 2017 17:02
Detect modernizr - Is it being used in the project?
# Determine the feature list from the custom build URL in the source file, e.g.:
# Build: http://modernizr.com/download/#-fontface-backgroundsize-borderimage-borderradius-boxshadow-flexbox-flexboxlegacy-hsla-multiplebgs-opacity-rgba-textshadow-cssanimations-csscolumns-generatedcontent-cssgradients-cssreflections-csstransforms-csstransforms3d-csstransitions-applicationcache-canvas-canvastext-draganddrop-hashchange-history-audio-video-indexeddb-input-inputtypes-localstorage-postmessage-sessionstorage-websockets-websqldatabase-webworkers-geolocation-inlinesvg-smil-svg-svgclippaths-touch-webgl-cssclasses-teststyles-testprop-testallprops-hasevent-prefixes-domprefixes-load
has_features = [
"fontface",
"backgroundsize",
"borderimage",
"borderradius",
"boxshadow",
"flexbox",