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) ⇒ Array
Handles
*_previous_change
formethod_missing
. -
#attribute_previously_changed?(attr) ⇒ true|false
Handles
*_previously_changed?
formethod_missing
. -
#attribute_was(attr) ⇒ 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]
. -
#previous_changes ⇒ ActiveSupport::HashWithIndifferentAccess
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
.
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')
152 153 154 155 156 157 |
# File 'lib/dynamoid/dirty.rb', line 152 def attribute_changed?(attr, = {}) 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) ⇒ Array
Handles *_previous_change
for method_missing
.
person = Person.create(name: 'Alice')
person.name = 'Bob'
person.save
person.attribute_previously_changed(:name) # => ["Alice", "Bob"]
202 203 204 |
# File 'lib/dynamoid/dirty.rb', line 202 def attribute_previous_change(attr) previous_changes[attr] if attribute_previously_changed?(attr) end |
#attribute_previously_changed?(attr) ⇒ true|false
Handles *_previously_changed?
for method_missing
.
person = Person.create(name: 'Alice')
person.name = 'Bob'
person.save
person.attribute_changed?(:name) # => true
188 189 190 |
# File 'lib/dynamoid/dirty.rb', line 188 def attribute_previously_changed?(attr) previous_changes_include?(attr) end |
#attribute_was(attr) ⇒ Object
Handle *_was
for method_missing
.
person = Person.create(name: 'Alice')
person.name = 'Bob'
person.attribute_was(:name) # => "Alice"
167 168 169 |
# File 'lib/dynamoid/dirty.rb', line 167 def attribute_was(attr) attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) 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"]
100 101 102 |
# File 'lib/dynamoid/dirty.rb', line 100 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
88 89 90 |
# File 'lib/dynamoid/dirty.rb', line 88 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"}
136 137 138 |
# File 'lib/dynamoid/dirty.rb', line 136 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"] }
112 113 114 |
# File 'lib/dynamoid/dirty.rb', line 112 def changes ActiveSupport::HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }] 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"]}
124 125 126 |
# File 'lib/dynamoid/dirty.rb', line 124 def previous_changes @previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new end |
#reload ⇒ Object
75 76 77 78 79 |
# File 'lib/dynamoid/dirty.rb', line 75 def reload(*) super.tap do clear_changes_information end end |
#restore_attributes(attributes = changed) ⇒ Object
Restore all previous data of the provided attributes.
174 175 176 |
# File 'lib/dynamoid/dirty.rb', line 174 def restore_attributes(attributes = changed) attributes.each { |attr| restore_attribute! attr } end |
#save ⇒ Object
47 48 49 50 51 |
# File 'lib/dynamoid/dirty.rb', line 47 def save(*) super.tap do |status| changes_applied if status end 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
61 62 63 64 65 |
# File 'lib/dynamoid/dirty.rb', line 61 def update(*) super.tap do clear_changes_information end end |
#update! ⇒ Object
68 69 70 71 72 |
# File 'lib/dynamoid/dirty.rb', line 68 def update!(*) super.tap do clear_changes_information end end |