Module: MakeTaggable

Extended by:
ActiveSupport::Autoload
Defined in:
lib/make_taggable.rb,
lib/make_taggable/tag.rb,
lib/make_taggable/utils.rb,
lib/make_taggable/engine.rb,
lib/make_taggable/tagger.rb,
lib/make_taggable/tagging.rb,
lib/make_taggable/version.rb,
lib/make_taggable/tag_list.rb,
lib/make_taggable/taggable.rb,
lib/make_taggable/tags_helper.rb,
lib/make_taggable/default_parser.rb,
lib/make_taggable/generic_parser.rb

Overview

Tagging for Active Record models, across any number of named contexts.

Make a model taggable with Taggable#make_taggable, and configure the library through MakeTaggable.setup. Every setting on Configuration can also be read and written directly here.

Examples:

class Book < ActiveRecord::Base
  make_taggable :genres
end

Book.create!(genre_list: "sci-fi, classic")
Book.tagged_with("sci-fi")

Defined Under Namespace

Modules: Taggable, Tagger, TagsHelper, Utils Classes: Configuration, DefaultParser, DuplicateTagError, Engine, GenericParser, Tag, TagList, Tagging

Constant Summary collapse

VERSION =

The released version of the gem.

Returns:

  • (String)
"1.7.1"

Class Method Summary collapse

Class Method Details

.glueString

The string used to join tags back together for display, derived from the configured delimiter.

A trailing space is added when the delimiter does not already end in one, so a comma delimiter renders as "one, two" rather than "one,two". When several delimiters are configured the first is used.

Examples:

MakeTaggable.glue # => ", "

Returns:

  • (String)

    the delimiter, guaranteed to end in a space



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

def self.glue
  setting = @configuration.delimiter
  delimiter = setting.is_a?(Array) ? setting[0] : setting
  delimiter.end_with?(" ") ? delimiter : "#{delimiter} "
end

.method_missing(method_name, *args, &block) ⇒ Object

Forwards unknown calls to the configuration, so every setting can be read and written directly on MakeTaggable as well as inside a setup block.

Examples:

MakeTaggable.force_lowercase = true
MakeTaggable.force_lowercase # => true

Parameters:

  • method_name (Symbol)

    the configuration reader or writer

  • args (Array<Object>)

    arguments forwarded to the configuration

Returns:

  • (Object)

    whatever the configuration returns

Raises:

  • (NoMethodError)

    when the configuration has no such setting



93
94
95
96
97
98
99
# File 'lib/make_taggable.rb', line 93

def self.method_missing(method_name, *args, &block)
  if @configuration.respond_to?(method_name)
    @configuration.send(method_name, *args, &block)
  else
    super
  end
end

.respond_to_missing?(method_name, include_private = false) ⇒ TrueClass, FalseClass

Reports the configuration's settings as available on MakeTaggable itself.

Parameters:

  • method_name (Symbol)

    the method being tested

  • include_private (TrueClass, FalseClass) (defaults to: false)

    whether private methods count

Returns:

  • (TrueClass, FalseClass)


108
109
110
# File 'lib/make_taggable.rb', line 108

def self.respond_to_missing?(method_name, include_private = false)
  @configuration.respond_to?(method_name, include_private) || super
end

.setup {|configuration| ... } ⇒ MakeTaggable::Configuration, NilClass

Yields the configuration so settings can be assigned in one block.

Called without a block it only ensures the configuration exists, which is how the library initialises itself on load.

Examples:

MakeTaggable.setup do |config|
  config.force_lowercase = true
  config.remove_unused_tags = true
end

Yield Parameters:

Returns:



75
76
77
78
# File 'lib/make_taggable.rb', line 75

def self.setup
  @configuration ||= Configuration.new
  yield @configuration if block_given?
end

.tag_modelClass

The class tags are read, written and returned as.

Resolved on each call rather than memoised, so a reloaded class in development is picked up.

Returns:

  • (Class)


137
138
139
# File 'lib/make_taggable.rb', line 137

def self.tag_model
  tag_class.constantize
end