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

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')

Parameters:

  • attr (Symbol)

    attribute name

  • options (Hash) (defaults to: {})

    conditions on from and to value (optional)

Options Hash (options):

  • :from (Symbol)

    previous attribute value

  • :to (Symbol)

    current attribute value

Returns:

  • (Boolean)


155
156
157
158
159
160
# File 'lib/dynamoid/dirty.rb', line 155

def attribute_changed?(attr, options = {})
  result = changes_include?(attr)
  result &&= options[:to] == __send__(attr) if options.key?(:to)
  result &&= options[:from] == changed_attributes[attr] if options.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"]

Parameters:

  • attr (Symbol)

Returns:

  • (Array)


205
206
207
# File 'lib/dynamoid/dirty.rb', line 205

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

Parameters:

  • attr (Symbol)

    attribute name

Returns:

  • (true|false)


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

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"

Parameters:

  • attr (Symbol)

    attribute name



170
171
172
# File 'lib/dynamoid/dirty.rb', line 170

def attribute_was(attr)
  attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
end

#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])


103
104
105
# File 'lib/dynamoid/dirty.rb', line 103

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)


91
92
93
# File 'lib/dynamoid/dirty.rb', line 91

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)


139
140
141
# File 'lib/dynamoid/dirty.rb', line 139

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)


115
116
117
# File 'lib/dynamoid/dirty.rb', line 115

def changes
  ActiveSupport::HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }]
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)


127
128
129
# File 'lib/dynamoid/dirty.rb', line 127

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

#reloadObject



78
79
80
81
82
# File 'lib/dynamoid/dirty.rb', line 78

def reload(*)
  super.tap do
    clear_changes_information
  end
end

#restore_attributes(attributes = changed) ⇒ Object

Restore all previous data of the provided attributes.

Parameters:

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

    a list of attribute names



177
178
179
# File 'lib/dynamoid/dirty.rb', line 177

def restore_attributes(attributes = changed)
  attributes.each { |attr| restore_attribute! attr }
end

#saveObject



49
50
51
52
53
54
# File 'lib/dynamoid/dirty.rb', line 49

def save(*)
  if status = super
    changes_applied
  end
  status
end

#save!Object



57
58
59
60
61
# File 'lib/dynamoid/dirty.rb', line 57

def save!(*)
  super.tap do
    changes_applied
  end
end

#updateObject



64
65
66
67
68
# File 'lib/dynamoid/dirty.rb', line 64

def update(*)
  super.tap do
    clear_changes_information
  end
end

#update!Object



71
72
73
74
75
# File 'lib/dynamoid/dirty.rb', line 71

def update!(*)
  super.tap do
    clear_changes_information
  end
end