Module: MakeTaggable::Tagger::InstanceMethods

Defined in:
lib/make_taggable/tagger.rb

Overview

Added to a model once it is a tagger.

Instance Method Summary collapse

Instance Method Details

#is_tagger?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)

See Also:



125
126
127
# File 'lib/make_taggable/tagger.rb', line 125

def is_tagger?
  tagger?
end

#tag(taggable, opts = {}) ⇒ TrueClass, FalseClass

Tags a record, with this tagger as the owner of the tags.

The taggable is saved unless :skip_save is given, so the tags are persisted immediately.

Examples:

@user.tag(@photo, with: "paris, normandy", on: :locations)

Parameters:

  • taggable (ActiveRecord::Base)

    the record to tag

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

    the tagging options

Options Hash (opts):

  • :with (String, Array<String>)

    the tags to apply

  • :on (Symbol, String)

    the context to apply them in

  • :skip_save (TrueClass, FalseClass)

    whether to leave the taggable unsaved

  • :force (TrueClass, FalseClass)

    whether to allow a context the model does not declare, on by default

Returns:

  • (TrueClass, FalseClass)

    whether the taggable saved, or false when it is not taggable

Raises:

  • (RuntimeError)

    when :on or :with is missing, or the context is undeclared and :force is off



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/make_taggable/tagger.rb', line 99

def tag(taggable, opts = {})
  opts.reverse_merge!(force: true)
  skip_save = opts.delete(:skip_save)
  return false unless taggable.respond_to?(:is_taggable?) && taggable.is_taggable?

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

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

#tagger?TrueClass, FalseClass

Whether this record claims ownership of the tags it applies.

Returns:

  • (TrueClass, FalseClass)


117
118
119
# File 'lib/make_taggable/tagger.rb', line 117

def tagger?
  self.class.is_tagger?
end