Sha256: 47dd0338f41543f090cd507f39203599e7b7edafc10524e8d7a717ba8e792b10

Contents?: true

Size: 1.48 KB

Versions: 22

Compression:

Stored size: 1.48 KB

Contents

module Tagtical
  module Taggable
    def taggable?
      false
    end

    ##
    # Make a model taggable on specified contexts.
    #
    # @param [Array] tag_types An array of taggable contexts. These must have an associated subclass under Tag.
    #
    # Example:
    #   module Tag
    #     class Language < Tagtical::Tag
    #     end
    #     class Skill < Tagtical::Tag
    #     end
    #   end
    #   class User < ActiveRecord::Base
    #     acts_as_taggable :languages, :skills
    #   end
    def acts_as_taggable(*tag_types)
      tag_types.flatten!
      tag_types << Tagtical::Tag::Type::BASE # always include the base type.
      tag_types = Tagtical::Tag::Type.register(tag_types.uniq.compact, self)

      if taggable?
        write_inheritable_attribute(:tag_types, (self.tag_types + tag_types).uniq)
      else
        write_inheritable_attribute(:tag_types, tag_types)
        class_inheritable_reader(:tag_types)

        has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag, :class_name => "Tagtical::Tagging"
        has_many :tags, :through => :taggings, :class_name => "Tagtical::Tag"
        
        class_eval do
          def self.taggable?
            true
          end
        end

        include Tagtical::Taggable::Core
        include Tagtical::Taggable::Collection
        include Tagtical::Taggable::Cache
        include Tagtical::Taggable::Ownership
        include Tagtical::Taggable::Related
        
      end
      
    end
  end
end 

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
tagtical-1.1.3 lib/tagtical/taggable.rb
tagtical-1.1.2 lib/tagtical/taggable.rb