Module: Dynamoid::Dirty

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
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

Instance Method Summary collapse

Instance Method Details

#changedArray[String]

Returns an array with names of the attributes with unsaved changes.

person = Person.new
person.changed # => []
person.name = 'Bob'
person.changed # => ["name"]

Returns:

  • (Array[String])


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

Returns:

  • (true|false)


86
87
88
# File 'lib/dynamoid/dirty.rb', line 86

def changed?
  changed_attributes.present?
end

#changed_attributesActiveSupport::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"}

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)


134
135
136
# File 'lib/dynamoid/dirty.rb', line 134

def changed_attributes
  @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end

#changesActiveSupport::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"] }

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)


110
111
112
# File 'lib/dynamoid/dirty.rb', line 110

def changes
  ActiveSupport::HashWithIndifferentAccess[changed.map { |name| [name, attribute_change(name)] }]
end

#changes_appliedObject

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.

Parameters:

  • attributes (Array[String])
    • a list of attributes to clear changes for



153
154
155
# File 'lib/dynamoid/dirty.rb', line 153

def clear_attribute_changes(names)
  attributes_changed_by_setter.except!(*names)
end

#clear_changes_informationObject

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_changesActiveSupport::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"]}

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)


122
123
124
# File 'lib/dynamoid/dirty.rb', line 122

def previous_changes
  @previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new
end

#restore_attributes(names = changed) ⇒ Object

Restore all previous data of the provided attributes.

Parameters:

  • attributes (Array[Symbol])

    a list of attribute names



191
192
193
# File 'lib/dynamoid/dirty.rb', line 191

def restore_attributes(names = changed)
  names.each { |name| restore_attribute! name }
end