module ActsAsTaggableOnMongoid::Taggable

This module defines the class methods to be added to the Mongoid model so that tags can be defined on and added to a model.

When a tag is added to a model, additional modules will be included to add methods that are needed only if a tag is actually being used.

Public Instance Methods

acts_as_ordered_taggable(options = {}) click to toggle source

This is an alias for calling acts_as_ordered_taggable_on :tags.

Example:

class Book < ActiveRecord::Base
  acts_as_ordered_taggable
end
# File lib/acts_as_taggable_on_mongoid/taggable.rb, line 75
def acts_as_ordered_taggable(options = {})
  acts_as_ordered_taggable_on :tags, options
end
acts_as_ordered_taggable_on(*tag_types) click to toggle source

Make a model taggable on specified contexts and preserves the order in which tags are created.

An alias for acts_as_taggable_on *tag_types, preserve_tag_order: true

@param [Array] tag_types An array of taggable contexts

Example:

class User < ActiveRecord::Base
  acts_as_ordered_taggable_on :languages, :skills
end
# File lib/acts_as_taggable_on_mongoid/taggable.rb, line 104
def acts_as_ordered_taggable_on(*tag_types)
  dup_tag_types = tag_types.dup
  options       = dup_tag_types.extract_options!.dup

  taggable_on(*dup_tag_types, options.merge(preserve_tag_order: true))
end
acts_as_taggable(options = {}) click to toggle source

This is an alias for calling acts_as_taggable_on :tags.

Example:

class Book < ActiveRecord::Base
  acts_as_taggable
end
# File lib/acts_as_taggable_on_mongoid/taggable.rb, line 64
def acts_as_taggable(options = {})
  acts_as_taggable_on :tags, options
end
acts_as_taggable_on(*tag_types) click to toggle source

Make a model taggable on specified contexts.

@param [Array] tag_types An array of taggable contexts

Example:

class User < ActiveRecord::Base
  acts_as_taggable_on :languages, :skills
end
# File lib/acts_as_taggable_on_mongoid/taggable.rb, line 88
def acts_as_taggable_on(*tag_types)
  taggable_on(*tag_types)
end
taggable_on(*tag_types) click to toggle source

Make a model taggable on specified contexts and optionally preserves the order in which tags are created

Separate methods used above for backwards compatibility so that the original acts_as_taggable_on method is unaffected as it's not possible to add another argument to the method without the tag_types being enclosed in square brackets

NB: method overridden in core module in order to create tag type

associations and methods after this logic has executed
# File lib/acts_as_taggable_on_mongoid/taggable.rb, line 124
def taggable_on(*tag_types)
  # if we are actually defining tags on a module, add these modules to add hooks and global methods
  # used by tagging.  We only add them dynamically like this so that they don't bloat the model
  # and add hooks/callbacks that aren't needed without tags.
  [ActsAsTaggableOnMongoid::Taggable::Core,
   ActsAsTaggableOnMongoid::Taggable::Changeable,
   ActsAsTaggableOnMongoid::Taggable::TaggedWith,
   # include Collection - not sure we will need as done here.  Need to think more on this one.
   ActsAsTaggableOnMongoid::Taggable::Cache,
   # include Related - TODO: Add this.
   ActsAsTaggableOnMongoid::Taggable::TaggerRelation,
   ActsAsTaggableOnMongoid::Taggable::ListTags].each do |include_module|
    include include_module unless included_modules.include?(include_module)
  end

  dup_tag_types = tag_types.dup
  options       = dup_tag_types.extract_options!.dup
  dup_tag_types.flatten!

  dup_tag_types.each do |tag_type|
    next if tag_type.blank?

    define_tag tag_type, options
  end
end