Module: DatastaxRails::AttributeMethods::Dirty

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

Overview

Change tracking for attributes. Builds on ActiveModel::Dirty in order to reset changes on save.

Instance Method Summary collapse

Instance Method Details

#reloadObject

reload the record and clears changed attributes.


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

def reload(*)
  super.tap do
    @previously_changed.clear
    @changed_attributes.clear
  end
end

#saveObject

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


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/datastax_rails/attribute_methods/dirty.rb', line 16

def save(*) #:nodoc:
  if (status = super)
    # FIXME: This dup/replace mess is here to work around what APPEARS to be a bug in Rails
    # See Issue #29 for details
    attrs = @attributes.dup
    @previously_changed = changes
    @attributes = attrs
    @changed_attributes.clear
  end
  status
end

#save!Object

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


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

def save!(*) #:nodoc:
  super.tap do
    attrs = @attributes.dup
    @previously_changed = changes
    @attributes = attrs
    @changed_attributes.clear
  end
end

#write_attribute(attr, value) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datastax_rails/attribute_methods/dirty.rb', line 46

def write_attribute(attr, value)
  attr = attr.to_s
  loaded_attributes[attr] = true

  # The attribute already has an unsaved change.
  if attribute_changed?(attr)
    old = @changed_attributes[attr]
    @changed_attributes.delete(attr) unless _field_changed?(attr, old, value)
  else
    old = clone_attribute_value(:read_attribute, attr)
    @changed_attributes[attr] = old if _field_changed?(attr, old, value)
  end

  super
end