Skip to content
Browse files

has_details fields now support validation

  • Loading branch information...
1 parent 41f0969 commit 44e285ea8914525bd288032b222b568e9ccdcb16 @tarmo tarmo committed with erkki
Showing with 7 additions and 1 deletion.
  1. +7 −1 lib/has_details.rb
View
8 lib/has_details.rb
@@ -47,7 +47,7 @@ def has_details(options = {})
# everything can be converted to boolean so we don't have an exception here
"val = ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(val) unless val.nil?"
elsif t == Integer
- "val = Integer(val) rescue nil"
+ "val = (val.nil? ? nil : (Integer(val) rescue nil))"
else
"raise \"Assigned value must be a #{t.inspect}\" unless val.nil? || val.is_a?(#{t.inspect})"
end
@@ -57,6 +57,12 @@ def #{f}
self.details ||= {}
self.details[:#{f}]
end
+
+ def #{f}_before_type_cast
+ if self.details
+ self.details[:#{f}]
+ end
+ end
def #{f}=(val)
#{exception_code}

0 comments on commit 44e285e

Please sign in to comment.
Something went wrong with that request. Please try again.