Skip to content

Instantly share code, notes, and snippets.

@garrow
garrow / dynamic_matcher_steps.rb
Last active December 24, 2015 13:29
Turnip expectation placeholders.
# An attempt to allow turnip steps something along the lines of;
#
# Then I should not see awesomeness!
# Then I shouldn't see awesomeness!
# Then I should see awesomeness!
step 'I :expectation see awesomeness!' do |expectation|
binding.pry ##
expectation
end
@garrow
garrow / supercase.rb
Created September 27, 2013 05:18
Unfortunately you can't use the symbol to_proc short syntax `&:symbol` in case statements. Reply to http://batsov.com/articles/2013/09/24/lambdas-slash-procs-in-case-expressions/
case v
when ->(v) { v == 1 } then puts 'one'
when &:even? then puts 'even' # Syntax error :(
when :odd?.to_proc then puts 'odd'
else puts v
end
@garrow
garrow / dual_field_input.rb
Created September 23, 2013 13:53
Experimenting with library classes that define lots of hook methods. This class is subclassed and the hooks are overridden to add additional behaviour while still allowing reuse.
# Options
# :primary_field The entity/model attribute for the first field, displayed by default.
# :secondary_field The entity/model attribute for the second field, hidden by default.
# :show_text The Show button label - defaults to 'Show'.
# :hide_text The Hide button label - defaults to 'Hide'.
#
# e.g. In our Spy form;
# f.input :name, as: :dual_field,
# primary_field: :alias,
@garrow
garrow / table_steps.rb
Created September 17, 2013 04:49
Adding a `within_cell` Capybara helper. We use this within our Turnip specs to find a particular cell within a particular column within a table.
def within_cell(row, column)
within_row(row) do
within_column(column) { yield }
end
end
def within_column(column)
# Find the table column offset for the column header, then grab the columns' cells at that offset.
# Adapted from http://stackoverflow.com/questions/14745478
within(:xpath, "//table/tbody/tr/td[count(//table/thead/tr/th[normalize-space()='#{column}']/preceding-sibling::th)+1]") do
@garrow
garrow / output
Created February 26, 2013 03:14
A very simple git pre-commit hook to add/set a VERSION file with each commit. It will automatically insert the version and add that to the pending commit. Note that git hooks are not distributed with the repo, though adding to a fresh repo via the dev environment bootstrap script for our project works fine.
~/work/tmp/version_file <master +>± git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: foo
#
~/work/tmp/version_file <master +>± git diff --staged
diff --git a/foo b/foo
new file mode 100644
@garrow
garrow / spoiler-css-only.css
Created February 23, 2013 13:10
spoiler-alert, css only version. Still doesn't work on firefox. https://github.com/joshbuddy/spoiler-alert
spoiler, .spoiler {
-webkit-transition: all 125ms linear;
-moz-transition: all 125ms linear;
-o-transition: all 125ms linear;
transition: all 125ms linear;
filter: blur(10px);
-webkit-filter:blur(10px);
-moz-filter: blur(10px);
-o-filter: blur(10px);
@garrow
garrow / snapshot_restore.sql
Created October 8, 2012 06:15
Restore tables from a snapshot into public.
DO
$$
DECLARE
hidden_table record;
BEGIN
FOR hidden_table IN
SELECT schemaname, tablename FROM pg_tables
WHERE schemaname = (SELECT MAX(schemaname) FROM pg_tables WHERE schemaname LIKE 'snapshot_%')
LOOP
EXECUTE 'DROP TABLE IF EXISTS public.' || quote_ident(hidden_table.tablename) || ';';
@garrow
garrow / colourise_svn_status.rb
Created May 28, 2012 02:05
Quick and terrible svn output colouriser, might be ugly in non custom terminal colour schemes. Mine is based on monokai.
#!/usr/bin/ruby
colours = { :white => "\033[1;37m" \
, :yellow => "\033[1;33m" \
, :green => "\033[1;32m" \
, :blue => "\033[1;34m" \
, :cyan => "\033[1;36m" \
, :red => "\033[1;31m" \
, :magenta => "\033[1;35m" \
, :black => "\033[1;30m" \
ActiveAdmin::Register Package do
# A custom action we want treated like the :show or :edit actions.
member_action :overview, :method => :get do
@package = Package.find params[:id]
end
# Add the actions for the :overview action.
template_actions_for :overview, :show
end
@garrow
garrow / prompt_char.sh
Created April 17, 2012 07:09
Nice changing prompt character, based on the current repository type.
function prompt_char {
if [[ -d './.svn/' ]]; then echo '§' && return; fi
# it's faster to test for a `./.svn` directory than
# to exec `svn` & check return codes
git branch >/dev/null 2>&1 && echo '±' && return
hg root >/dev/null 2>&1 && echo '☿' && return
# redirect output of commands to /dev/null &
# echo custom char based on successful execution
echo '○'