Class: TagList
- Inherits:
-
Array
- Object
- Array
- TagList
- Defined in:
- lib/acts_as_taggable_on/tag_list.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#taggable ⇒ Object
Returns the value of attribute taggable.
Class Method Summary collapse
-
.from(string) ⇒ Object
Returns a new TagList using the given tag string.
- .from_owner(owner, *tags) ⇒ Object
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.
5 6 7 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 5 def initialize(*args) add(*args) end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
10 11 12 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 10 def context @context end |
#owner ⇒ Object
Returns the value of attribute owner.
9 10 11 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 9 def owner @owner end |
#taggable ⇒ Object
Returns the value of attribute taggable.
11 12 13 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 11 def taggable @taggable end |
Class Method Details
.from(string) ⇒ Object
Returns a new TagList using the given tag string.
tag_list = TagList.from("One , Two, Three")
tag_list # ["One", "Two", "Three"]
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 79 def from(string) returning new do |tag_list| string = string.to_s.dup # Parse the quoted tags string.gsub!(/"(.*?)"\s*#{delimiter}?\s*/) { tag_list.add $1; "" } string.gsub!(/'(.*?)'\s*#{delimiter}?\s*/) { tag_list.add $1; "" } tag_list.add(string.split(delimiter)) end end |
.from_owner(owner, *tags) ⇒ Object
91 92 93 94 95 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 91 def from_owner(owner, *) returning from(*) do |taglist| taglist.owner = owner end end |
Instance Method Details
#add(*names) ⇒ Object
Add tags to the tag_list. Duplicate or blank tags will be ignored.
tag_list.add("Fun", "Happy")
Use the :parse
option to add an unparsed tag string.
tag_list.add("Fun, Happy", :parse => true)
20 21 22 23 24 25 26 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 20 def add(*names) taggable.send("#{context.singularize}_list_changed=", true) if (taggable && context) (names) concat(names) clean! self end |
#remove(*names) ⇒ Object
Remove specific tags from the tag_list.
tag_list.remove("Sad", "Lonely")
Like #add, the :parse
option can be used to remove multiple tags in a string.
tag_list.remove("Sad, Lonely", :parse => true)
35 36 37 38 39 40 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 35 def remove(*names) taggable.send("#{context.singularize}_list_changed=", true) if (taggable && context) (names) delete_if { |name| names.include?(name) } self end |
#to_s ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 47 def to_s clean! map do |name| name.include?(delimiter) ? "\"#{name}\"" : name end.join(delimiter.ends_with?(" ") ? delimiter : "#{delimiter} ") end |