Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
class ConvertAllCurrentDatetimesToUtc < ActiveRecord::Migration TIME_DIFFERENCE_FROM_UTC = 10.hours # Based on the timezone your data is already stored in (AEST in my case) MODELS_WITH_DATETIME_FIELDS = { "Offer" => %w(valid_from valid_to), "Payment" => %w(paid_at), "Promotion" => %w(valid_from valid_to), "Purchase" => %w(purchased_at), "Refund" => %w(refunded_at) } MODELS_WITH_DATETIME_FIELDS.keys.each do |class_name| eval "class #{class_name} < ActiveRecord::Base; end" end def self.up update_datetime_fields(-TIME_DIFFERENCE_FROM_UTC) end def self.down update_datetime_fields(TIME_DIFFERENCE_FROM_UTC) end private def self.update_datetime_fields(time_difference) MODELS_WITH_DATETIME_FIELDS.each_pair do |class_name, fields| class_name.constantize.find(:all).each do |model| announce "Updating #{class_name} #{model.id}" fields.each do |f| model[f] = model[f] + time_difference if model[f] end model.save_without_validation end end end end
This paste will be private.
From the Design Piracy series on my blog: