Created
February 12, 2011 19:21
-
-
Save rwilcox/824021 to your computer and use it in GitHub Desktop.
Metaprogramming I did today
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Question: can you tell what's going on here? | |
classes = subclasses_of(ActiveRecord::Base) | |
classes.each do |klass| | |
klass.class_eval do | |
before_save do |record| | |
record.class.columns.each do |c| | |
if ( (c.type == :datetime) && (record.send("#{c.name}_changed?")) && record.send("#{c.name}").present? ) | |
unmunged_datetime = record.send(c.name) + 5.hours | |
record.send("#{c.name}=", unmunged_datetime) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exactly. :)
That's an interesting thought RE performance (we don't have enough data to really have a hit). And umm I didn't think about clobbering existing before_save filters (in our case it wasn't an issue, but very good point)