Class: ActiveRecord::AttributeMutationTracker

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

Overview

:nodoc:

Constant Summary collapse

OPTION_NOT_GIVEN =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeMutationTracker

Returns a new instance of AttributeMutationTracker.



5
6
7
8
9
# File 'lib/active_record/attribute_mutation_tracker.rb', line 5

def initialize(attributes)
  @attributes = attributes
  @forced_changes = Set.new
  @deprecated_forced_changes = Set.new
end

Instance Method Details

#any_changes?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_record/attribute_mutation_tracker.rb', line 34

def any_changes?
  attr_names.any? { |attr| changed?(attr) } || deprecated_forced_changes.any?
end

#change_to_attribute(attr_name) ⇒ Object



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

def change_to_attribute(attr_name)
  if changed?(attr_name)
    [attributes[attr_name].original_value, attributes.fetch_value(attr_name)]
  end
end

#changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/active_record/attribute_mutation_tracker.rb', line 38

def changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
  attr_name = attr_name.to_s
  forced_changes.include?(attr_name) ||
    attributes[attr_name].changed? &&
    (OPTION_NOT_GIVEN == from || attributes[attr_name].original_value == from) &&
    (OPTION_NOT_GIVEN == to || attributes[attr_name].value == to)
end

#changed_in_place?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/active_record/attribute_mutation_tracker.rb', line 46

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

#changed_valuesObject



11
12
13
14
15
16
17
# File 'lib/active_record/attribute_mutation_tracker.rb', line 11

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



19
20
21
22
23
24
25
26
# File 'lib/active_record/attribute_mutation_tracker.rb', line 19

def changes
  attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
    change = change_to_attribute(attr_name)
    if change
      result[attr_name] = change
    end
  end
end

#deprecated_force_change(attr_name) ⇒ Object



64
65
66
# File 'lib/active_record/attribute_mutation_tracker.rb', line 64

def deprecated_force_change(attr_name)
  deprecated_forced_changes << attr_name.to_s
end

#force_change(attr_name) ⇒ Object



60
61
62
# File 'lib/active_record/attribute_mutation_tracker.rb', line 60

def force_change(attr_name)
  forced_changes << attr_name.to_s
end

#forget_change(attr_name) ⇒ Object



50
51
52
53
54
# File 'lib/active_record/attribute_mutation_tracker.rb', line 50

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

#original_value(attr_name) ⇒ Object



56
57
58
# File 'lib/active_record/attribute_mutation_tracker.rb', line 56

def original_value(attr_name)
  attributes[attr_name].original_value
end