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 4 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
35 changes: 28 additions & 7 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,26 @@ def self.find_or_create_all_with_like_by_name(*list)

return [] if list.empty?

existing_tags = Tag.named_any(list)

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 }
duplicates = []
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?

result = []
existing_tags = named_any_by_comparable_name(list)
list.each do |tag_name|
existing_tag = existing_tags[comparable_name(tag_name)]
if existing_tag
result << existing_tag
else
begin
result << Tag.create(:name => tag_name)
rescue ActiveRecord::RecordNotUnique
duplicates << tag_name
end
end
end

existing_tag || Tag.create(:name => tag_name)
if duplicates.empty?
result
else
result + Tag.named_any(duplicates)
end
end

Expand All @@ -103,8 +116,16 @@ def comparable_name(str)
as_8bit_ascii(str).downcase
end

# Get the list of tags and index them by the comparable_name for faster lookup
#
# @param *list [Array<Array>] Array of tag lists
# @return [Hash<comparable_name, tag_name>]
def named_any_by_comparable_name(list)
Hash[Tag.named_any(list).group_by{|tag| comparable_name(tag.name)}.map{|k,v| [k,v.first]}]
end

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

def as_8bit_ascii(string)
Expand Down
6 changes: 5 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,14 @@ 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 sha_prefix(string)
Digest::SHA1.hexdigest("#{string}#{rand}")[0..6]
end

private
def like_operator
using_postgresql? ? 'ILIKE' : 'LIKE'
Expand Down
4 changes: 2 additions & 2 deletions spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,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
12 changes: 12 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,16 @@
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
end