Skip to content

Instantly share code, notes, and snippets.

View niklas's full-sized avatar

Niklas H niklas

View GitHub Profile
@niklas
niklas / rails-fields-with-errors-get-class-by-nokogiri.rb
Created January 9, 2011 21:03
Add .error CSS class to all invalid form fields. Rails wraps them in div.fieldWithErrors, which still sucks in 3.0
# put this in config/initializer/fields_with_errors.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<(input|label|textarea|select)/
error_class = 'error'
doc = Nokogiri::XML(html_tag)
doc.children.each do |field|
unless field['type'] == 'hidden'
unless field['class'] =~ /\berror\b/
@niklas
niklas / fetch_facts.sh
Created October 23, 2012 18:57
Fetch facter facts from a remote host and convert to be used in cucumber-puppet
#!/bin/bash
host=$1
if [ -z "$host" ]; then
echo "please provide hostname for node to fetch facts from"
exit
fi
file="features/nodes/$host.yaml"
@niklas
niklas / branch_database.sh
Last active July 3, 2017 06:31
An own database for every branch - escape the migration hell
#!/bin/bash
# An own database for every branch
#
# 1) put this script somewhere, make it executable (chmod +x)
# 2) modify your database.yml:
# development:
# adapter: postgresql
# database: "<%= `path/to/stored_script development` %>"
# You can specify another environment as first arg.
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke setup:check_app (first_time)
** Execute setup:check_app
cap aborted!
Don't know how to build task 'deploy:web:enable'
/home/niklas/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task_manager.rb:62:in `[]'
/home/niklas/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task.rb:353:in `[]'
/home/niklas/.rvm/gems/ruby-2.2.3@heute_kaufen/gems/capistrano-3.4.0/lib/capistrano/dsl/task_enhancements.rb:12:in `after'
/home/niklas/work/xunde/b2c/lib/capistrano/tasks/backup.rake:22:in `<top (required)>'
@niklas
niklas / ramir.rb
Created October 18, 2013 18:46
script to generate symlinks to a ramdisk (good for logs) TODO: * repair symlink targets after reboot * run automatically after chdir
#!/usr/bin/env ruby
require 'pathname'
# global setup
ramdisk = Pathname.new( Dir.home(ENV['USER']) ).join('tmp')
if File.read('/etc/mtab').lines.grep(%r~ #{ramdisk} ~).empty?
system "mount", ramdisk.to_s
end
#!/usr/bin/env ruby-1.9.3-p392@markup_snapshot
# Our pixel hero wants to know when we change the HTML layout. This script runs
# selected cucumber features in given branch/sha1 and makes snapshots in HTML
# and HAML, original and stripped.
#
# Prerequisites:
# you
# * have rails project
# * use cucumber with capybara
#!/bin/bash
set -e
sudo aptitude install vim git-core xsane byobu libsane-extras virtualbox-guest-utils ssh
SCANSDIR="$HOME/Scanned"
mkdir -p $SCANSDIR
sudo sed -i "s&exit 0&mount.vboxsf Scanned $SCANSDIR vboxsf\nexit 0&" /etc/rc.local
echo "/etc/rc.local"
tail -n 5 /etc/rc.local
@niklas
niklas / ember-data-create-error-handling.coffee
Created October 30, 2012 17:50
HowTo create a DS.Model from ember-data, handling server-side validation errors
# If we save a record using ember-data's RESTadapter, and it fails, Rails
# returns the validation errors of the model as JSON hash:
#
# {"errors":{"name":["may not be blank"]}}
#
# This patches the RESTadapter to add these errors to the invalid record. It
# can be removed when the following Pull Request was merged into ember-data:
# https://github.com/emberjs/data/pull/376
DS.RESTAdapter.reopen
@niklas
niklas / pivotal2kanbanery.rb
Created August 8, 2012 11:49
Migrate from pivotal tracker to kanbanery
#!/usr/bin/env ruby
# Converts PivotalTracker's (PT) export CSV in a CSV kanbanery (KB) accepts to import.
#
# Features:
# * maps PT's states to KB's columns, see $mapper
# * story types are converted
#
# Bugs:
# * comments are not imported
@niklas
niklas / pause_steps.rb
Created May 4, 2012 10:35
Pause a cucumber scenario and ask for pry
When /^I pause|try pry|I pry|I debug$/ do
STDERR.puts "Pausing..."
if system(%Q~which zenity~)
if system(%Q~zenity --question --text="Paused. Want to pry?"~)
binding.pry
end
else
# no zenity installed, pry without asking
binding.pry
end