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

Case Insensitivity for id, fixes #785 #787

Merged
merged 6 commits into from
Dec 6, 2019

Conversation

ghbooth12
Copy link
Contributor

Hello. This PR is about fixing the first issue of #785 by using id.downcase instead of id in first_by_friendly_id method. In this way if the url is like users/John-Doe or users/JOHN-DOE, it will search by john-doe in database.

Currently, it find_by with id.(case-sensitive)

# finder_methods.rb

def first_by_friendly_id(id)
	# If `id` is "John-Doe" or "JOHN-DOE"
	find_by(friendly_id_config.query_field => id)
	# It raises `ActiveRecord::RecordNotFound`.
end

It find_by with id.downcase.(case-insensitive)

# finder_methods.rb

def first_by_friendly_id(id)
	# If `id` is "John-Doe" or "JOHN-DOE"
	find_by(friendly_id_config.query_field => id.downcase)
	# It finds data by "john-doe".
end

The tests for the change have been added and all have passed.

@coveralls
Copy link

coveralls commented Jan 30, 2017

Coverage Status

Coverage remained the same at 96.333% when pulling 1d7a5d7 on ghbooth12:785-case-insensitivity into dac59ef on norman:master.

4 similar comments
@coveralls
Copy link

Coverage Status

Coverage remained the same at 96.333% when pulling 1d7a5d7 on ghbooth12:785-case-insensitivity into dac59ef on norman:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 96.333% when pulling 1d7a5d7 on ghbooth12:785-case-insensitivity into dac59ef on norman:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 96.333% when pulling 1d7a5d7 on ghbooth12:785-case-insensitivity into dac59ef on norman:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 96.333% when pulling 1d7a5d7 on ghbooth12:785-case-insensitivity into dac59ef on norman:master.

@eric-norcross
Copy link

+1

@ducnguyenhuy-agilityio
Copy link

Could I know this is merged or not yet?

@vfonic
Copy link
Contributor

vfonic commented Nov 20, 2019

I was just wandering around this repo and found this PR, which is identical to the change I have overridden locally. Would be great to see this merged. 👍

@parndt parndt changed the title Case Insensitivity for id, Fix #785 Case Insensitivity for id, fixes #785 Dec 6, 2019
@parndt parndt merged commit f576251 into norman:master Dec 6, 2019
@vfonic
Copy link
Contributor

vfonic commented Dec 7, 2019

I just realized there could be couple of issues with this PR:

What if id is nil? (or doesn't respond to :downcase?

@parndt
Copy link
Collaborator

parndt commented Dec 8, 2019

All the tests passed, so perhaps if we had tests for those cases, we could fix that preemptively. Are you keen @vfonic ?

@coneybeare
Copy link

This was a bad merge. It is a breaking change and has. no opt-in or opt-out behavior. There are many valid reasons for allowing a URL to have case-sensitive slugs, such as short URL services. It should, at the very least, use the normalize_friendly_id method instead of simply downcase

@parndt
Copy link
Collaborator

parndt commented Jul 4, 2020

@coneybeare thanks, I see what you mean. Do you have a patch in mind that would fix this for you, that you could open a pull request for? Or if you just want to share a snippet I can turn it into a PR.

@coneybeare
Copy link

I’m AFK now, but how about adding a method that defaults one way or the other, and can be overridden in the model to change if necessary? That is the pattern used by the normalize process

@parndt
Copy link
Collaborator

parndt commented Jul 4, 2020

yep, that would make sense to me.. something like parse_friendly_id

@vfonic
Copy link
Contributor

vfonic commented Jul 4, 2020

That's a good idea. It could also be a config option: friendly_id [slugged: { case_sensitive: true }] or a global FriendlyId config.

@parndt
Copy link
Collaborator

parndt commented Jul 5, 2020

That's a good idea. It could also be a config option: friendly_id [slugged: { case_sensitive: true }] or a global FriendlyId config.

I like that too!

@marckohlbrugge
Copy link

FWIW, this broke my implementation. I use FriendlyId for usernames, and some usernames are stored with capitalization in the database. For example MarcKohlbrugge. This change makes makes it so that FriendlyId searches for marckohlbrugge instead, which has no matches.

If we want FriendlyId to be truly case insensitive we should also downcase the attribute we're matching on (e.g. LOWER(username))

@parndt
Copy link
Collaborator

parndt commented Aug 16, 2020

Yeah, the comments on this PR didn't result in any patches unfortunately. I'm all for making this work for everyone. For now, you can use 5.3.0 and I still think @vfonic's suggestion is the best one about making this a config option (probably that defaults to false)

@parndt
Copy link
Collaborator

parndt commented Aug 16, 2020

Let's continue discussion in #950 and #951

parndt added a commit that referenced this pull request Aug 16, 2020
parndt added a commit that referenced this pull request Nov 10, 2020
parndt added a commit that referenced this pull request Nov 10, 2020
In #787 we merged a change which found slugs by `id.downcase` by
default, but this has understandably caused issues for other users of
FriendlyId who don't want to do this.

This reverts that change and introduces a new method, 
`parse_friendly_id`, which can be overridden similar to 
`normalize_friendly_id`.  

Instead of being used for slug generation, `parse_friendly_id` is used
for parsing incoming slugs.

Fixes #950
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.

8 participants