From 633100b9e5ab0ca634c86eb56bc2da4f3b33e8c4 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Thu, 14 Nov 2013 15:02:05 -0500 Subject: Addendum to #12891 * Fix incorrectly named tests * Restore Object#to_param behavior * Ensure param is derived from a squished and truncated string --- activerecord/lib/active_record/integration.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb index f881fa74b2..951db5b756 100644 --- a/activerecord/lib/active_record/integration.rb +++ b/activerecord/lib/active_record/integration.rb @@ -86,12 +86,16 @@ module ActiveRecord # # params[:id] # => "123-fancy-pants" # User.find(params[:id]).id # => 123 - 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}" - else - default + def to_param(method_name = nil) + if method_name.nil? + super() + else + define_method :to_param do + if (default = super()) && (result = send(method_name).to_s).present? + "#{default}-#{result.squish.truncate(20, separator: /\s/, omission: nil).parameterize}" + else + default + end end end end -- cgit v1.2.3