Skip to content

Instantly share code, notes, and snippets.

@mschuerig
mschuerig / gist:7891143
Created December 10, 2013 14:10
Mongoid: Raise an exception and log errors if the identity map contains changed but unsaved objects after a request was processed.
unless Rails.env.production?
after_filter do
objects = Mongoid::IdentityMap.values.flat_map(&:values)
changed_objects = objects.select { |o| o.changes.except('updated_at', 'locked_at', 'locked_until').present? }
unsaved_objects = changed_objects.select { |o| o.errors.empty? }
unless unsaved_objects.empty?
unsaved_objects.each do |obj|
Rails.logger.error "Unsaved object: #{obj.inspect}\n Changes: #{obj.changes.inspect}"
end
raise "Some changed objects have not been saved. See log for details."
@mschuerig
mschuerig / gist:7577756
Created November 21, 2013 08:13
within_any. Helper method for capybara. For selectors matching multiple elements, expect the block not to raise an error for one of them.
def within_any(*args)
candidates = page.all(*args)
if candidates.empty?
query = Capybara::Query.new(*args)
raise Capybara::ElementNotFound.new("Unable to find #{query.description}")
end
error = nil
candidates.any? do
begin
@mschuerig
mschuerig / git-amnesia.rb
Created May 11, 2013 20:25
In a directory containing multiple git repositories, run git log on each of them. git amnesia --author=me --since=2012-03-01
#! /usr/bin/ruby -w
require 'date'
require 'find'
# TODO
# - make work with -p option
def main
logopts = "--color=always --since=#{Date.today - 3} #{ARGV.join(' ')}"
@mschuerig
mschuerig / yearly.rb
Created April 20, 2013 08:40
Generate yearly and year-monthly playlists based on when albums were added to the collection. The heuristic used to find the adding date is to take the date of the oldest file in the album's directory. No CLI yet, use at your own risk.
#! /usr/bin/ruby1.9.3
require 'taglib'
require 'debugger'
# TODO
# - CLI
# - -m, --monthly
# - -u, --update
@mschuerig
mschuerig / copytags.rb
Last active December 15, 2015 20:58
Copy tags from OGG or MP3 files to FLAC files. Matching files are found by patterns containing their track number. Optionally rename files.
#! /usr/bin/ruby1.9.3 -w
require 'optparse'
require 'taglib'
FIELDS = %w( ALBUM ARTIST CDDB DATE GENRE DISCNUMBER TITLE TRACKNUMBER )
$dry_run = false
$tag = true
$rename = false
@mschuerig
mschuerig / webshot.js
Created August 26, 2012 15:58
Screenshot script for webpages
#! /usr/bin/env phantomjs
// Michael Schuerig, <[email protected]>, 2012.
// Portions are Copyright 2011 Valeriu Paloş ([email protected]). (see below)
// This script requires PhantomJS
// http://phantomjs.org/download.html
var jQueryUrl = "//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js";
@mschuerig
mschuerig / git-pre-commit-check.rb
Created June 23, 2012 12:51
Pre-commit hook for git
#! /usr/bin/ruby -w
# Michael Schuerig, <[email protected]>, 2012.
# This file is in the public domain.
# Any trouble it may cause is yours, not mine.
#
# This is a pre-commit hook for git with a few checks I find useful
# useful in Rails projects. The checks are:
#
# * Bad tags (such as XXX, WTF, ...) as specified in
@mschuerig
mschuerig / cucumber-completion.sh
Created June 16, 2012 15:21
Bash completion for cucumber
__cucumbercomp() {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $( compgen -W "$1" -- "$cur" ) )
}
__cucumbertags() {
sed -r -n 's/^\s*(((@[[:alnum:]]+)\s?)+).*$/\1/p' features/**/*.feature \
| sed -r 's/\s+/\n/'
}
@mschuerig
mschuerig / cheat-completion.sh
Created June 14, 2012 08:36
Bash completion for cheat
_cheat()
{
local sheets
list="$HOME/.cheat/.sheets"
if [ ! -f "$list" ]; then
cheat sheets | sed -n 's/^ \(.*\)$/\1/p' > "$list"
fi
sheets=$(cat "$list")
@mschuerig
mschuerig / after_use_switch_gemfiles_copy
Created June 10, 2012 15:28
Switch Gemfile.lock when rvm use'ing another ruby
#!/usr/bin/env bash
# Place this file at ~/.rvm/hooks/after_use_switch_gemfiles
# and make it executable.
#
# This version copies Ruby-specific Gemfile.lock.x to Gemfile.lock.
if [ -e "Gemfile" ]; then
. "${rvm_path}/scripts/functions/hooks/jruby"
unset current_platform gemfile_platform