Skip to content

Commit

Permalink
validate uniqueness with limit in utf8
Browse files Browse the repository at this point in the history
[#2653 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
Elise Huard authored and jeremy committed Aug 9, 2009
1 parent 87e2c18 commit c5896bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/validations/uniqueness.rb
Expand Up @@ -119,7 +119,7 @@ def validates_uniqueness_of(*attr_names)
comparison_operator = "IS ?"
elsif column.text?
comparison_operator = "#{connection.case_sensitive_equality_operator} ?"
value = column.limit ? value.to_s[0, column.limit] : value.to_s
value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s
else
comparison_operator = "= ?"
end
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/validations/uniqueness_validation_test.rb
Expand Up @@ -238,6 +238,16 @@ def test_validate_uniqueness_with_limit
assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique"
end

def test_validate_uniqueness_with_limit_and_utf8
with_kcode('UTF8') do
# Event.title is limited to 5 characters
e1 = Event.create(:title => "一二三四五")
assert e1.valid?, "Could not create an event with a unique, 5 character title"
e2 = Event.create(:title => "一二三四五六七八")
assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique"
end
end

def test_validate_straight_inheritance_uniqueness
w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork")
assert w1.valid?, "Saving w1"
Expand Down

0 comments on commit c5896bf

Please sign in to comment.