Sha256: 0b6abaf445c52f3732c504fdf4ea40ef7d7d51d3c83c4ce633cba67ed29c5cd2

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

module ActsAsTaggableOn
  module Tagger
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def acts_as_tagger(opts={})
        has_many :owned_taggings, opts.merge(:as => :tagger, :dependent => :destroy,
                                             :include => :tag, :class_name => "Tagging")
        has_many :owned_tags, :through => :owned_taggings, :source => :tag, :uniq => true

        include ActsAsTaggableOn::Tagger::InstanceMethods
        extend ActsAsTaggableOn::Tagger::SingletonMethods
      end

      def is_tagger?
        false
      end
    end

    module InstanceMethods
      def tag(taggable, opts={})
        opts.reverse_merge!(:force => true)

        return false unless taggable.respond_to?(:is_taggable?) && taggable.is_taggable?

        raise "You need to specify a tag context using :on"                unless opts.has_key?(:on)
        raise "You need to specify some tags using :with"                  unless opts.has_key?(:with)
        raise "No context :#{opts[:on]} defined in #{taggable.class.to_s}" unless (opts[:force] || taggable.tag_types.include?(opts[:on]))

        taggable.set_owner_tag_list_on(self, opts[:on].to_s, opts[:with])
        taggable.save
      end

      def is_tagger?
        self.class.is_tagger?
      end
    end

    module SingletonMethods
      def is_tagger?
        true
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
acts-as-taggable-on-2.0.0.rc2 lib/acts_as_taggable_on/acts_as_tagger.rb
acts-as-taggable-on-2.0.0.rc1 lib/acts_as_taggable_on/acts_as_tagger.rb
acts-as-taggable-on-2.0.0.pre5 lib/acts_as_taggable_on/acts_as_tagger.rb
acts-as-taggable-on-0.0.0 lib/acts_as_taggable_on/acts_as_tagger.rb
acts-as-taggable-on-2.0.0.pre4 lib/acts_as_taggable_on/acts_as_tagger.rb