Permalink
Browse files

support for multi delimiters

  • Loading branch information...
1 parent 4690e04 commit 535dac32461600995831afe7f8297cebdbccc7e8 @halida halida committed May 2, 2012
Showing with 46 additions and 7 deletions.
  1. +1 −0 .gitignore
  2. +2 −1 lib/acts-as-taggable-on.rb
  3. +9 −5 lib/acts_as_taggable_on/tag_list.rb
  4. +34 −1 spec/acts_as_taggable_on/tag_list_spec.rb
View
@@ -8,3 +8,4 @@ spec/database.yml
tmp*.sw?
*.sw?
tmp
+*.gem
@@ -20,7 +20,8 @@ module ActsAsTaggableOn
self.remove_unused_tags = false
def self.glue
- @@delimiter.ends_with?(" ") ? @@delimiter : "#{@@delimiter} "
+ delimiter = @@delimiter.kind_of?(Array) ? @@delimiter[0] : @@delimiter
+ delimiter.ends_with?(" ") ? delimiter : "#{delimiter} "
end
def self.setup
@@ -21,10 +21,12 @@ def self.from(string)
string = string.to_s.dup
# Parse the quoted tags
- string.gsub!(/(\A|#{ActsAsTaggableOn.delimiter})\s*"(.*?)"\s*(#{ActsAsTaggableOn.delimiter}\s*|\z)/) { tag_list << $2; $3 }
- string.gsub!(/(\A|#{ActsAsTaggableOn.delimiter})\s*'(.*?)'\s*(#{ActsAsTaggableOn.delimiter}\s*|\z)/) { tag_list << $2; $3 }
+ d = ActsAsTaggableOn.delimiter
+ 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
@@ -67,7 +69,9 @@ def to_s
tags.send(:clean!)
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
@@ -94,4 +98,4 @@ def extract_and_apply_options!(args)
args.flatten!
end
end
-end
+end
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
require File.expand_path('../../spec_helper', __FILE__)
describe ActsAsTaggableOn::TagList do
@@ -90,4 +91,36 @@
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.