brainspec / enumerize

Enumerated attributes with I18n and ActiveRecord/Mongoid support
MIT License
1.73k stars 190 forks source link

Properly deserialize boolean values #434

Closed mihyaeru21 closed 1 year ago

mihyaeru21 commented 1 year ago

A boolean field was nil after retrieving it from the DB where it should have a value. Fixed so that the value can be retrieved in this PR. If you comment out this change, you can see that it is nil in the added test case.

In the test case 'supports boolean column as enumerized field' in file test/activerecord_test.rb, the @attr in Enumerize::ActiveRecordSupport::Type#cast looked like this.

In the test case, when Enumerize::ActiveRecordSupport::Type#cast was called, the @attr looked like this.

...
@value_hash={"true"=>"subscribed", "false"=>"unsubscribed", "subscribed"=>"subscribed", "unsubscribed"=>"unsubscribed"}
...

The actual value is '0' or '1' at the time it is retrieved from the DB. The result was nil because the key of @attr did not contain them.

nashby commented 1 year ago

@mihyaeru21 thanks for PR! I fixed this issue and potential other issue with deserialization here https://github.com/brainspec/enumerize/pull/435