Permalink
Browse files

Added proper :boolean type support to has_details plugin, works like …

…the one in activerecord
  • Loading branch information...
1 parent ace6bf9 commit b2b60ce921a47364948b619bddc82a82b805c72a @tarmo tarmo committed with erkki Oct 28, 2008
Showing with 16 additions and 2 deletions.
  1. +16 −2 lib/has_details.rb
View
@@ -41,8 +41,14 @@ def has_details(options = {})
configuration.each do |f,t|
- exception_code = t.is_a?(Array) ? "raise \"Assigned value must be one of #{t.inspect}\" unless #{t.inspect}.include?(val)" : \
- "raise \"Assigned value must be a #{t.inspect}\" unless val.nil? || val.is_a?(#{t.inspect})"
+ exception_code = if t.is_a?(Array)
+ "raise \"Assigned value must be one of #{t.inspect}\" unless #{t.inspect}.include?(val)"
+ elsif t == :boolean
+ # 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?"
+ else
+ "raise \"Assigned value must be a #{t.inspect}\" unless val.nil? || val.is_a?(#{t.inspect})"
+ end
class_eval <<-EOV
def #{f}
@@ -61,6 +67,14 @@ def #{f}=(val)
end
end
EOV
+
+ if t == :boolean
+ class_eval <<-EOS
+ def #{f}?
+ self.#{f} ? true : false
+ end
+ EOS
+ end
end
end

0 comments on commit b2b60ce

Please sign in to comment.