Skip to content
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

Make attributes deserialization "relaxed" #22

Closed
FunkyloverOne opened this issue Jul 31, 2019 · 3 comments
Closed

Make attributes deserialization "relaxed" #22

FunkyloverOne opened this issue Jul 31, 2019 · 3 comments

Comments

@FunkyloverOne
Copy link

Hi!

So I was playing around with this gem, and tried the following:

I have defined some JSON-backed model, let's say it was:

class Configuration
  include StoreModel::Model

  attribute :model, :string
end

Then I have created some record in my DB with filled model attribute.
Then I have renamed that model attribute to something_else.

And form this point if I load my stored record from DB, and trying to access JSON-backed model - I'm getting an error: ActiveModel::UnknownAttributeError.

So I think it's not very flexible. This makes it harder to change that JSON-schema over time.
I think that we could benefit from using same approach as mongoid uses here:
if JSON has some extra attributes - no errors raised, there's no such attribute, so just "ignore" it, but, that data is still accessible from attributes hash.

What do you think?

@DmitryTsepelev
Copy link
Owner

Hi @FunkyloverOne,

Thank you for the interest! Your idea sounds pretty reasonable, let's discuss how we can make this change. The problem is that I use #attributes from Rails and there is no way to add a custom attribute without hackery because

def attributes
  @attributes.to_hash
end

I believe the situation you describe is not a "golden path" and user knows what's going on, so it won't bother him to look up for the "lost" attribute somewhere else–what about #unknown_attributes method? Here is my proof-of-concept PR, there are no docs (yet!) but there is a spec and the code, what do you think? 🙂

@FunkyloverOne
Copy link
Author

Hey @DmitryTsepelev,

I like it! Thanks for such a quick reply and PR! #unknown_attributes method is totally reasonable to me :)

@DmitryTsepelev
Copy link
Owner

Rolled out 0.5.0, it took a bit more time than I expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants