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: https://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])


101
102
103
# File 'lib/dynamoid/dirty.rb', line 101

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)


89
90
91
# File 'lib/dynamoid/dirty.rb', line 89

def changed?
  changed_attributes.present?
end

#changed_attributesActiveSupport::HashWithIndifferentAccess

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)


137
138
139
# File 'lib/dynamoid/dirty.rb', line 137

def changed_attributes
  attributes_changed_by_setter.merge(attributes_changed_in_place)
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)


113
114
115
# File 'lib/dynamoid/dirty.rb', line 113

def changes
  ActiveSupport::HashWithIndifferentAccess[changed_attributes.map { |name, old_value| [name, [old_value, read_attribute(name)]] }]
end

#changes_appliedObject

Clears dirty data and moves changes to previous_changes.



149
150
151
152
153
# File 'lib/dynamoid/dirty.rb', line 149

def changes_applied
  @previously_changed = changes
  @attributes_changed_by_setter = ActiveSupport::HashWithIndifferentAccess.new
  @attributes_from_database = HashWithIndifferentAccess.new(DeepDupper.dup_attributes(@attributes, self.class))
end

#clear_attribute_changes(names) ⇒ Object

Remove changes information for the provided attributes.

Parameters:

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


158
159
160
161
162
163
# File 'lib/dynamoid/dirty.rb', line 158

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

  slice = HashWithIndifferentAccess.new(@attributes).slice(*names)
  attributes_from_database.merge!(DeepDupper.dup_attributes(slice, self.class))
end

#clear_changes_informationObject

Clear all dirty data: current changes and previous changes.



142
143
144
145
146
# File 'lib/dynamoid/dirty.rb', line 142

def clear_changes_information
  @previously_changed = ActiveSupport::HashWithIndifferentAccess.new
  @attributes_changed_by_setter = ActiveSupport::HashWithIndifferentAccess.new
  @attributes_from_database = HashWithIndifferentAccess.new(DeepDupper.dup_attributes(@attributes, self.class))
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)


125
126
127
# File 'lib/dynamoid/dirty.rb', line 125

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

#restore_attributes(names = changed) ⇒ Object

Restore all previous data of the provided attributes.

Parameters:

  • names (Array[Symbol]) (defaults to: changed)

    a list of attribute names



199
200
201
# File 'lib/dynamoid/dirty.rb', line 199

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