Module: ActiveRecord::AttributeMethods::Dirty

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#attribute_before_last_save(attr_name) ⇒ Object

Returns the original value of an attribute before the last save. Behaves similarly to attribute_was. This method is useful in after callbacks to get the original value of an attribute before the save that just occurred


165
166
167
# File 'lib/active_record/attribute_methods/dirty.rb', line 165

def attribute_before_last_save(attr_name)
  mutations_before_last_save.original_value(attr_name)
end

#attribute_changeObject


219
220
221
222
# File 'lib/active_record/attribute_methods/dirty.rb', line 219

def attribute_change(*)
  emit_warning_if_needed("attribute_change", "saved_change_to_attribute")
  super
end

#attribute_change_to_be_saved(attr_name) ⇒ Object

Alias for `attribute_change`


185
186
187
# File 'lib/active_record/attribute_methods/dirty.rb', line 185

def attribute_change_to_be_saved(attr_name)
  mutations_from_database.change_to_attribute(attr_name)
end

#attribute_changed?Boolean

Returns:

  • (Boolean)

224
225
226
227
# File 'lib/active_record/attribute_methods/dirty.rb', line 224

def attribute_changed?(*)
  emit_warning_if_needed("attribute_changed?", "saved_change_to_attribute?")
  super
end

#attribute_changed_in_place?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)

127
128
129
# File 'lib/active_record/attribute_methods/dirty.rb', line 127

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

#attribute_in_database(attr_name) ⇒ Object

Alias for `attribute_was`


190
191
192
# File 'lib/active_record/attribute_methods/dirty.rb', line 190

def attribute_in_database(attr_name)
  mutations_from_database.original_value(attr_name)
end

#attribute_wasObject


214
215
216
217
# File 'lib/active_record/attribute_methods/dirty.rb', line 214

def attribute_was(*)
  emit_warning_if_needed("attribute_was", "attribute_before_last_save")
  super
end

#attributes_in_databaseObject

Alias for `changed_attributes`


210
211
212
# File 'lib/active_record/attribute_methods/dirty.rb', line 210

def attributes_in_database
  changes_to_save.transform_values(&:first)
end

#changedObject


234
235
236
237
# File 'lib/active_record/attribute_methods/dirty.rb', line 234

def changed(*)
  emit_warning_if_needed("changed", "saved_changes.keys")
  super
end

#changed?Boolean

Returns:

  • (Boolean)

229
230
231
232
# File 'lib/active_record/attribute_methods/dirty.rb', line 229

def changed?(*)
  emit_warning_if_needed("changed?", "saved_changes?")
  super
end

#changed_attribute_names_to_saveObject

Alias for `changed`


205
206
207
# File 'lib/active_record/attribute_methods/dirty.rb', line 205

def changed_attribute_names_to_save
  changes_to_save.keys
end

#changed_attributesObject


97
98
99
100
101
102
103
104
105
106
# File 'lib/active_record/attribute_methods/dirty.rb', line 97

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
    emit_warning_if_needed("changed_attributes", "saved_changes.transform_values(&:first)")
    super.reverse_merge(mutation_tracker.changed_values).freeze
  end
end

#changesObject


108
109
110
111
112
113
# File 'lib/active_record/attribute_methods/dirty.rb', line 108

def changes
  cache_changed_attributes do
    emit_warning_if_needed("changes", "saved_changes")
    super
  end
end

#changes_appliedObject


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

def changes_applied
  @previous_mutation_tracker = mutation_tracker
  @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
  clear_mutation_trackers
end

#changes_internally_appliedObject

:nodoc:


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

def changes_internally_applied # :nodoc:
  @mutations_before_last_save = mutation_tracker
  forget_attribute_assignments
  @mutations_from_database = AttributeMutationTracker.new(@attributes)
end

#changes_to_saveObject

Alias for `changes`


200
201
202
# File 'lib/active_record/attribute_methods/dirty.rb', line 200

def changes_to_save
  mutations_from_database.changes
end

#clear_attribute_changes(attr_names) ⇒ Object


90
91
92
93
94
95
# File 'lib/active_record/attribute_methods/dirty.rb', line 90

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

#clear_changes_informationObject


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

def clear_changes_information
  @previous_mutation_tracker = nil
  @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
  forget_attribute_assignments
  clear_mutation_trackers
end

#has_changes_to_save?Boolean

Alias for `changed?`

Returns:

  • (Boolean)

195
196
197
# File 'lib/active_record/attribute_methods/dirty.rb', line 195

def has_changes_to_save?
  mutations_from_database.any_changes?
end

#initialize_dup(other) ⇒ Object

:nodoc:


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

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
  clear_mutation_trackers
end

#previous_changesObject


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_record/attribute_methods/dirty.rb', line 115

def previous_changes
  unless previous_mutation_tracker.equal?(mutations_before_last_save)
    ActiveSupport::Deprecation.warn(<<-EOW.strip_heredoc)
      The behavior of `previous_changes` inside of after callbacks is
      deprecated without replacement. In the next release of Rails,
      this method inside of `after_save` will return the changes that
      were just saved.
    EOW
  end
  previous_mutation_tracker.changes
end

#raw_write_attribute(attr_name) ⇒ Object


84
85
86
87
88
# File 'lib/active_record/attribute_methods/dirty.rb', line 84

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

#reloadObject

reload the record and clears changed attributes.


49
50
51
52
53
54
55
# File 'lib/active_record/attribute_methods/dirty.rb', line 49

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

#saveObject

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


34
35
36
37
38
39
# File 'lib/active_record/attribute_methods/dirty.rb', line 34

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

#save!Object

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


42
43
44
45
46
# File 'lib/active_record/attribute_methods/dirty.rb', line 42

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

#saved_change_to_attribute(attr_name) ⇒ Object

Returns the change to an attribute during the last save. If the attribute was changed, the result will be an array containing the original value and the saved value.

Behaves similarly to attribute_change. This method is useful in after callbacks, to see the change in an attribute that just occurred

This method can be invoked as `saved_change_to_name` in instead of `saved_change_to_attribute(“name”)`


157
158
159
# File 'lib/active_record/attribute_methods/dirty.rb', line 157

def saved_change_to_attribute(attr_name)
  mutations_before_last_save.change_to_attribute(attr_name)
end

#saved_change_to_attribute?(attr_name, **options) ⇒ Boolean

Did this attribute change when we last saved? This method can be invoked as `saved_change_to_name?` instead of `saved_change_to_attribute?(“name”)`. Behaves similarly to attribute_changed?. This method is useful in after callbacks to determine if the call to save changed a certain attribute.

Options

from When passed, this method will return false unless the original value is equal to the given option

to When passed, this method will return false unless the value was changed to the given value

Returns:

  • (Boolean)

144
145
146
# File 'lib/active_record/attribute_methods/dirty.rb', line 144

def saved_change_to_attribute?(attr_name, **options)
  mutations_before_last_save.changed?(attr_name, **options)
end

#saved_changesObject

Returns a hash containing all the changes that were just saved.


175
176
177
# File 'lib/active_record/attribute_methods/dirty.rb', line 175

def saved_changes
  mutations_before_last_save.changes
end

#saved_changes?Boolean

Did the last call to `save` have any changes to change?

Returns:

  • (Boolean)

170
171
172
# File 'lib/active_record/attribute_methods/dirty.rb', line 170

def saved_changes?
  mutations_before_last_save.any_changes?
end

#will_save_change_to_attribute?(attr_name, **options) ⇒ Boolean

Alias for `attribute_changed?`

Returns:

  • (Boolean)

180
181
182
# File 'lib/active_record/attribute_methods/dirty.rb', line 180

def will_save_change_to_attribute?(attr_name, **options)
  mutations_from_database.changed?(attr_name, **options)
end