Skip to content

Commit

Permalink
Define Type#cast instead of deserialize to fix serialization issue.
Browse files Browse the repository at this point in the history
Type#deserialize by default calls Type#cast so we can keep one.

Closes #404
  • Loading branch information
aomran authored and nashby committed Mar 19, 2023
1 parent 977dcfb commit 1c0b115
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/enumerize/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ def serialize(value)

alias type_cast_for_database serialize

def deserialize(value)
def cast(value)
@attr.find_value(value)
end

alias type_cast_from_database deserialize
alias type_cast_from_database cast

def as_json(options = nil)
{attr: @attr.name}.as_json(options)
Expand Down
9 changes: 9 additions & 0 deletions test/activerecord_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ class ActiveRecordTest < MiniTest::Spec
expect(user.interests).must_equal %w[music dancing]
end

it 'has enumerized values in active record attributes after reload' do
User.delete_all
user = User.new
user.status = :blocked
user.save!
user.reload
expect(user.attributes["status"]).must_equal 'blocked'
end

it 'validates inclusion when using write_attribute with string attribute' do
user = User.new
user.send(:write_attribute, 'role', 'wrong')
Expand Down

0 comments on commit 1c0b115

Please sign in to comment.