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 AM::Dirty less dirty to plugin into AR or other library #10816
Conversation
|
@@ -142,9 +136,22 @@ def changed_attributes | |||
@changed_attributes ||= {} | |||
end | |||
|
|||
# Removes current changes and makes them accessible through | |||
# #previous_changes. | |||
def changes_applied! |
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.
In my opinion these methods should not be public
@carlosantoniodasilva @josevalim do you have some input about this? |
👍 on this (with all the changes suggested by @rafaelfranca) |
Made all changes suggested by @rafaelfranca |
@changed_attributes = {} | ||
end | ||
|
||
# Handle changes<tt>*_changed?</tt> for +method_missing+. |
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.
I think this comment is still wrong. Should not be: Handle <tt>*_changed?</tt> for +method_missing+.
?
I agree with @egilburg about the bang on the method name. I'd only define they without the bang |
Fixed comment, removed bang. |
@@ -144,6 +138,19 @@ def changed_attributes | |||
|
|||
private | |||
|
|||
# Removes current changes and makes them accessible through | |||
# #previous_changes. |
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.
This should probably be +previous_changes+
We will need a CHANGELOG entry and a rebase. So I'll merge |
Added new API methods #reset_changes and #changed_applied to control changes state. Previsously you needed to update internal instance variables, but now API methods is available. class Person include ActiveModel::Dirty define_attribute_methods :name # Old way: def save # do some work @previously_changed = changes @changed_attributes = nil end def reload # do some work @previously_changed = nil @changed_attributes = nil end # New way: def save # do some work changes_applied! end def reload # do some work reset_changes end end
Done |
Make AM::Dirty less dirty to plugin into AR or other library
Merged |
We discussing the problem of how "reset_changes" doesn't actually reset the changes, as in restore them to their original value. That's creating a mismatch with reset_, which does do that. Would be great to see some real use cases that prompted adding reset_changes in the first place. Can people using it on this thread share? |
Ah, this is how we refactored Active Record to use It's probably more of a "plugin API" rather than something an ordinary user would use in their app. |
Yes it is intended to be a plugin API.
|
After a fresh look at this PR The good name would be I think we can still rename it and deprecate old method without causing too many problems to people who are mixing up |
Added new API methods #reset_changes! and #changed_applied!
to control changes state. Previsously you needed to update internal
instance variables, but now API methods is available.