diff --git a/lib/active_enum/extensions.rb b/lib/active_enum/extensions.rb index 290ed7b..600d16e 100644 --- a/lib/active_enum/extensions.rb +++ b/lib/active_enum/extensions.rb @@ -105,11 +105,12 @@ def #{attribute}(arg=nil) # Examples: # user.sex = 1 # user.sex = :male + # user.sex = 'Male' # def define_active_enum_write_method(attribute) class_eval <<-DEF def #{attribute}=(arg) - if arg.is_a?(Symbol) + if arg.is_a?(Symbol) || arg.is_a?(String) super self.class.active_enum_for(:#{attribute})[arg] else super arg diff --git a/spec/active_enum/extensions_spec.rb b/spec/active_enum/extensions_spec.rb index 49f0420..ac17c76 100644 --- a/spec/active_enum/extensions_spec.rb +++ b/spec/active_enum/extensions_spec.rb @@ -182,6 +182,11 @@ class Accepted < ActiveEnum::Base person.sex.should == nil end + it 'should store id value when valid enum name' do + person.sex = 'Female' + person.sex.should == 2 + end + end context "with value as enum name" do @@ -198,6 +203,11 @@ class Accepted < ActiveEnum::Base person.sex.should == 'Male' end + it 'should store value when valid enum name' do + person.sex = 'Female' + person.sex.should == 'Female' + end + it 'should return true for boolean match' do person.sex?(:male).should be_true end