lib/rooftop/coercions.rb in rooftop-0.0.6 vs lib/rooftop/coercions.rb in rooftop-0.0.7.4
- old
+ new
@@ -13,9 +13,14 @@
if r.respond_to?(field)
r.send("#{field}=",coercion.call(r.send(field)))
end
end
})
+ base.send(:before_save, ->(r) {
+ r.coercions.each do |field,coercion|
+ r.send(:"restore_#{field}!") unless r.new?
+ end
+ })
end
module ClassMethods
# Call coerce_field() in a class to do something with the attribute. Useful for parsing dates etc.
# For example: coerce_field(date: ->(date_string) { DateTime.parse(date_string)}) to get a DateTime object from a string field. The date field will now be a DateTime.
\ No newline at end of file