Module: ActiveRecord::AttributeMethods::Dirty

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

Overview

:nodoc:

Constant Summary

Constants included from ActiveModel::AttributeMethods

ActiveModel::AttributeMethods::CALL_COMPILABLE_REGEXP, ActiveModel::AttributeMethods::NAME_COMPILABLE_REGEXP

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, class_methods, extended, included

Methods included from ActiveModel::Dirty

#attribute_changed?, #attribute_previously_changed?, #attribute_was, #changed, #changed?, #restore_attributes

Methods included from ActiveModel::AttributeMethods

#attribute_missing, #method_missing, #respond_to?, #respond_to_without_attributes?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveModel::AttributeMethods

Instance Method Details

#attribute_changed_in_place?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)

97
98
99
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 97

def attribute_changed_in_place?(attr_name)
  mutation_tracker.changed_in_place?(attr_name)
end

#changed_attributesObject


77
78
79
80
81
82
83
84
85
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 77

def changed_attributes
  # This should only be set by methods which will call changed_attributes
  # multiple times when it is known that the computed value cannot change.
  if defined?(@cached_changed_attributes)
    @cached_changed_attributes
  else
    super.reverse_merge(mutation_tracker.changed_values).freeze
  end
end

#changesObject


87
88
89
90
91
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 87

def changes
  cache_changed_attributes do
    super
  end
end

#changes_appliedObject


52
53
54
55
56
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 52

def changes_applied
  @previous_mutation_tracker = mutation_tracker
  @changed_attributes = HashWithIndifferentAccess.new
  store_original_attributes
end

#clear_attribute_changes(attr_names) ⇒ Object


70
71
72
73
74
75
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 70

def clear_attribute_changes(attr_names)
  super
  attr_names.each do |attr_name|
    clear_attribute_change(attr_name)
  end
end

#clear_changes_informationObject


58
59
60
61
62
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 58

def clear_changes_information
  @previous_mutation_tracker = nil
  @changed_attributes = HashWithIndifferentAccess.new
  store_original_attributes
end

#initialize_dup(other) ⇒ Object

:nodoc:


44
45
46
47
48
49
50
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 44

def initialize_dup(other) # :nodoc:
  super
  @attributes = self.class._default_attributes.map do |attr|
    attr.with_value_from_user(@attributes.fetch_value(attr.name))
  end
  @mutation_tracker = nil
end

#previous_changesObject


93
94
95
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 93

def previous_changes
  previous_mutation_tracker.changes
end

#raw_write_attribute(attr_name) ⇒ Object


64
65
66
67
68
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 64

def raw_write_attribute(attr_name, *)
  result = super
  clear_attribute_change(attr_name)
  result
end

#reloadObject

reload the record and clears changed attributes.


36
37
38
39
40
41
42
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 36

def reload(*)
  super.tap do
    @mutation_tracker = nil
    @previous_mutation_tracker = nil
    @changed_attributes = HashWithIndifferentAccess.new
  end
end

#saveObject

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


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

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

#save!Object

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


29
30
31
32
33
# File 'activerecord/lib/active_record/attribute_methods/dirty.rb', line 29

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