Skip to content

Do not cast value if it's already been casted. #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/enumerize/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,8 @@ def serialize(value)
end

def cast(value)
return value if @subtype.is_a?(Type)

if value.is_a?(::Enumerize::Value)
value
else
@attr.find_value(@subtype.cast(value))
end
value = @subtype.cast(value) if [email protected]_a?(Type) && !value.is_a?(Enumerize::Value)
@attr.find_value(value)
end

def as_json(options = nil)
Expand Down
16 changes: 16 additions & 0 deletions test/activerecord_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,22 @@ class AdminUser < User
expect(admin.account_type).must_equal 'pro'
end

it 'has correct value in _was attribute' do
user = User.create(status: 'active')
user.status = 'blocked'
expect(user.status_was).must_equal 'active'
end

it 'has correct value in _was attribute in child class' do
class AdminUser < User
enumerize :status, :in => { active: 1, blocked: 2, inactive: 3 }, scope: true
end

admin = AdminUser.create(status: 'active')
admin.status = 'blocked'
expect(admin.status_was).must_equal 'active'
end

if Rails::VERSION::MAJOR >= 6
it 'supports AR#insert_all' do
User.delete_all
Expand Down
Loading