davydovanton / shallow_attributes

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

Fix inheriting from a base class that does not define any attributes. #7

Closed tatey closed 7 years ago

tatey commented 7 years ago

Hello,

Firstly, thank you for creating this project 😄. We are using it for our non-ActiveRecord backed forms.

We have a base class that mixes in ShallowAttributes, but doesn't define attributes of its own. Any engineer can subclass this base class and have confidence that it will work out of the box in our forms and controllers.

Unfortunately we hit a bug where the subclass would raise TypeError: no implicit conversion of nil into Hash because it's trying to read default_values from a nil object which it expected to be a hash.

We have managed to work around it by defining MyExampleBaseClass.default_values to return an empty hash. While this is satisfactory it means the out of box experience with ShallowAttributes is not as smooth as it could be.

I hope you find this patch valuable and I'm open to any feedback you have.

Cheers, Tate

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.02%) to 99.077% when pulling 9900e85a9cf1b738313eb603013344a39e2aff7d on fivegoodfriends:fix-inheritance-when-base-class-has-no-attributes into dd82ec67f65d778ae15ad62d80b9267ec0a52fba on davydovanton:master.

davydovanton commented 7 years ago

Hey, I'm happy that for someone this gem is useful :blush: Changes and motivation look good, that's why I'll merge it. Thanks for contribution 👍

tatey commented 7 years ago

Glad you like it :+1: