Module: Recliner::AttributeMethods::Dirty

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Dirty
Defined in:
lib/recliner/attribute_methods/dirty.rb

Instance Method Summary collapse

Instance Method Details

#original_attributesObject

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



29
30
31
# File 'lib/recliner/attribute_methods/dirty.rb', line 29

def original_attributes
  attributes.merge(changed_attributes)
end

#save_with_dirty(*args) ⇒ Object

Attempts to save the record and clears changed attributes if successful.



14
15
16
17
18
19
# File 'lib/recliner/attribute_methods/dirty.rb', line 14

def save_with_dirty(*args) #:nodoc:
  if status = save_without_dirty(*args)
    changed_attributes.clear
  end
  status
end

#save_with_dirty!(*args) ⇒ Object

Attempts to save! the record and clears changed attributes if successful.



22
23
24
25
26
# File 'lib/recliner/attribute_methods/dirty.rb', line 22

def save_with_dirty!(*args) #:nodoc:
  status = save_without_dirty!(*args)
  changed_attributes.clear
  status
end

#write_attribute(attr, value) ⇒ Object

Wrap write_attribute to remember original attribute value.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/recliner/attribute_methods/dirty.rb', line 34

def write_attribute(attr, value)
  attr = attr.to_s
    
  # 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
  
  # Carry on.
  super
end