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?(name, options = {}) ⇒ Boolean
Handle
*_changed?
formethod_missing
. -
#attribute_previous_change(name) ⇒ Array
Handles
*_previous_change
formethod_missing
. -
#attribute_previously_changed?(name) ⇒ true|false
Handles
*_previously_changed?
formethod_missing
. -
#attribute_was(name) ⇒ Object
Handle
*_was
formethod_missing
. -
#changed ⇒ Array[String]
Returns an array with names of the attributes with unsaved changes.
-
#changed? ⇒ true|false
Returns
true
if any attribute have unsaved changes,false
otherwise. -
#changed_attributes ⇒ ActiveSupport::HashWithIndifferentAccess
(also: #attributes_changed_by_setter)
Returns a hash of the attributes with unsaved changes indicating their original values like
attr => original value
. -
#changes ⇒ ActiveSupport::HashWithIndifferentAccess
Returns a hash of changed attributes indicating their original and new values like
attr => [original value, new value]
. -
#changes_applied ⇒ Object
Clears dirty data and moves
changes
toprevious_changes
. -
#clear_attribute_changes(names) ⇒ Object
Remove changes information for the provided attributes.
-
#clear_changes_information ⇒ Object
Clear all dirty data: current changes and previous changes.
-
#previous_changes ⇒ ActiveSupport::HashWithIndifferentAccess
Returns a hash of attributes that were changed before the model was saved.
- #reload ⇒ Object
-
#restore_attributes(names = changed) ⇒ Object
Restore all previous data of the provided attributes.
- #save ⇒ Object
- #save! ⇒ Object
- #update ⇒ Object
- #update! ⇒ Object
Instance Method Details
#attribute_changed?(name, options = {}) ⇒ Boolean
Handle *_changed?
for method_missing
.
person.attribute_changed?(:name) # => true
person.attribute_changed?(:name, from: 'Alice')
person.attribute_changed?(:name, to: 'Bob')
person.attribute_changed?(:name, from: 'Alice', to: 'Bod')
169 170 171 172 173 174 |
# File 'lib/dynamoid/dirty.rb', line 169 def attribute_changed?(name, = {}) result = changes_include?(name) result &&= [:to] == read_attribute(name) if .key?(:to) result &&= [:from] == changed_attributes[name] if .key?(:from) result end |
#attribute_previous_change(name) ⇒ Array
Handles *_previous_change
for method_missing
.
person = Person.create(name: 'Alice')
person.name = 'Bob'
person.save
person.attribute_previously_changed(:name) # => ["Alice", "Bob"]
219 220 221 |
# File 'lib/dynamoid/dirty.rb', line 219 def attribute_previous_change(name) previous_changes[name] if attribute_previously_changed?(name) end |
#attribute_previously_changed?(name) ⇒ true|false
Handles *_previously_changed?
for method_missing
.
person = Person.create(name: 'Alice')
person.name = 'Bob'
person.save
person.attribute_changed?(:name) # => true
205 206 207 |
# File 'lib/dynamoid/dirty.rb', line 205 def attribute_previously_changed?(name) previous_changes_include?(name) end |
#attribute_was(name) ⇒ Object
Handle *_was
for method_missing
.
person = Person.create(name: 'Alice')
person.name = 'Bob'
person.attribute_was(:name) # => "Alice"
184 185 186 |
# File 'lib/dynamoid/dirty.rb', line 184 def attribute_was(name) attribute_changed?(name) ? changed_attributes[name] : read_attribute(name) end |
#changed ⇒ Array[String]
Returns an array with names of the attributes with unsaved changes.
person = Person.new
person.changed # => []
person.name = 'Bob'
person.changed # => ["name"]
98 99 100 |
# File 'lib/dynamoid/dirty.rb', line 98 def changed changed_attributes.keys end |
#changed? ⇒ true|false
Returns true
if any attribute have unsaved changes, false
otherwise.
person.changed? # => false
person.name = 'Bob'
person.changed? # => true
86 87 88 |
# File 'lib/dynamoid/dirty.rb', line 86 def changed? changed_attributes.present? end |
#changed_attributes ⇒ ActiveSupport::HashWithIndifferentAccess 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"}
134 135 136 |
# File 'lib/dynamoid/dirty.rb', line 134 def changed_attributes @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end |
#changes ⇒ ActiveSupport::HashWithIndifferentAccess
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"] }
110 111 112 |
# File 'lib/dynamoid/dirty.rb', line 110 def changes ActiveSupport::HashWithIndifferentAccess[changed.map { |name| [name, attribute_change(name)] }] end |
#changes_applied ⇒ Object
Clears dirty data and moves changes
to previous_changes
.
145 146 147 148 |
# File 'lib/dynamoid/dirty.rb', line 145 def changes_applied @previously_changed = changes @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new end |
#clear_attribute_changes(names) ⇒ Object
Remove changes information for the provided attributes.
153 154 155 |
# File 'lib/dynamoid/dirty.rb', line 153 def clear_attribute_changes(names) attributes_changed_by_setter.except!(*names) end |
#clear_changes_information ⇒ Object
Clear all dirty data: current changes and previous changes.
139 140 141 142 |
# File 'lib/dynamoid/dirty.rb', line 139 def clear_changes_information @previously_changed = ActiveSupport::HashWithIndifferentAccess.new @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new end |
#previous_changes ⇒ ActiveSupport::HashWithIndifferentAccess
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"]}
122 123 124 |
# File 'lib/dynamoid/dirty.rb', line 122 def previous_changes @previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new end |
#reload ⇒ Object
73 74 75 76 77 |
# File 'lib/dynamoid/dirty.rb', line 73 def reload(*) super.tap do clear_changes_information end end |
#restore_attributes(names = changed) ⇒ Object
Restore all previous data of the provided attributes.
191 192 193 |
# File 'lib/dynamoid/dirty.rb', line 191 def restore_attributes(names = changed) names.each { |name| restore_attribute! name } end |
#save ⇒ Object
45 46 47 48 49 |
# File 'lib/dynamoid/dirty.rb', line 45 def save(*) super.tap do |status| changes_applied if status end end |
#save! ⇒ Object
52 53 54 55 56 |
# File 'lib/dynamoid/dirty.rb', line 52 def save!(*) super.tap do changes_applied end end |
#update ⇒ Object
59 60 61 62 63 |
# File 'lib/dynamoid/dirty.rb', line 59 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 |