Permalink
Browse files

Merge pull request #13386 from rono23/fix-to-param

Fix to_param when attribute has multibyte character
  • Loading branch information...
2 parents 6d894cf + ee738f9 commit 4779aaa1d191b0306eaf25658b4c6a611dd9a5c2 @rafaelfranca rafaelfranca committed Dec 18, 2013
Showing with 10 additions and 2 deletions.
  1. +4 −2 activerecord/lib/active_record/integration.rb
  2. +6 −0 activerecord/test/cases/integration_test.rb
@@ -98,8 +98,10 @@ def to_param(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}"
+ if (default = super()) &&
+ (result = send(method_name).to_s).present? &&
+ (param = result.squish.truncate(20, separator: /\s/, omission: nil).parameterize).present?
+ "#{default}-#{param}"
else
default
end
@@ -46,6 +46,12 @@ def test_to_param_class_method_squishes
assert_equal '4-ab-ab-ab-ab-ab-ab', firm.to_param
end
+ def test_to_param_class_method_multibyte_character
+ firm = Firm.find(4)
+ firm.name = "戦場ヶ原 ひたぎ"
+ assert_equal '4', firm.to_param
+ end
+
def test_to_param_class_method_uses_default_if_blank
firm = Firm.find(4)
firm.name = nil

0 comments on commit 4779aaa

Please sign in to comment.