-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
prevent 'attribute_changed?' from returning nil #24515
prevent 'attribute_changed?' from returning nil #24515
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@@ -180,7 +180,7 @@ def attribute_changed?(attr, options = nil) #:nodoc: | |||
result &&= options[:to] == __send__(attr) if options.key?(:to) | |||
result &&= options[:from] == changed_attributes[attr] if options.key?(:from) | |||
end | |||
result | |||
!!result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double !
is often frowned upon, so let's explain why we're doing it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeremy The change is trying to fix this issue #24220. The issue is caused by the fact that method attribute_changed?
could return nil. I have proposed a more comprehensive fix #24242, but it has been considered going too far because some changes are not desired. I also tried to fix it by using result.present?
, but this solution is also not preferable(@pixeltrix left some comments here for that #24242).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git blame
gives more than enough answer for why we're doing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I did just notice the commit message is pretty barren. @Sen-Zhang can you try to provide more context in your commit messages in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sgrif Sorry about that. I will provide more context for sure.
Can we add a line to the 5.0 changelog about this? This change (which I like) broke (a trivial thing in) my app and it was really hard to trace it back to this change. I think people should know this was an intentional change in 5.0 so they don't file a bug report. :) |
But in Rails 4.2 it never returned |
I'm upgrading from 4.2.7 to 5.0, so that's weird. I have a test like this:
Passes in 4.2.7, fails in 5.0.0. But, maybe there's something else going on. |
Oh, it may be a regression in 4.2.7. Can you create a reproduction script and report the bug? |
Well, I like this change though. I wish more functions returned actual booleans :) I'd just like to see this change acknowledged in a changelog, if we can figure out which changelog it belongs in. :) |
Rafael, Here's a script demonstrating that 4.2.7 returns begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '4.2.7'
gem 'sqlite3'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
end
end
class Post < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_one
assert_equal nil, Post.new.title_changed?
end
end So, this is a breaking change in 5.0.0 and it would be great if we could add it to the 5.0 changelog. Do you want a PR updating the changelog? |
See discussion in rails#24515
See discussion in rails#24515
See discussion in rails#24515
See discussion in rails#24515
a simple attempt fix for #24220