Module: Superstore::AttributeMethods::Dirty

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

Instance Method Summary collapse

Instance Method Details

#old_attribute_value(attr) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/superstore/attribute_methods/dirty.rb', line 30

def old_attribute_value(attr)
  if attribute_changed?(attr)
    changed_attributes[attr]
  else
    read_attribute attr
  end
end

#reloadObject

reload the record and clears changed attributes.



17
18
19
20
21
22
# File 'lib/superstore/attribute_methods/dirty.rb', line 17

def reload
  super.tap do
    @previously_changed.try :clear
    @changed_attributes.try :clear
  end
end

#saveObject

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



8
9
10
11
12
13
14
# File 'lib/superstore/attribute_methods/dirty.rb', line 8

def save(*) #:nodoc:
  if status = super
    @previously_changed = changes
    @changed_attributes = {}
  end
  status
end

#unapplied_changesObject



24
25
26
27
28
# File 'lib/superstore/attribute_methods/dirty.rb', line 24

def unapplied_changes
  result = {}
  changed_attributes.each_key { |attr| result[attr] = read_attribute(attr) }
  result
end

#write_attribute(name, value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/superstore/attribute_methods/dirty.rb', line 38

def write_attribute(name, value)
  name = name.to_s
  old = old_attribute_value(name)

  super

  if old == read_attribute(name)
    changed_attributes.delete(name)
  else
    changed_attributes[name] = old
  end
end