Module: Dynamoid::Dirty
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods
- Included in:
- Components
- Defined in:
- lib/dynamoid/dirty.rb
Overview
Support interface of Rails’ ActiveModel::Dirty module
The reason why not just include ActiveModel::Dirty - ActiveModel::Dirty conflicts either with @attributes or #attributes in different Rails versions.
Separate implementation (or copy-pasting) is the best way to avoid endless monkey-patching
Documentation: api.rubyonrails.org/v4.2/classes/ActiveModel/Dirty.html
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#attribute_changed?(attr, options = {}) ⇒ Boolean
Handle
*_changed?
formethod_missing
. -
#attribute_previous_change(attr) ⇒ Object
Handles
*_previous_change
formethod_missing
. -
#attribute_previously_changed?(attr) ⇒ Boolean
Handles
*_previously_changed?
formethod_missing
. -
#attribute_was(attr) ⇒ Object
Handle
*_was
formethod_missing
. -
#changed ⇒ Object
Returns an array with the name of the attributes with unsaved changes.
-
#changed? ⇒ Boolean
Returns
true
if any attribute have unsaved changes,false
otherwise. -
#changed_attributes ⇒ Object
(also: #attributes_changed_by_setter)
Returns a hash of the attributes with unsaved changes indicating their original values like
attr => original value
. -
#changes ⇒ Object
Returns a hash of changed attributes indicating their original and new values like
attr => [original value, new value]
. -
#previous_changes ⇒ Object
Returns a hash of attributes that were changed before the model was saved.
- #reload ⇒ Object
-
#restore_attributes(attributes = changed) ⇒ Object
Restore all previous data of the provided attributes.
- #save ⇒ Object
- #save! ⇒ Object
- #update ⇒ Object
- #update! ⇒ Object
Instance Method Details
#attribute_changed?(attr, options = {}) ⇒ Boolean
Handle *_changed?
for method_missing
.
127 128 129 130 131 132 |
# File 'lib/dynamoid/dirty.rb', line 127 def attribute_changed?(attr, = {}) #:nodoc: result = changes_include?(attr) result &&= [:to] == __send__(attr) if .key?(:to) result &&= [:from] == changed_attributes[attr] if .key?(:from) result end |
#attribute_previous_change(attr) ⇒ Object
Handles *_previous_change
for method_missing
.
150 151 152 |
# File 'lib/dynamoid/dirty.rb', line 150 def attribute_previous_change(attr) previous_changes[attr] if attribute_previously_changed?(attr) end |
#attribute_previously_changed?(attr) ⇒ Boolean
Handles *_previously_changed?
for method_missing
.
145 146 147 |
# File 'lib/dynamoid/dirty.rb', line 145 def attribute_previously_changed?(attr) #:nodoc: previous_changes_include?(attr) end |
#attribute_was(attr) ⇒ Object
Handle *_was
for method_missing
.
135 136 137 |
# File 'lib/dynamoid/dirty.rb', line 135 def attribute_was(attr) # :nodoc: attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) end |
#changed ⇒ Object
Returns an array with the name of the attributes with unsaved changes.
person.changed # => []
person.name = 'bob'
person.changed # => ["name"]
92 93 94 |
# File 'lib/dynamoid/dirty.rb', line 92 def changed changed_attributes.keys end |
#changed? ⇒ Boolean
Returns true
if any attribute have unsaved changes, false
otherwise.
person.changed? # => false
person.name = 'bob'
person.changed? # => true
83 84 85 |
# File 'lib/dynamoid/dirty.rb', line 83 def changed? changed_attributes.present? end |
#changed_attributes ⇒ Object Also known as: attributes_changed_by_setter
Returns a hash of the attributes with unsaved changes indicating their original values like attr => original value
.
person.name # => "bob"
person.name = 'robert'
person.changed_attributes # => {"name" => "bob"}
122 123 124 |
# File 'lib/dynamoid/dirty.rb', line 122 def changed_attributes @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end |
#changes ⇒ Object
Returns a hash of changed attributes indicating their original and new values like attr => [original value, new value]
.
person.changes # => {}
person.name = 'bob'
person.changes # => { "name" => ["bill", "bob"] }
102 103 104 |
# File 'lib/dynamoid/dirty.rb', line 102 def changes ActiveSupport::HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }] end |
#previous_changes ⇒ Object
Returns a hash of attributes that were changed before the model was saved.
person.name # => "bob"
person.name = 'robert'
person.save
person.previous_changes # => {"name" => ["bob", "robert"]}
112 113 114 |
# File 'lib/dynamoid/dirty.rb', line 112 def previous_changes @previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new end |
#reload ⇒ Object
72 73 74 75 76 |
# File 'lib/dynamoid/dirty.rb', line 72 def reload(*) super.tap do clear_changes_information end end |
#restore_attributes(attributes = changed) ⇒ Object
Restore all previous data of the provided attributes.
140 141 142 |
# File 'lib/dynamoid/dirty.rb', line 140 def restore_attributes(attributes = changed) attributes.each { |attr| restore_attribute! attr } end |
#save ⇒ Object
47 48 49 50 51 52 |
# File 'lib/dynamoid/dirty.rb', line 47 def save(*) if status = super changes_applied end status end |
#save! ⇒ Object
54 55 56 57 58 |
# File 'lib/dynamoid/dirty.rb', line 54 def save!(*) super.tap do changes_applied end end |
#update ⇒ Object
60 61 62 63 64 |
# File 'lib/dynamoid/dirty.rb', line 60 def update(*) super.tap do clear_changes_information end end |
#update! ⇒ Object
66 67 68 69 70 |
# File 'lib/dynamoid/dirty.rb', line 66 def update!(*) super.tap do clear_changes_information end end |