aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb12
-rw-r--r--activerecord/lib/active_record/attribute_mutation_tracker.rb29
2 files changed, 39 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index a439683185..24f0ccb239 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -51,12 +51,12 @@ module ActiveRecord
end
def changes_applied
- super
+ @previous_mutation_tracker = @mutation_tracker
store_original_attributes
end
def clear_changes_information
- super
+ @previous_mutation_tracker = nil
store_original_attributes
end
@@ -89,6 +89,10 @@ module ActiveRecord
end
end
+ def previous_changes
+ previous_mutation_tracker.changes
+ end
+
def attribute_changed_in_place?(attr_name)
@mutation_tracker.changed_in_place?(attr_name)
end
@@ -119,6 +123,10 @@ module ActiveRecord
@mutation_tracker = @mutation_tracker.now_tracking(@attributes)
end
+ def previous_mutation_tracker
+ @previous_mutation_tracker ||= NullMutationTracker.new
+ end
+
def cache_changed_attributes
@cached_changed_attributes = changed_attributes
yield
diff --git a/activerecord/lib/active_record/attribute_mutation_tracker.rb b/activerecord/lib/active_record/attribute_mutation_tracker.rb
index e4be5c5524..168794fcb4 100644
--- a/activerecord/lib/active_record/attribute_mutation_tracker.rb
+++ b/activerecord/lib/active_record/attribute_mutation_tracker.rb
@@ -13,6 +13,14 @@ module ActiveRecord
end
end
+ def changes
+ attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
+ if changed?(attr_name)
+ result[attr_name] = [original_attributes.fetch_value(attr_name), attributes.fetch_value(attr_name)]
+ end
+ end
+ end
+
def changed?(attr_name)
attr_name = attr_name.to_s
modified?(attr_name) || changed_in_place?(attr_name)
@@ -52,4 +60,25 @@ module ActiveRecord
end
end
end
+
+ class NullMutationTracker
+ def changed_values
+ {}
+ end
+
+ def changes
+ {}
+ end
+
+ def changed?(*)
+ false
+ end
+
+ def changed_in_place?(*)
+ false
+ end
+
+ def forget_change(*)
+ end
+ end
end