-
Notifications
You must be signed in to change notification settings - Fork 35
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
get out of the way of rails 5.2 ActiveModel::Attributes #18
Comments
Actually, can we deprecate AttrJson::Model entirely, in favor of ActiveModel::Attributes? Possibly provide a custom type that can serialize AttrJson::Attributes properly in the context of AttrJson::Record serialization and model-inside-model nesting. |
Might mean giving up some features. Ones identified include:
Maybe we don't need these. Or maybe we could replace with a light wrapper over ActiveModel::Model,Attributes,Serialization. |
Yep, returning to this.... seems to mean giving up
Not quite sure what to do. Coming back to this, I forgot about these notes, and thought this would be pretty easy. I am doing this for a future 2.x release, so features can be dropped, but I don't think we want to drop MAYBE we could drop |
Another thing that ActiveModel::Attributes does differently than our implementation:
|
OK I delved into an Right now,
I can change it to I am considering abandoning attempt to actually base on or be compatible with AM::Attributes itself is some very convoluted code including things that I think are used by ActiveRecord but just aren't necessarily and I don't know what the're doing there in a model context. I suspect there were plans for more that never went anywhere, AM::Attributes hasnt' really been touched since sgrif left rails. I will take the liberty of tagging those who thumbs-upped this issue over the years. @stevenharman, @swrobel, @rainerborene, @aried3r, @TylerRick, @jits, @huobazi, @exsemt, @gilbert, @thomasklemm |
So I realized we had to/wanted to take care of that nil serialization issue either way. So did so in #175 So theoretically we could base the implementation on If the upcoming (prob Rails 7.1) normalizes feature were going to be useable in an For now I think I will leave things as they are -- in our upcoming 2.x release the things should be more-or-less indistinguishable from actual But always interested in feedback, or use cases that are made a problem by not actually being I will perhaps leave this ticket open? |
A use case I just encountered where we are using attr_json to fetch things out of the DB but we wanted to use AM:Attributes to add temporary accessors while running the code with the benefits of defaults, typecasting etc, normalizing will also be great once out. I wasnt aware of this issue and saw that all the attr_jsons were set to nil. There are plenty of workarounds, but as attributes become more useful, this will eventually be needed |
Hi, thanks for the feedback @paulodeon, I appreciate it! Hm. Well, maybe we can get this in there before I release attr_json 2.0, I'll mess with it some more. Can you explain more about:
I don't understand what you mean there. "all the attr_jsons were set to nil"? Ah, are you saying you tried to manually
Be aware that as far as I can tell, there's not currently anything committed to Rails -- or as far as I know planned -- that will let you use the new normalization features with Now... here's the thing though, about your use case... if I do what I am thinking of to merge ActiveModel::Attributes in with AttrJson::Model... you would still not be able to add temporary accessors that are not persisted , if that's what you meant? If I actually merge things, then anything you declare with If your use case requires the ability to declare attributes that are NOT persisted.... that requires more thought to figure out how to meet. Just using (Note that other alternative solutions to AttrJson that have nested models that are ActiveModel::Attributes based -- also I think don't meet your use case , I think they will all serialize all attributes! That is the most 'natural' way to implement it). |
Hey @jrochkind, thanks for the reply. Here's a little code as an example, but yes, you got it right about the nils. class AccessGroup
include AttrJson::Model
include ActiveModel::Attributes
attr_json :id, :string, default: -> { SecureRandom.alphanumeric(8) }
attr_json :name, Name.to_type
attribute :for_option_model, :boolean, default: false
end
class App < ApplicationRecord
attr_json :access_group, AccessGroup.to_type
end
App.new.access_group.id # => nil The Also thanks for the info about the normalization, but I would guess given past history AR Features tend to work their way to AM, over time. Now I recognise that my use case may be an unusual one and as you say ActiveModel::Attributes is specifically designed for attributes that are not persisted. By merging with it you are in fact using it to persist attributes. In addition you would be removing a piece of rails functionality, which isnt ideal either. Conceptually I can see that it would make it more difficult to implement all the AM features here but surely there is also some value from being a non-AM solution? Perhaps adding some info to the docs suggesting why this is better than using AM directly? |
OK thanks @paulodeon ! I appreciate the discussion! (Also some history is that the original attr-json implementation actually precedes ActiveModel::Attributes existing!) The thing is, that the solution I was thinking of wouldn't meet your use case either....
I don't see it that way. AttrJson is designed for taking pure-ruby models and letting you serialize them to json (in nested ways). The thing is that if I completely stay away from AM::Attributes without merging with it... our BUT, in a straight I don't think what you are asking for is realistic, if you think through all the edge cases. (In another alternate implementation I know of for JSON serialization to DB, which is based on AM::Attributes -- all AM::Attributes are serialized. https://github.com/DmitryTsepelev/store_model -- that makes sense to me) BUT, as far as your actual use case.... Would you be interested in a: attr_json :for_option_model, :boolean, default: false, store_key: false That would let you define an attr_json that is a "virtual" attribute that is not persisted. ( If you are interested in that, I think it's actually pretty easy to add! Do you want to file a separate issue on it? |
Rails 5.2 has a real attributes API for ActiveModel.
We're doing okay without it, and supporting Rails 5.0-5.2, but let's rename some of our methods and ivars in JsonAttribute::Model to step out of it's way and not conflict, to avoid any weirdness, and in case someone wants to use both.
(Jan 18 2024: We really should move to the models having ActiveModel attributes, and maybe include ActiveModel::API by defualt. See some things possible at https://betacraft.com/blog/08/06/2023/active-model-jsonb-coulmn.html, although it's not totally clear how this gets integrated with an AR container, as they suggest. https://www.reddit.com/r/ruby/comments/199yvhl/progressively_adopt_activemodelapi_for_your_poros/).
The text was updated successfully, but these errors were encountered: