lib/iron/enum/attr_support.rb in iron-enum-1.0.3 vs lib/iron/enum/attr_support.rb in iron-enum-1.0.4
- old
+ new
@@ -39,11 +39,12 @@
#
# ActiveRecord models get a few extras. To start, each enum attribute will add a smart scope:
#
# User.with_user_type(UserType::MEMBER) => scope returning a relation selecting User instances where user_type's value == UserType::MEMBER
#
- # In addition, enum attributes will show up in #inspect output as e.g. UserType::GUEST instead of 0.
+ # In addition, enum attributes will show up in #inspect output as e.g. UserType::GUEST instead of 0. Enum attributes will also generate an
+ # automatic inclusion validation to ensure that the attribute never ends up being an invalid value.
module AttrSupport
# Call with enum_attr :field => Enum
def enum_attr(field_to_enum_map)
# Save off the attr map
@@ -107,9 +108,10 @@
}
# Override default setter to allow setting an enum attribute via key
class_eval <<-eos, __FILE__, __LINE__ + 1
def #{attr_field}=(val)
+ val = nil if val.is_a?(String) && val.empty?
write_attribute(:#{attr_field}, #{enum_klass}.value(val))
end
eos
else
\ No newline at end of file