Class: ActiveRecord::AttributeMutationTracker

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/attribute_mutation_tracker.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeMutationTracker

Returns a new instance of AttributeMutationTracker.


3
4
5
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 3

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#changed?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)

23
24
25
26
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 23

def changed?(attr_name)
  attr_name = attr_name.to_s
  attributes[attr_name].changed?
end

#changed_in_place?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)

28
29
30
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 28

def changed_in_place?(attr_name)
  attributes[attr_name].changed_in_place?
end

#changed_valuesObject


7
8
9
10
11
12
13
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 7

def changed_values
  attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
    if changed?(attr_name)
      result[attr_name] = attributes[attr_name].original_value
    end
  end
end

#changesObject


15
16
17
18
19
20
21
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 15

def changes
  attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
    if changed?(attr_name)
      result[attr_name] = [attributes[attr_name].original_value, attributes.fetch_value(attr_name)]
    end
  end
end

#forget_change(attr_name) ⇒ Object


32
33
34
35
# File 'activerecord/lib/active_record/attribute_mutation_tracker.rb', line 32

def forget_change(attr_name)
  attr_name = attr_name.to_s
  attributes[attr_name] = attributes[attr_name].forgetting_assignment
end