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

Addon for numerically sequential slugs #644

Merged
merged 2 commits into from
May 24, 2015
Merged

Conversation

tekin
Copy link
Contributor

@tekin tekin commented Feb 9, 2015

This provides an addon that enables sequential slugs for conflict resolution, mimicking the behaviour of friendly_id v4.

Well formed candidates combined with UUIDs as a fallback will almost always be the better solution for resolving slug conflicts. That said, there are circumstances when this is either not possible or undesirable. For example, either good candidates cannot be formed, or the existing behaviour needs to be maintained for consistency.

I have recently had to deal with this situation whilst upgrading a large Rails 3 application to Rails 4. In this case it's not possible to form appropriate candidates for many of the sluggable models, and UUIDs would make new URLs inconsistent and "ugly". Ultimately it's both cheaper and far easier for the business (in this case the UK government) to maintain the existing behaviour than it would be to retrospectively migrate to the candidate/UUID-based approach.

I have intentionally left out any documentation pending feedback. I'm assuming that if this is to make it into friendly_id, it would have to be made clear that it's meant as a last resort for those unable to use the preferred candidate/UUID-based approach.

/cc @parndt

@norman
Copy link
Owner

norman commented Feb 9, 2015

I'm on vacation without my computer so I can't give good feedback on this for at least a week, but I'm in favor of this feature and grateful that you worked on it. When we had this feature by default, I remember there bein so fairly complex code to pull the sequence off the end of the slug in order to increment it, and to extract the base for SQL queries. You should make sure to look through the git and issue history for this feature in version 4.x so we don't end up repeating the cycle of fixing bugs that were solved years before.

Your suggestions for documentation sound perfect. Thanks again for taking this on!

@parndt
Copy link
Collaborator

parndt commented Feb 9, 2015

@tekin what a legend

# Return the unnumbered (shortest) slug first, followed by the numbered ones
# in ascending order.
def ordering_query
"LENGTH(#{slug_column}) ASC, #{slug_column} ASC"
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this work in sqlite3, postgresl, mysql etc?

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've only tested it with MySQL myself, but according to the Travis build it does: https://travis-ci.org/norman/friendly_id/builds/50012697

@tekin
Copy link
Contributor Author

tekin commented Feb 10, 2015

Cheers guys. The code is mostly based on the previous slug generator code from v4, but there are a few bits I didn't port across because mainly because I couldn't get my head around the failing case they were trying to solve. I should have some time later in the week to go back through it and see if there are any edge cases that I've failed to cover.

@gaganawhad
Copy link
Contributor

Thanks for your work @tekin

@tekin tekin force-pushed the sequential-slugs branch 2 times, most recently from f39685f to 28327c5 Compare February 11, 2015 05:35
This addon provides the pre-5.0 behaviour that generated numerically-sequenced
slugs to resolve conflict resolution.
@tekin
Copy link
Contributor Author

tekin commented Feb 11, 2015

I've increased the test coverage and made a change to ensure the slug column is correctly escaped in the queries used to generate sequential slugs.

There is a particular bit of code from the previous implementation that I've not ported: https://github.com/norman/friendly_id/pull/244/files - I think I understand the issue this is trying to solve, but I've really struggled to replicate a failing test. This makes me reluctant to simply copy across the code that is supposedly doing the required escaping.

@tekin
Copy link
Contributor Author

tekin commented Mar 20, 2015

Hi @norman anything I can do to help move this forward? I'm not entirely sure what the failing build is about. It appears unrelated.

@norman
Copy link
Owner

norman commented Mar 24, 2015

@tekin sorry for the delay, I'll try to make time for this soon and get it (and the other PRs that are accumulating) merged in.

@mtarnovan
Copy link

@norman Any idea when this will be merged? The build doesn't seem to fail from this PR. Thanks.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 98.99% when pulling d0adf64 on tekin:sequential-slugs into c6ff6b9 on norman:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 98.99% when pulling d0adf64 on tekin:sequential-slugs into c6ff6b9 on norman:master.

Fixes a bug where a slug conflicting with a slug that starts with a number
would get incorrectly sequenced based on the number at the start of the slug,
rather than as a fresh sequence.
@tekin tekin force-pushed the sequential-slugs branch from d0adf64 to 43caf4b Compare May 15, 2015 11:01
@norman
Copy link
Owner

norman commented May 24, 2015

Sorry again for my long absence. I'm working on this today and will get it merged in along with the other open PRs.

@norman norman merged commit 43caf4b into norman:master May 24, 2015
@norman
Copy link
Owner

norman commented May 24, 2015

@tekin I picked through the history of this functionality in version 4.0 and added in support to escape the '%' and '_' characters in LIKE queries, the 'LEN' operator for MSSQL and specifying ESCAPE '\\' for SQLite3 - all issues that came up one time or another in the past. I've now merged your PR into master. Thanks again for your excellent work, and your patience as I've struggled to get the time and focus to give this the attention it deserved.

@tekin
Copy link
Contributor Author

tekin commented May 25, 2015

Thanks @norman, really appreciate your time and effort in creating and maintaining the gem 🍰

@norman
Copy link
Owner

norman commented Jun 1, 2015

This is now out in 5.2.0.beta.1. No timetable for stable release yet, but probably within a couple weeks.

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.

6 participants