Skip to content
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

Possible fix for issue #409 #416

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8
module ActsAsTaggableOn
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plz put back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Sorry.

class Tag < ::ActiveRecord::Base
include ActsAsTaggableOn::Utils
Expand Down Expand Up @@ -72,16 +71,29 @@ def self.find_or_create_all_with_like_by_name(*list)

return [] if list.empty?

existing_tags = Tag.named_any(list)
existing_tags = existing_tags_by_list_name(list)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to replace Tag.named_any entirely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was not the intent. The intent was to push the tag comparison to the database because of collation problems. Tag.named_any works great to retrieve the requested tags.

The problem was comparing the tag strings with strings in ruby. In my case I am using MySQL with utf8_general_ci collation. I had the a tag with the string 'inupiat' and tried to add a tag with the string 'IÑUPIAT'. Tag.named_any('IÑUPIAT') does find the tag with string 'inupiat', but when we compare with comparable_name they are not the same, so an attempt to create a new tag is made. I also have a unique index on tag name, so this threw an error. Since I could not make the comparable_name compare the tags the same way, I pushed that to the database. Sorry for the long winded response. Does that make sense?

list.map do |tag_name|
comparable_tag_name = comparable_name(tag_name)
existing_tag = existing_tags.detect { |tag| comparable_name(tag.name) == comparable_tag_name }
existing_tag = existing_tags[comparable_name(tag_name)]

existing_tag || Tag.create(:name => tag_name)
end
end

def self.existing_tags_by_list_name(*list)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in Tag.named_any? See above

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before merge, it would be good to add comments what the params are and what the return is e.g.

# @param *list [Array<Array>] Array of tag lists
# @return [Hash<comparable_name, tag_name>]

list = [list].flatten

return [] if list.empty?

if !ActsAsTaggableOn.strict_case_match && using_case_insensitive_collation?
names_to_find = sanitize_sql([(["SELECT '%s' AS name"] * list.length).join( " UNION ALL " ), *list])
tags = ActsAsTaggableOn::Tag.select("#{all_columns}, names.name AS list_name").joins("INNER JOIN (#{names_to_find}) AS names ON tags.name = names.name")
else
tags = named_any(list).select("#{all_columns}, #{name_column} AS list_name")
end
Hash[tags.group_by { |tag| comparable_name(tag.list_name) }.map{|k,v| [k,v.first]}]
end

### INSTANCE METHODS:

def ==(object)
Expand All @@ -99,12 +111,20 @@ def count
class << self
private

def all_columns
"#{ActsAsTaggableOn::Tag.table_name}.*"
end

def name_column
"#{ActsAsTaggableOn::Tag.table_name}.name"
end

def comparable_name(str)
as_8bit_ascii(str).downcase
end

def binary
/mysql/ === ActiveRecord::Base.connection_config[:adapter] ? "BINARY " : nil
using_mysql? ? "BINARY " : nil
end

def as_8bit_ascii(string)
Expand Down
10 changes: 9 additions & 1 deletion lib/acts_as_taggable_on/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ def using_sqlite?
::ActiveRecord::Base.connection && ::ActiveRecord::Base.connection.adapter_name == 'SQLite'
end

def using_mysql?
::ActiveRecord::Base.connection && ::ActiveRecord::Base.connection.adapter_name.downcase =~ /mysql/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this to make a db connection, when before it just checked the configured adapter? /mysql/ === ActiveRecord::Base.connection_config[:adapter]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to be consistent with using_postgresql? and using_sqlite?. I did some testing and they all can be implemented to check the value of ActiveRecord::Base.connection_config[:adapter]. Do you think it makes sense to change all of the using_ methods to check the connection_config?

end

def using_case_insensitive_collation?
using_mysql? && ::ActiveRecord::Base.connection.collation =~ /_ci\Z/
end

def sha_prefix(string)
Digest::SHA1.hexdigest("#{string}#{rand}")[0..6]
end

private
def like_operator
using_postgresql? ? 'ILIKE' : 'LIKE'
Expand Down
25 changes: 23 additions & 2 deletions spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@
end
end

if ActsAsTaggableOn::Tag.using_case_insensitive_collation?
describe "find or create with a case insensitive and accent insensitive database" do
before(:each) do
@tag.name = "inupiat"
@tag.save
end

it "should find existing tags case and accent insensitivly" do
ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name("IÑUPIAT").should == [@tag]
end

it "should find existing tags accent insensitivly" do
ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name("iñupiat").should == [@tag]
end

it "should not let tags with single quotes break the query" do
ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name("their's").map(&:name).should == ["their's"]
end
end
end

it "should require a name" do
@tag.valid?

Expand Down Expand Up @@ -192,12 +213,12 @@
duplicate_tag.stub(:validates_name_uniqueness?).and_return(false)
duplicate_tag.save
duplicate_tag.should be_persisted
end
end
end

context "when do need unique names" do
it "should run uniqueness validation" do
duplicate_tag.should_not be_valid
duplicate_tag.should_not be_valid
end

it "add error to name" do
Expand Down
35 changes: 35 additions & 0 deletions spec/acts_as_taggable_on/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,39 @@
TaggableModel.send(:like_operator).should == "LIKE"
end
end

describe "using_mysql?" do
it "should return true when the adapter is mysql" do
TaggableModel.connection.stub(:adapter_name).and_return("MySQL2")
TaggableModel.using_mysql?.should be_true
end

it "should return false when the adapter is not mysql" do
TaggableModel.connection.stub(:adapter_name).and_return("PostgreSQL")
TaggableModel.using_mysql?.should be_false
end
end

describe "using_case_insensitive_collation?" do
context "when the adapter is mysql" do
before do
TaggableModel.connection.stub(:adapter_name).and_return("MySQL2")
end

it "should return true when the collation is case insensitive" do
TaggableModel.connection.stub(:collation).and_return("utf8_unicode_ci")
TaggableModel.using_case_insensitive_collation?.should be_true
end

it "should return false when the adapter is case sensitive" do
TaggableModel.connection.stub(:collation).and_return("utf8_bin")
TaggableModel.using_case_insensitive_collation?.should be_false
end
end

it "should return false when the adapter is not mysql" do
TaggableModel.connection.stub(:adapter_name).and_return("PostgreSQL")
TaggableModel.using_case_insensitive_collation?.should be_false
end
end
end