-
Notifications
You must be signed in to change notification settings - Fork 92
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
Implement Parent tracking #35
Implement Parent tracking #35
Conversation
93e2c12
to
9998c33
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @blaze182, that's a good start!
I've made a couple of comments here and there, but it feels like there should be a place in the Rails codebase where we have an access to both AR model and the attribute. A quick search brought me here.
This is the patch that made your spec pass:
ActiveRecord::Base.prepend(Module.new do
def _read_attribute(*)
super.tap do |attr|
attr.parent = self if attr.is_a?(StoreModel::Model)
end
end
end)
I'm definitely not sure that it's a single place we have to patch (e.g. it does not handle the case when we assign the attribute value to the AR model etc.), but the overall approach should help avoiding the context and passing it here and there 🙂What do you think?
Thanks for the feedback! The motivation behind preserving parent reference and passing it top-down was attempt to eliminate possible edge-cases (i.e. reassignments, ActiveModel::Dirty tracking, attributes restoring and reloads etc.) by having the StoreModel initiated with parent straight away. As a bonus, we are leaving parent assignment logic encapsulated in Types :) Sent from phone, so pardon any typos |
866578a
to
8c47a55
Compare
After digging deeper I found out that context approach isn't stable enough for attribute assignment case, so decided to proceed with read/write approach as you initially suggested. Looks like it's working now for AR objects for both json and array types :) |
Wow, that looks short! Great progress! |
Hi Dmitry, hope you're doing well! The only limitation I see now is direct StoreModel's Array mutation, it won't update a parent until one of the accessor methods gets called. As far as I can see it's achievable if we could introduce |
That looks awesome, thanks for the great work! I'll play with it a little bit on Thursday and come back with a feedback (if I bump into something). In the meantime, could you please add yourself into the changelog? |
e02c1e8
to
174422e
Compare
Also refactor ActiveRecord support and specs
174422e
to
5cd4c8b
Compare
Thank you, let me know how it goes :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blaze182 I've played with it and it looks amazing! I have only one question before merging it in and creating a follow-up issue for handling collections 🙂
|
||
private | ||
|
||
def attribute(*) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know the case when we need this patch? I've tried to remove it and all specs are passing, looks like this scenario is covered by #_read_attribute
method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! We had a preemptive parent assignment on attribute write in specs :)
I updated scenarios, so now couple specs should fail accordingly
Merging, thanks again! 🎉 |
Hi @blaze182! Could you please send me your email and mailing address (we plan to send some goodies to CultOfMartians participants) to |
Woohoo, thanks @DmitryTsepelev! Just dropped you a line |
G'day, trying to resolve #31 :)
This is still work in progress, needs some cleaning up, but parent is finally there, at least locally :)
Like
StoreModel::Types::*
,ActiveModel::Attribute
initializer unfortunately doesn't have access to ActiveRecord/ActiveModel context as well, so I tried to introduce the patches to provide it during AttributeSet initialization and modification.Hope the overall direction makes sense, open to any suggestions