Skip to content

Commit

Permalink
Adds a fix and its test to handle special characters combinations and…
Browse files Browse the repository at this point in the history
… MySQL
  • Loading branch information
jerefrer committed Sep 15, 2014
1 parent 368bb8a commit bf1b443
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
23 changes: 12 additions & 11 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,10 @@ def self.named(name)
end

def self.named_any(list)
if ActsAsTaggableOn.strict_case_match
clause = list.map { |tag|
sanitize_sql(["name = #{binary}?", as_8bit_ascii(tag)])
}.join(' OR ')
where(clause)
else
clause = list.map { |tag|
sanitize_sql(['LOWER(name) = LOWER(?)', as_8bit_ascii(unicode_downcase(tag))])
}.join(' OR ')
where(clause)
end
clause = list.map { |tag|
sanitize_sql_for_named_any(tag).force_encoding('BINARY')
}.join(' OR ')
where(clause)
end

def self.named_like(name)
Expand Down Expand Up @@ -135,6 +128,14 @@ def as_8bit_ascii(string)
string.to_s.mb_chars
end
end

def sanitize_sql_for_named_any(tag)
if ActsAsTaggableOn.strict_case_match
sanitize_sql(["name = #{binary}?", as_8bit_ascii(tag)])
else
sanitize_sql(['LOWER(name) = LOWER(?)', as_8bit_ascii(unicode_downcase(tag))])
end
end
end
end
end
16 changes: 15 additions & 1 deletion spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
end
end

describe 'named any' do
context 'with some special characters combinations', if: using_mysql? do
it 'should not raise an invalid encoding exception' do
expect{ActsAsTaggableOn::Tag.named_any(["holä", "hol'ä"])}.not_to raise_error
end
end
end

describe 'find or create by name' do
before(:each) do
@tag.name = 'awesome'
Expand Down Expand Up @@ -250,13 +258,19 @@
end
end

it 'should not change enconding' do
it 'should not change encoding' do
name = "\u3042"
original_encoding = name.encoding
record = ActsAsTaggableOn::Tag.find_or_create_with_like_by_name(name)
record.reload
expect(record.name.encoding).to eq(original_encoding)
end

context 'named any with some special characters combinations', if: using_mysql? do
it 'should not raise an invalid encoding exception' do
expect{ActsAsTaggableOn::Tag.named_any(["holä", "hol'ä"])}.not_to raise_error
end
end
end

describe 'name uniqeness validation' do
Expand Down

0 comments on commit bf1b443

Please sign in to comment.