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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation introduced in ActiveRecord 5.1.0.beta1. Closes #257 #259

Merged
merged 2 commits into from Feb 27, 2017

Conversation

CvX
Copy link
Contributor

@CvX CvX commented Feb 24, 2017

Added ruby 2.4 and Rails 5.1.0.beta1 to test matrix. All tests are passing. Also, the change was tested in a live application.

Thanks to @brendon for guidance! 馃槂

Copy link
Collaborator

@swanandp swanandp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes, but this looks good. Thanks for contributing!

@@ -393,7 +393,13 @@ def insert_at_position(position)
end

def update_positions
old_position = send("#{position_column}_was") || bottom_position_in_list + 1
if ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be encapsulated in a method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ActiveRecord version check? Something along those lines?

def activerecord_version_at_least?(major, minor = nil)
  ActiveRecord::VERSION::MAJOR >= major && (!minor || ActiveRecord::VERSION::MINOR >= minor)
end

# activerecord_version_at_least?(5, 1)

Or is that way of specifying the version a bit awkward and it would be better to go with version strings?

def activerecord_version_at_least?(required)
  Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new(required)
end

# activerecord_version_at_least?('5.1')

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, let's get rid of checking the version altogether, and rely on respond_to? for this. Why check version, When we can duck type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do either way, but @brendon made a (valid) suggestion that version comparison makes it easier to remove old code path in the future.

That's what's currently done in:
https://github.com/swanandp/acts_as_list/blob/f9b40e1cbdefe5244b787c66da74148e351af26c/lib/acts_as_list/active_record/acts/sequential_updates_method_definer.rb#L8
(which could be replaced w/ eventual activerecord_version_at_least?('5')).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Let's just leave it at a direct comparison for now, and we can come back to it later. So no change needed here, just the other minor one and this is good to go.

if ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1
old_position = send("#{position_column}_before_last_save")
else
old_position = send("#{position_column}_was")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old_position = can be outside the if clause for better readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted that block to a method. It's clean and leaves original method basically intact.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this looks good.

`attribute_was` replaced by `attribute_before_last_save`
@brendon
Copy link
Owner

brendon commented Feb 27, 2017

Looks good @CvX :) @swanandp, are you happy for me to merge this? (or you can if you want to) :)

@swanandp swanandp merged commit 177740f into brendon:master Feb 27, 2017
@swanandp
Copy link
Collaborator

Merged. Thanks @CvX !

@CvX
Copy link
Contributor Author

CvX commented Mar 20, 2017

Hi, have to dig up this PR because I've noticed that the code I committed and which ended up in the repository is incorrect.
Version conditional used here: https://github.com/swanandp/acts_as_list/pull/259/files#diff-708784f0c89e5cdbdd54e18d2b2243d1R406 is wrong for future AR versions: 6.0, 7.0, etc.

We can either correct the conditional (ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)) or we could use Gem::Version as mentioned before (like this CvX@0e7f24f)

Please let me know which way do you prefer and I'll submit a PR.
Sorry for the inconvenience.

@brendon
Copy link
Owner

brendon commented Mar 20, 2017

Thanks @CvX, I've pushed a fix for that to master :) Thanks for picking it up! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants