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

Fix to_param in ActiveRecord callbacks #867

Merged
merged 2 commits into from
May 20, 2018
Merged

Fix to_param in ActiveRecord callbacks #867

merged 2 commits into from
May 20, 2018

Conversation

lazyatom
Copy link
Contributor

(... and also remove deprecation warnings under Rails 5.1)

I originally started investigating this as part of #816 (see some discussion there about to_param from the perspective of the deprecation warnings), and also #862 (which features the first version of the callback test I've used here).

On the latter, it became clear that there were actually two issues: the deprecation warnings, and the behaviour of to_param in callbacks. I decided to write an explicit test for the callback behaviour here, and included a bunch of other tests about to_param in various scenarios.

The "fix" basically involves deleting all the dirty tracking code within to_param, which seems superficially crazy to me, but all the tests pass, so either we don't need that code, or we are missing some key tests around this behaviour.

lazyatom added 2 commits May 17, 2018 14:27
These tests were prompted by the presence of lots of deprecation
warnings under Rails 5.1, due to the calls to the `attribute_changed?`
method within `to_param`, whose behaviour was expected to change in
Rails 5.2. I believe the fix might involve a significant change to
`to_param`, so I've also add other tests to comprehensively describe
the behaviour of `to_param` in various scenarios where the record
succeeded or failed to save.

I believe the new callback test also demonstrates a bug in all
versions of FriendlyId up to Rails 5.2; the value of the slug used in
the callback is the old value. This is a problem if we use that slug
to generate a URL as part of behaviour called within that callback.
This ensures that we use the new value of a slug when calling
`to_param` within an ActiveRecord callback. It also seems to pass all
the other regression tests I've added, thus keeping the fixes working
for #259 and #148, and as a bonus, prevents getting a bunch of
deprecation warnings in Rails 5.1.
@parndt
Copy link
Collaborator

parndt commented May 20, 2018

Fantastic! 👍 I ran this against the test suite of https://github.com/refinery/refinerycms as this should have had issues if your code changes something meaningful, and it works fine.

@parndt parndt merged commit 9b372d7 into norman:master May 20, 2018
@jeremywadsack
Copy link

Is it possible we could get a new release with this issue fixed? It was merged seven months ago.

@parndt
Copy link
Collaborator

parndt commented Dec 30, 2018

Pushing gem to https://rubygems.org...
Successfully registered gem: friendly_id (5.2.5)

Thanks for the push @jeremywadsack @lazyatom !

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.

3 participants