Module: ActsAsTaggable::ActMethod

Defined in:
lib/acts_as_taggable/acts_as_taggable.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#acts_as_taggable(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 3

def acts_as_taggable(options = {})
  class_attribute :acts_as_taggable_options
  self.acts_as_taggable_options = options
  self.acts_as_taggable_options.reverse_merge! :delimiter => ',', :downcase => true, :remove_tag_if_empty => true
  self.acts_as_taggable_options.reverse_merge! :output_delimiter => acts_as_taggable_options[:delimiter]
  self.acts_as_taggable_options[:types] = Array(self.acts_as_taggable_options[:types])

  has_many :taggings, -> { order("#{Tagging.table_name}.id") }, :as => :taggable, :after_remove => :delete_tag_if_necessary, :dependent => :destroy, :class_name => 'ActsAsTaggable::Tagging'
  has_many :tags, :through => :taggings, :class_name => 'ActsAsTaggable::Tag', :after_add => :reset_scoped_associations

  extend ClassMethods
  include InstanceMethods

  self.acts_as_taggable_options[:types].each do |tag_type|
    has_many :"#{tag_type}_taggings", -> { joins(:tag).order("#{Tagging.table_name}.id").where(Tag.table_name => {:tag_type => tag_type}) }, :as => :taggable, :after_add => :reset_associations, :after_remove => :delete_tag_if_necessary, :class_name => 'ActsAsTaggable::Tagging'
    has_many :"#{tag_type}_tags", -> { where(:tag_type => tag_type) }, :through => :taggings, :source => :tag, :class_name => 'ActsAsTaggable::Tag', :after_add => :reset_associations

    metaclass = class << self; self; end
    HelperMethods.scope_class_methods(metaclass, tag_type)
    HelperMethods.scope_instance_methods(self, tag_type)
  end
end