Module: MongoModel::AttributeMethods::Dirty

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Included in:
EmbeddedDocument
Defined in:
lib/mongomodel/concerns/attribute_methods/dirty.rb

Instance Method Summary collapse

Instance Method Details

#attribute_changed?(attr, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) ⇒ Boolean

Returns:



40
41
42
43
44
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 40

def attribute_changed?(attr, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
  !!changes_include?(attr) &&
    (to == OPTION_NOT_GIVEN || to == __send__(attr)) &&
    (from == OPTION_NOT_GIVEN || from == changed_attributes[attr])
end

#attribute_previously_changed?(attr) ⇒ Boolean

Returns:



50
51
52
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 50

def attribute_previously_changed?(attr)
  previous_changes.include?(attr)
end

#attribute_was(attr) ⇒ Object



46
47
48
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 46

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

#changedObject



24
25
26
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 24

def changed
  changed_attributes.keys
end

#changed?Boolean

Returns:



20
21
22
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 20

def changed?
  changed_attributes.present?
end

#changed_attributesObject Also known as: attributes_changed_by_setter



36
37
38
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 36

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

#changesObject



28
29
30
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 28

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

#original_attributesObject

Returns the attributes as they were before any changes were made to the document.



74
75
76
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 74

def original_attributes
  {}.with_indifferent_access.merge(attributes).merge(changed_attributes)
end

#previous_changesObject



32
33
34
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 32

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

#restore_attributes(attributes = changed) ⇒ Object



54
55
56
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 54

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

#write_attribute(key, value) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongomodel/concerns/attribute_methods/dirty.rb', line 58

def write_attribute(key, value)
  attr = key.to_sym

  # The attribute already has an unsaved change.
  if changed_attributes.include?(attr)
    old = changed_attributes[attr]
    changed_attributes.delete(attr) if value == old
  else
    old = clone_attribute_value(attr)
    changed_attributes[attr] = old unless value == old
  end

  super
end