-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,7 @@ module ActiveModel | |
# track. | ||
# * Call <tt>attr_name_will_change!</tt> before each change to the tracked | ||
# attribute. | ||
# | ||
# If you wish to also track previous changes on save or update, you need to | ||
# add: | ||
# | ||
# @previously_changed = changes | ||
# | ||
# inside of your save or update method. | ||
# * Call <tt>changes_applied</tt> after the changes are persisted. | ||
# | ||
# A minimal implementation could be: | ||
# | ||
|
@@ -39,8 +33,8 @@ module ActiveModel | |
# end | ||
# | ||
# def save | ||
# @previously_changed = changes | ||
# @changed_attributes.clear | ||
# # do persistence work | ||
# changes_applied | ||
# end | ||
# end | ||
# | ||
|
@@ -129,7 +123,7 @@ def changes | |
# person.save | ||
# person.previous_changes # => {"name" => ["bob", "robert"]} | ||
def previous_changes | ||
@previously_changed | ||
@previously_changed ||= {} | ||
end | ||
|
||
# Returns a hash of the attributes with unsaved changes indicating their original | ||
|
@@ -144,6 +138,18 @@ def changed_attributes | |
|
||
private | ||
|
||
# Removes current changes and makes them accessible through +previous_changes+. | ||
def changes_applied | ||
@previously_changed = changes | ||
@changed_attributes = {} | ||
end | ||
|
||
# Removes all dirty data: current changes and previous changes | ||
def reset_changes | ||
@previously_changed = {} | ||
@changed_attributes = {} | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't be better to just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think this is right idea. You can assign this hashes to some variable in your code and you might not expect it to change by some internals of activemodel. |
||
|
||
# Handle <tt>*_changed?</tt> for +method_missing+. | ||
def attribute_changed?(attr) | ||
changed_attributes.include?(attr) | ||
|
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.
We have to put the
reset_changes
in the class documentation or users will not know it exists.