Skip to content
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

Merged
merged 1 commit into from
Apr 12, 2016
Merged

prevent 'attribute_changed?' from returning nil #24515

merged 1 commit into from
Apr 12, 2016

Conversation

Sen-Zhang
Copy link
Contributor

a simple attempt fix for #24220

@rails-bot
Copy link

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
Copy link
Member

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.

Copy link
Contributor Author

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).

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

@jaredbeck
Copy link
Contributor

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. :)

@rafaelfranca
Copy link
Member

But in Rails 4.2 it never returned nil, which version were your application using that relied on this behavior?

@jaredbeck
Copy link
Contributor

jaredbeck commented Jul 20, 2016

But in Rails 4.2 it never returned nil, which version were your application using that relied on this behavior?

I'm upgrading from 4.2.7 to 5.0, so that's weird. I have a test like this:

      it "returns nil" do
        expect(RedactedModel.new.redacted_attribute_changed?).to eq(nil)
      end

Passes in 4.2.7, fails in 5.0.0. But, maybe there's something else going on.

@rafaelfranca
Copy link
Member

Oh, it may be a regression in 4.2.7. Can you create a reproduction script and report the bug?

@jaredbeck
Copy link
Contributor

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. :)

@jaredbeck
Copy link
Contributor

Rafael, Here's a script demonstrating that 4.2.7 returns nil.

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?

jaredbeck added a commit to jaredbeck/rails that referenced this pull request Jul 20, 2016
jaredbeck added a commit to jaredbeck/rails that referenced this pull request Jul 20, 2016
jaredbeck added a commit to jaredbeck/rails that referenced this pull request Jul 20, 2016
jaredbeck added a commit to jaredbeck/rails that referenced this pull request Jul 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants