Module: MakeTaggable::Tagger::ClassMethods

Defined in:
lib/make_taggable/tagger.rb

Overview

Added to every Active Record model.

Instance Method Summary collapse

Instance Method Details

#is_tagger?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)

See Also:



71
72
73
# File 'lib/make_taggable/tagger.rb', line 71

def is_tagger?
  tagger?
end

#make_tagger(opts = {}) ⇒ void

This method returns an undefined value.

Make a model a tagger, allowing its instances to claim ownership of the tags they apply.

Adds an owned_taggings association and an owned_tags association to the model.

Examples:

class User < ActiveRecord::Base
  make_tagger
end

Parameters:

  • opts (Hash) (defaults to: {})

    options forwarded to the generated owned_taggings association

Options Hash (opts):

  • :scope (Proc)

    a scope applied to the owned_taggings association



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/make_taggable/tagger.rb', line 37

def make_tagger(opts = {})
  class_eval do
    owned_taggings_scope = opts.delete(:scope)

    has_many :owned_taggings, owned_taggings_scope,
      **opts.merge(
        as: :tagger,
        class_name: "::MakeTaggable::Tagging",
        dependent: :destroy
      )

    has_many :owned_tags, -> { distinct },
      class_name: MakeTaggable.tag_class,
      source: :tag,
      through: :owned_taggings
  end

  include MakeTaggable::Tagger::InstanceMethods
  extend MakeTaggable::Tagger::SingletonMethods
end

#tagger?TrueClass, FalseClass

Whether this model claims ownership of the tags it applies.

Returns:

  • (TrueClass, FalseClass)

    false until #make_tagger is called



63
64
65
# File 'lib/make_taggable/tagger.rb', line 63

def tagger?
  false
end