module ActiveRecord::Acts::Taggable::InstanceMethods
Public Instance Methods
This method applies tags to the target object, by parsing the tags parameter into Tag object instances and adding them to the tag collection of the object. If the tag name already exists in the tags table, it just adds a relationship to the existing tag record. If it doesn't exist, it then creates a new Tag record for it.
The tags
parameter can be a String
,
Array
or a Proc
object. If it's a
String
, it's split using the :separator
specified in the options
hash. If it's an
Array
it is flattened and compacted. Duplicate entries will
be removed as well. Tag names are also stripped of trailing and leading
whitespace. If a Proc is passed, the proc should split the string in any
way it wants and return an array of strings.
The options
hash has the following parameters:
:separator
=> defines the separator (String or Regex) used
to split the tags parameter and defaults to ' ' (space and line
breaks).
:clear
=> defines whether the existing tag collection will
be cleared before applying the new tags
passed. Defaults to
false
.
# File lib/taggable.rb, line 556 def tag(tags, options = {}) options = { :separator => ' ', :clear => false }.merge(options) attributes = options[:attributes] || {} # parse the tags parameter tag_names = ActiveRecord::Acts::Taggable.split_tag_names(tags, options[:separator], normalizer) # clear the collection if appropriate self.clear_tags! if options[:clear] # append the tag names to the collection tag_names.each do |name| # ensure that tag names don't get duplicated tag_record = tag_model.find(:first, :conditions=>["#{tag_model_name} = ?",name]) || tag_model.new(tag_model_name.to_sym => name) if tags_join_model tag_join_record = tags_join_model.new(attributes) tag_join_record.tag = tag_record tag_join_record.tagged = self tag_collection << tag_join_record unless tagged_with?(name) else tag_collection.push_with_attributes(tag_record, attributes) unless tagged_with?(name) end end end
Returns an array of strings containing the tags applied to this object. If
reload
is true
, the tags collection is reloaded.
# File lib/taggable.rb, line 595 def tag_names(reload = false) ary = tag_collection(reload).map { |tag| tag.send(tag_model_name.to_sym)} ary.extend(TagNamesMixin) ary.set_tag_container(self) ary end
Clears the current tags collection and sets the tag names for this object. Equivalent of calling tag(…, :clear => true)
Another way of appending tags to a existing tags collection is by using the
+<<+ or concat
method on tag_names
, which
is equivalent of calling tag(…, :clear => false).
# File lib/taggable.rb, line 589 def tag_names=(tags, options = {}) tag(tags, options.merge(:clear => true)) end
This method removes tags from the target object, by parsing the tags parameter into Tag object instances and removing them from the tag collection of the object if they exist.
The tags
parameter can be a String
,
Array
or a Proc
object. If it's a
String
, it's split using the :separator
specified in the options
hash. If it's an
Array
it is flattened and compacted. Duplicate entries will
be removed as well. Tag names are also stripped of trailing and leading
whitespace. If a Proc is passed, the proc should split the string in any
way it wants and return an array of strings.
The options
hash has the following parameters:
:separator
=> defines the separator (String or Regex) used
to split the tags parameter and defaults to ' ' (space and line
breaks).
# File lib/taggable.rb, line 519 def tag_remove(tags, options = {}) options = { :separator => ' '}.merge(options) attributes = options[:attributes] || {} # parse the tags parameter tag_names = ActiveRecord::Acts::Taggable.split_tag_names(tags, options[:separator], normalizer) # remove the tag names to the collection tag_names.each do |name| tag_record = tag_model.find(:first, :conditions=>["#{tag_model_name} = ?",name]) || tag_model.new(tag_model_name.to_sym => name) if tag_record tag_collection.delete(tag_record) end end end
Checks to see if this object has been tagged with tag_name
. If
reload
is true, reloads the tag collection before doing the
check.
# File lib/taggable.rb, line 604 def tagged_with?(tag_name, reload = false) tag_names(reload).include?(tag_name) end
Checks to see if this object has been tagged with all tags
-
they can be a string,or list The options
hash has the
following parameters: :separator
=> defines the separator
(String or Regex) used to split the tags parameter and defaults to '
' (space and line breaks). :reload
=> That forces the
tag names to be reloaded first
# File lib/taggable.rb, line 612 def tagged_with_all?(tags, options = {}) options = { :separator => ' ', :reload => false }.merge(options) requested= ActiveRecord::Acts::Taggable.split_tag_names(tags, options[:separator], normalizer) tag_names(options[:reload]) if options[:reload] requested.each {|tag_name| return false if !tag_names.include?(tag_name) } return true end
Checks to see if this object has been tagged with any tags
-
they can be a string,or list The options
hash has the
following parameters: :separator
=> defines the separator
(String or Regex) used to split the tags parameter and defaults to '
' (space and line breaks). :reload
=> That forces the
tag names to be reloaded first
# File lib/taggable.rb, line 626 def tagged_with_any?(tags, options = {}) options = { :separator => ' ', :reload => false }.merge(options) requested= ActiveRecord::Acts::Taggable.split_tag_names(tags, options[:separator], normalizer) tag_names(options[:reload]) if options[:reload] requested.each {|tag_name| return true if tag_names.include?(tag_name) } return false end
Private Instance Methods
# File lib/taggable.rb, line 646 def tag_collection(reload = false) send(self.class.tag_collection_name, reload) end
# File lib/taggable.rb, line 642 def tag_model self.class.tag_model end