Skip to content

Commit

Permalink
support for multi delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
halida committed May 2, 2012
1 parent 4690e04 commit 535dac3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@ spec/database.yml
tmp*.sw? tmp*.sw?
*.sw? *.sw?
tmp tmp
*.gem
3 changes: 2 additions & 1 deletion lib/acts-as-taggable-on.rb
Expand Up @@ -20,7 +20,8 @@ module ActsAsTaggableOn
self.remove_unused_tags = false self.remove_unused_tags = false


def self.glue def self.glue
@@delimiter.ends_with?(" ") ? @@delimiter : "#{@@delimiter} " delimiter = @@delimiter.kind_of?(Array) ? @@delimiter[0] : @@delimiter
delimiter.ends_with?(" ") ? delimiter : "#{delimiter} "
end end


def self.setup def self.setup
Expand Down
14 changes: 9 additions & 5 deletions lib/acts_as_taggable_on/tag_list.rb
Expand Up @@ -21,10 +21,12 @@ def self.from(string)
string = string.to_s.dup string = string.to_s.dup


# Parse the quoted tags # Parse the quoted tags
string.gsub!(/(\A|#{ActsAsTaggableOn.delimiter})\s*"(.*?)"\s*(#{ActsAsTaggableOn.delimiter}\s*|\z)/) { tag_list << $2; $3 } d = ActsAsTaggableOn.delimiter
string.gsub!(/(\A|#{ActsAsTaggableOn.delimiter})\s*'(.*?)'\s*(#{ActsAsTaggableOn.delimiter}\s*|\z)/) { tag_list << $2; $3 } d = d.join("|") if d.kind_of?(Array)
string.gsub!(/(\A|#{d})\s*"(.*?)"\s*(#{d}\s*|\z)/) { tag_list << $2; $3 }
string.gsub!(/(\A|#{d})\s*'(.*?)'\s*(#{d}\s*|\z)/) { tag_list << $2; $3 }


tag_list.add(string.split(ActsAsTaggableOn.delimiter)) tag_list.add(string.split(Regexp.new d))
end end
end end


Expand Down Expand Up @@ -67,7 +69,9 @@ def to_s
tags.send(:clean!) tags.send(:clean!)


tags.map do |name| tags.map do |name|
name.include?(ActsAsTaggableOn.delimiter) ? "\"#{name}\"" : name d = ActsAsTaggableOn.delimiter
d = Regexp.new d.join("|") if d.kind_of? Array
name.index(d) ? "\"#{name}\"" : name
end.join(ActsAsTaggableOn.glue) end.join(ActsAsTaggableOn.glue)
end end


Expand All @@ -94,4 +98,4 @@ def extract_and_apply_options!(args)
args.flatten! args.flatten!
end end
end end
end end
35 changes: 34 additions & 1 deletion spec/acts_as_taggable_on/tag_list_spec.rb
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
require File.expand_path('../../spec_helper', __FILE__) require File.expand_path('../../spec_helper', __FILE__)


describe ActsAsTaggableOn::TagList do describe ActsAsTaggableOn::TagList do
Expand Down Expand Up @@ -90,4 +91,36 @@
end end


end end
end
describe "Multiple Delimiter" do
before do
@old_delimiter = ActsAsTaggableOn.delimiter
end

after do
ActsAsTaggableOn.delimiter = @old_delimiter
end

it "should separate tags by delimiters" do
ActsAsTaggableOn.delimiter = [',', ' ', '\|']
tag_list = ActsAsTaggableOn::TagList.from "cool, data|I have"
tag_list.to_s.should == 'cool, data, I, have'
end

it "should escape quote" do
ActsAsTaggableOn.delimiter = [',', ' ', '\|']
tag_list = ActsAsTaggableOn::TagList.from "'I have'|cool, data"
tag_list.to_s.should == '"I have", cool, data'

tag_list = ActsAsTaggableOn::TagList.from '"I, have"|cool, data'
tag_list.to_s.should == '"I, have", cool, data'
end

it "should work for utf8 delimiter and long delimiter" do
ActsAsTaggableOn.delimiter = [',', '的', '可能是']
tag_list = ActsAsTaggableOn::TagList.from "我的东西可能是不见了,还好有备份"
tag_list.to_s.should == "我, 东西, 不见了, 还好有备份"
end
end

end

0 comments on commit 535dac3

Please sign in to comment.