davydovanton / shallow_attributes

Simple and lightweight Virtus analog.
MIT License
101 stars 18 forks source link

Fix bug for Custom Types when the type changes to Hash after call the `attributes` method #21

Closed danielvidal closed 6 years ago

danielvidal commented 6 years ago

Greetings!

I figured out a bug that changes the custom type after the method attributes is called from the model.

Remember these classes...

class Address
  include ShallowAttributes
  attribute :street, String
end

class Person
  include ShallowAttributes
  attribute :address, Address
end

Current behavior:

person = Person.new(address: { street: 'Street 1/2' })
person.address.class
# => Address
person.address
# => #<Address street="Street 1/2">
person.attributes
# => { address: { street: "Street 1/2" } }
person.address.class
# => Hash
person.address
# => { street: "Street 1/2" }

After fix behavior:

person = Person.new(address: { street: 'Street 1/2' })
person.address.class
# => Address
person.address
# => #<Address street="Street 1/2">
person.attributes
# => { address: { street: "Street 1/2" } }
person.address.class
# => Address
person.address
# => #<Address street="Street 1/2">

I think this is the expected behavior πŸ˜„ Any comments about the changes and/or code implementation are welcome

coveralls commented 6 years ago

Coverage Status

Coverage increased (+52.8%) to 99.879% when pulling d8e644069f9eeb79ba8466a0a851e01e62c3f3a8 on danielvidal:bugfix-custom-type-changed into 754d238009b8068d0268f54cb5acc53377e4d03e on davydovanton:master.

jits commented 5 years ago

Hi – any chance you could release a new version with this fix in it? Thanks!

davydovanton commented 5 years ago

hey, new version is here πŸŽ‰ https://rubygems.org/gems/shallow_attributes/versions/0.9.4

jits commented 5 years ago

@davydovanton – thank you!