Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
mmrwoods / human_attribute_name_writers.rb
Last active January 1, 2016 05:49
Create writer methods for active model human attribute names. Useful when importing from spreadsheets.
module Importing
module HumanAttributeNameWriters
def self.included(base)
base.new # Force activerecord to initialize attributes
# only allow accessible attributes that are not foreign keys or nested attributes
foreign_keys = Agent.reflect_on_all_associations.map(&:association_foreign_key)
allowed_attributes = base.accessible_attributes.select do |name|
name.present? && !foreign_keys.include?(name) && !name.match(/_attributes$/)
@mmrwoods
mmrwoods / account.rb
Last active January 1, 2016 05:39
Dumping ground for capistrano recipes...
namespace :account do
desc "Change the deploy user password"
task :reset_password do
deploy_user = user
deploy_pass = generate_password
as_sudo_user do
find_servers.each do |server|
if read_file("/etc/passwd", :hosts => server.host).lines.grep(/#{deploy_user}:/).any?
run "echo \"#{deploy_user}:#{deploy_pass}\" | #{sudo} chpasswd", :hosts => server.host
# Rails email validator is required by the secure validatable module in devise security extension
# However, it handles internation domain names and does mx lookups by default, and there's no easy way to change this default.
class EmailValidator
def validate_mx?
options[:validate_mx] == true
end
def allow_idn?
options[:allow_idn] == true
end
end
@mmrwoods
mmrwoods / activerecord_nested_attributes_patch.rb
Created December 23, 2013 16:16
Skip validation of associated objects unless nested attributes have been provided
module ActiveRecord
module NestedAttributes
module ClassMethods
# Skips validation of associated objects unless nested attributes have been provided.
#
# This is a horrid workaround for the fact that nested_attributes_for sets the autosave
# option for each assocation to true, which causes validation to fail for associated
# objects that have been loaded, whether any nested attributes have been provided or not.
# This causes problems with the parent object is updated from forms that don't include
@mmrwoods
mmrwoods / address.rb
Last active January 1, 2016 05:39
Postal code validation example
class Address < ActiveRecord::Base
attr_accessible :line_1, :line_2, :line_3, :town, :region, :postcode
validates :line_1, :postcode, :presence => true
validates :postcode, :postcode => true, :allow_blank => true
after_validation do
self.postcode = PostalCode.new(self.postcode).to_formatted_s
end
@mmrwoods
mmrwoods / postgres_backup_restore.rake
Last active January 1, 2016 05:39
Simple backup and restore tasks for postgres
require 'highline/import'
def highlight(str)
"\e[33m" + str + "\e[0m"
end
def run_cmd(cmd)
say highlight "Executing \"#{cmd}\""
Timeout::timeout(30) do
IO.popen(cmd).each{ |line| print line }
@mmrwoods
mmrwoods / wicklow.vim
Last active December 30, 2015 21:39
My old vim colour scheme, based on Eclipse, tweaked for Ruby development. Dumping it here because I'm going to remove it from my dotfiles repo.
" Vim color file
" Maintainer: Mark Woods
" Licence: Public Domain
" GUI only color scheme based on the eclipse color scheme, optimised for ruby.
set background=light
highlight clear
if exists("syntax_on")
@mmrwoods
mmrwoods / fix_wordpress_permissions.sh
Last active December 30, 2015 06:48
Experimental script to fix fscked wordpress permissions
#!/bin/bash
# Experimental script to fix fscked wordpress permissions, needs testing
usage() {
echo "Usage: $0 [-u user] path-to-wordpress-install"
}
# allow user to be set via command line option
while getopts “u:v” OPTION
@mmrwoods
mmrwoods / deferred_gc.rb
Created October 21, 2013 08:16
Disable GC and running the collector manually every few seconds for cuke runs
# Taken from Jamis Buck's post "The road to faster tests"
# http://37signals.com/svn/posts/2742-the-road-to-faster-tests
#
# Disabling GC and running the collector manually every few seconds
# knocks 25-30% off the time taken to run all of my non-js scenarios
# on ruby 1.9.3p448, without any other memory management tuning.
DEFERRED_GC_THRESHOLD = (ENV['CUCUMBER_DEFERRED_GC_THRESHOLD'] || 3.0).to_f
last_gc_run = Time.now
@mmrwoods
mmrwoods / rekordbox2mp3cd
Last active December 23, 2015 09:28
Burn a rekordbox export to an MP3 CD
#!/bin/bash
# Burns a rekordbox export to an MP3 CD compatible with older CDJs.
#
# Copies mp3, m4a and wav audio files from export volume to a single CD,
# converting to constant bit rate mp3 compatible with older CDJs and other
# non-pioneer CD players as required.
#
# Will explode if files won't fit onto a single CD. Yup, I know this is shit.
#