Class: ActsAsTaggableOn::TagList
- Inherits:
-
Array
- Object
- Array
- ActsAsTaggableOn::TagList
- Defined in:
- lib/acts_as_taggable_on/tag_list.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
Returns the value of attribute owner.
Class Method Summary collapse
-
.from(tag_ids) ⇒ Object
Returns a new TagList using the given tag string.
Instance Method Summary collapse
-
#add(*names) ⇒ Object
Add tags to the tag_list.
-
#initialize(*args) ⇒ TagList
constructor
A new instance of TagList.
-
#remove(*names) ⇒ Object
Remove specific tags from the tag_list.
-
#to_s ⇒ Object
Transform the tag_list into a tag string suitable for edting in a form.
Constructor Details
#initialize(*args) ⇒ TagList
Returns a new instance of TagList.
7 8 9 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 7 def initialize(*args) add(*args) end |
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
5 6 7 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 5 def owner @owner end |
Class Method Details
.from(tag_ids) ⇒ Object
Returns a new TagList using the given tag string.
Example:
tag_list = TagList.from("One , Two, Three")
tag_list # ["One", "Two", "Three"]
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 17 def self.from(tag_ids) string = ActsAsTaggableOn::Tag.find(tag_ids) string = string.respond_to?(:map) ? string.map(&:name) : string.name string = string.join(ActsAsTaggableOn.glue) if string.respond_to?(:join) new.tap do |tag_list| 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 } tag_list.add(string.split(ActsAsTaggableOn.delimiter)) end end |
Instance Method Details
#add(*names) ⇒ Object
Add tags to the tag_list. Duplicate or blank tags will be ignored. Use the :parse
option to add an unparsed tag string.
Example:
tag_list.add("Fun", "Happy")
tag_list.add("Fun, Happy", :parse => true)
41 42 43 44 45 46 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 41 def add(*names) (names) concat(names) clean! self end |
#remove(*names) ⇒ Object
Remove specific tags from the tag_list. Use the :parse
option to add an unparsed tag string.
Example:
tag_list.remove("Sad", "Lonely")
tag_list.remove("Sad, Lonely", :parse => true)
55 56 57 58 59 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 55 def remove(*names) (names) delete_if { |name| names.include?(name) } self end |
#to_s ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 68 def to_s = frozen? ? self.dup : self .send(:clean!) .map do |name| name.include?(ActsAsTaggableOn.delimiter) ? "\"#{name}\"" : name end.join(ActsAsTaggableOn.glue) end |