aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/attribute_methods
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-06-09 10:07:12 -0400
committerSean Griffin <sean@seantheprogrammer.com>2016-11-01 13:16:33 -0400
commit16ae3db5a5c6a08383b974ae6c96faac5b4a3c81 (patch)
treecd6c4c97c46484f84f5f1b339668a7c952bb4532 /activerecord/test/cases/attribute_methods
parent6b5ad4979ed2ba268755c74c804a2e22eee1d5b8 (diff)
downloadrails-16ae3db5a5c6a08383b974ae6c96faac5b4a3c81.tar.gz
rails-16ae3db5a5c6a08383b974ae6c96faac5b4a3c81.tar.bz2
rails-16ae3db5a5c6a08383b974ae6c96faac5b4a3c81.zip
Deprecate the behavior of AR::Dirty inside of after_(create|update|save) callbacks
We pretty frequently get bug reports that "dirty is broken inside of after callbacks". Intuitively they are correct. You'd expect `Model.after_save { puts changed? }; model.save` to do the same thing as `model.save; puts model.changed?`, but it does not. However, changing this goes much farther than just making the behavior more intuitive. There are a _ton_ of places inside of AR that can be drastically simplified with this change. Specifically, autosave associations, timestamps, touch, counter cache, and just about anything else in AR that works with callbacks have code to try to avoid "double save" bugs which we will be able to flat out remove with this change. We introduce two new sets of methods, both with names that are meant to be more explicit than dirty. The first set maintains the old behavior, and their names are meant to center that they are about changes that occurred during the save that just happened. They are equivalent to `previous_changes` when called outside of after callbacks, or once the deprecation cycle moves. The second set is the new behavior. Their names imply that they are talking about changes from the database representation. The fact that this is what we really care about became clear when looking at `BelongsTo.touch_record` when tests were failing. I'm unsure that this set of methods should be in the public API. Outside of after callbacks, they are equivalent to the existing methods on dirty. Dirty itself is not deprecated, nor are the methods inside of it. They will only emit the warning when called inside of after callbacks. The scope of this breakage is pretty large, but the migration path is simple. Given how much this can improve our codebase, and considering that it makes our API more intuitive, I think it's worth doing.
Diffstat (limited to 'activerecord/test/cases/attribute_methods')
-rw-r--r--activerecord/test/cases/attribute_methods/read_test.rb1
1 files changed, 1 insertions, 0 deletions
diff --git a/activerecord/test/cases/attribute_methods/read_test.rb b/activerecord/test/cases/attribute_methods/read_test.rb
index a8592bd179..978dd93fa4 100644
--- a/activerecord/test/cases/attribute_methods/read_test.rb
+++ b/activerecord/test/cases/attribute_methods/read_test.rb
@@ -14,6 +14,7 @@ module ActiveRecord
def self.decorate_matching_attribute_types(*); end
def self.initialize_generated_modules; end
+ include ActiveRecord::DefineCallbacks
include ActiveRecord::AttributeMethods
def self.attribute_names