Skip to content

Instantly share code, notes, and snippets.

View simplyb's full-sized avatar

Pete Broderick simplyb

View GitHub Profile
> user.name = "John"
> user.save
> user.name_previously_changed?
=> true
> user.name = "John"
> user.save
> user.previous_changes
=> {"name"=>["Frank", "John"]}
> user = User.first
> user.name
=> "Frank"
> user.name = "John"
> user.name_changed?
=> true
> user.name_change
=> ["Frank", "John"]
module PreviouslyDirty
extend ActiveSupport::Concern
included do
attribute_method_suffix '_previously_changed?'
end
private
# Handle <tt>*_previously_changed?</tt> for +method_missing+.
development:
adapter: mysql2
encoding: utf8
database: my_db
pool: 20
username:
password:
@simplyb
simplyb / multiscrub.rb
Created September 7, 2012 14:43
multi-threaded scrubbin
namespace :data do
MyApp::RakeData.tables.each do |table|
desc "scrubs #{table} table data"
task "scrub_#{table}" do
string_attributes = table.classify.constantize.columns.map {|c| c.name if [:string, :text].include?(c.type) }.compact
setters = []
update_string = "UPDATE #{table} SET "
string_attributes.each do |attribute|
@simplyb
simplyb / rake_data.rb
Created September 7, 2012 14:39
data for a rake task
module MyApp
module RakeData
extend Self
HILARIOUS_STRING = 'Gangster lorem ipsum from http://lorizzle.nl/'
def tables
["list", "of", "table", "names"]
end
@simplyb
simplyb / scrubbin.rb
Created September 7, 2012 14:28
rake task to scrub tables
namespace :data do
desc "scrubs all strings in every table"
task :scrub => :environment do
unless Rails.env.development?
raise "This should only ever be run in development!"
end
tables = ActiveRecord::Base.connection.tables - ["schema_migrations"]
Rabl.render(Event.first, 'events/event')