New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update dirty before callbacks #667
Conversation
* ActiveRecord updates its changes/previous_changes before after_create/after_update callbacks * However, MongoMapper does that after the callbacks, therefore the changes/previous_changes values don't match with equivalent code in ActiveRecord.
Need to review / think more about the implementation but like the idea overall. This is great! I think this will have to be a major version release since it's a major change in how the software works. So just heads up it might be a little bit before this is released. FYI, this is how AR used to work so MM was just copying ActiveRecord from back then. But agreed it should work the same way to abide by the principle of least surprise. |
For my own reference (as I'm in the middle of upgrading a rails app) - I ran into this: and the error that rails 5.1 generates:
|
Hmm, I believe this is not a breaking change but rather a bugfix, since the behaviour with
^ See these examples; I would consider Sorry, the original issue description I posted has been kinda misleading. I updated the comparison codes in the description now too. |
* Also cover the case when some callbacks didn't get called
I added ad9e819 to cover the While adding |
Please let me know if I need further descriptions or changes for this; I'm more than happy to work on them. |
Thanks @ujihisa. Yeah, I need to look at the pending patches! Will do soon. |
JFYI, this PR will fix #673. |
@ujihisa So FYI I do want to get this in, but first would like to release a point version with pending changes. Then I'm also reworking a lot of the proxy code. It's sort of a cleanup project but I'd like to get that in before taking this on. This would probably be a minor release version alone. But just a heads up in still on my radar - just waiting a bit to merge it. Thanks for your patience. |
Thanks for the update! Yes, it makes sense to release a minor version first with the changes already in the master branch before this. However, I still believe this fix should be included not too late, even before the rework, as I mentioned at #667 (comment). It's attracting to include proxy code reworks first to clean things up without changing behaviour, but it will also need to take time finding out new bugs before release. |
I think this change should be shipped as bugfix because the behavior for
coderequire 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'pry-byebug'
gem 'activemodel', '5.0.7.2'
case ENV['MM_VERSION']
when '0.15', 'pr-667'
gem 'mongo_mapper', '0.15.1'
when '0.14'
gem 'mongo_mapper', '0.14.0'
gem 'bson', '1.9.2'
else
raise 'MM_VERSION must be 0.14, 0.15 or pr-667'
end
gem 'bson_ext'
gem 'activemodel-serializers-xml'
end
MongoMapper.setup({'development' => {'uri' => 'mongodb://mongo/test'}}, 'development')
if ENV['MM_VERSION'] == 'pr-667'
module MongoMapper
module Plugins
module Dirty
def create_or_update(*)
super
end
def save_to_collection(*)
super.tap do
changes_applied
end
end
end
end
end
end
class Doc
include MongoMapper::Document
key :x, String
after_save do
puts "after_save"
p changes
p previous_changes
end
end
if ENV['MM_VERSION'] == 'pr-667'
puts "MongoMapper::Version #{MongoMapper::Version} (with pr-667 patch)"
else
puts "MongoMapper::Version #{MongoMapper::Version}"
end
d = Doc.create(x: 'hello')
d.x = 'world'
d.save! |
Ah! Didn't understand all the context. Merging now + will release a new point version. |
Thank you! 👍🎉👍 |
after_create/after_update callbacks
changes/previous_changes values don't match with equivalent code in
ActiveRecord.
Comparisons
ActiveRecord:
MongoMapper:
MongoMapper with this changeset:
The only difference between the new behaviour and ActiveRecord is to show
id
inprevious_changes
or not. Since the behaviour for IDs are already different between MongoMapper and ActiveRecord, I kept it as is, but if it makes more sense I'm also happy to work on adding_id
into theprevious_changes
.