Skip to content

Commit 804a79d

Browse files
committed
Do not cast value if it's already been casted.
1 parent 5a2b168 commit 804a79d

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/enumerize/activerecord.rb

+2-7
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,8 @@ def serialize(value)
126126
end
127127

128128
def cast(value)
129-
return value if @subtype.is_a?(Type)
130-
131-
if value.is_a?(::Enumerize::Value)
132-
value
133-
else
134-
@attr.find_value(@subtype.cast(value))
135-
end
129+
value = @subtype.cast(value) if !@subtype.is_a?(Type) && !value.is_a?(Enumerize::Value)
130+
@attr.find_value(value)
136131
end
137132

138133
def as_json(options = nil)

test/activerecord_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,22 @@ class AdminUser < User
731731
expect(admin.account_type).must_equal 'pro'
732732
end
733733

734+
it 'has correct value in _was attribute' do
735+
user = User.create(status: 'active')
736+
user.status = 'blocked'
737+
expect(user.status_was).must_equal 'active'
738+
end
739+
740+
it 'has correct value in _was attribute in child class' do
741+
class AdminUser < User
742+
enumerize :status, :in => { active: 1, blocked: 2, inactive: 3 }, scope: true
743+
end
744+
745+
admin = AdminUser.create(status: 'active')
746+
admin.status = 'blocked'
747+
expect(admin.status_was).must_equal 'active'
748+
end
749+
734750
if Rails::VERSION::MAJOR >= 6
735751
it 'supports AR#insert_all' do
736752
User.delete_all

0 commit comments

Comments
 (0)