-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
15a2a01
1d2082f
0455d4b
7be5492
826a228
f3d7538
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# coding: utf-8 | ||
module ActsAsTaggableOn | ||
class Tag < ::ActiveRecord::Base | ||
include ActsAsTaggableOn::Utils | ||
|
@@ -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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this meant to replace Tag.named_any entirely? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be in Tag.named_any? See above There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed it to be consistent with |
||
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' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plz put back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Sorry.