-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add AR::Base.to_param for convenient "pretty" URLs derived from a model's attribute or method #12891
Conversation
…el's attribute or method.
Thank you but we can't change the Rails default and this is already possible to do in your application. We don't need this on Rails |
Add AR::Base.to_param for convenient "pretty" URLs derived from a model's attribute or method
Now I got what the PR is doing. 😰 |
It's all opt-in, all good :) |
def to_param(method_name) | ||
define_method :to_param do | ||
if (default = super()) && (result = send(method_name).to_s).present? | ||
"#{default}-#{result.truncate(20, separator: /\s/, omission: nil).parameterize}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need tests to this truncate logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Fix incorrectly named tests * Restore Object#to_param behavior * Ensure param is derived from a squished and truncated string
Hmm, a bit astonished to find this merged to master "as is". This implementation ignores all prior art on the subject, for example, FriendlyId. The code seems pretty expensive to generate, say, hundreds of resource urls for an index page. What's the need for uncached interpolation, truncation, parameterization, etc.? Yes, it's optional but, it's being offered as a convenience for people to use. |
FriendlyId is great but far too complex for something default to fit in Rails. This is a great simple solution that'll be a nice upgrade for the 80% case. For when you want to go for the last 20%, external gems can fill the gap. If you have some performance benchmarks showing this approach is an issue, do post along with a patch to speed it up. But the core functionality is solid as is.
|
The one thing that feels wrong to me in this addition is the hard class User < ActiveRecord::Base
to_param :name, max_length: 40
end Using |
Yes sounds important the possibility to use an option |
Please refer this #12967. |
Example: