Module: ActiveRecord::AttributeMethods::Dirty

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

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



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

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

#attribute_changeObject



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

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`



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

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

#attribute_changed?Boolean

Returns:

  • (Boolean)


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

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

#attribute_changed_in_place?(attr_name) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


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

def attribute_changed_in_place?(attr_name) # :nodoc:
  mutation_tracker.changed_in_place?(attr_name)
end

#attribute_in_database(attr_name) ⇒ Object

Alias for ‘attribute_was`



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

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

#attribute_wasObject



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

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

#attributes_in_databaseObject

Alias for ‘changed_attributes`



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

def attributes_in_database
  mutations_from_database.changed_values
end

#changedObject



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

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

#changed?Boolean

Returns:

  • (Boolean)


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

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

#changed_attribute_names_to_saveObject

Alias for ‘changed`



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

def changed_attribute_names_to_save
  mutations_from_database.changed_attribute_names
end

#changed_attributesObject

:nodoc:



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

def changed_attributes # :nodoc:
  # 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

:nodoc:



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

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

#changes_appliedObject

:nodoc:



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

def changes_applied # :nodoc:
  @previous_mutation_tracker = mutation_tracker
  @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
  @mutation_tracker = nil
  @mutations_from_database = nil
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 = mutations_from_database
  forget_attribute_assignments
  @mutations_from_database = AttributeMutationTracker.new(@attributes)
end

#changes_to_saveObject

Alias for ‘changes`



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

def changes_to_save
  mutations_from_database.changes
end

#clear_attribute_changes(attr_names) ⇒ Object

:nodoc:



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

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

#clear_changes_informationObject

:nodoc:



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

def clear_changes_information # :nodoc:
  @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)


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

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

:nodoc:



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

def previous_changes # :nodoc:
  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

:nodoc:



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

def raw_write_attribute(attr_name, *) # :nodoc:
  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”)`



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

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)


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

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.



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

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)


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

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)


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

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