Build Status Code Climate Test Coverage Inline docs


MinimalTags

MinimalTags is a tiny gem for adding tagging support to Mongoid and ActiveRecord (Currently only Postgresql).

Multiple tag fields can be used, each with their own way of formatting.

Installation

Add to your Gemfile:

gem 'minimal_tags'

Mongoid

There is nothing else to add, fields will be created when you define your tag fields.

ActiveRecord

Add the following migration for each tag field you want.

class AddTagsToPosts < ActiveRecord::Migration
  def change
    add_column :posts, :tags, :text, default: [], null: false, array: true
    add_index :posts, :tags, using: :gin
  end
end

Usage

Start by including the MinimalTags module and define your tag fields with the tag_field method.

class Document
  include Mongoid::Document
  include MinimalTags

  tag_field :tags
  tag_field :upcase_tags, formatter: UpcaseFormatter.new
end

You can define multiple tag fields, just ensure they have unique names.
Formatters can be defined on each tag field if needed, otherwise the default, MinimalTags::SimpleFormatter be used.

Changing the default formatter

MinimalTags.default_formatter = UnderscoreTagFormatter.new

Searching

The following scopes are also added for each tag field and can be chained: .any_*, .all_*, .without_any_*, .without_all_*

Document.any_tags(['a', 'b', 'c'])
Document.all_tags(['a', 'b', 'c'])
Document.without_any_tags(['a', 'b', 'c'])
Document.without_all_tags(['a', 'b', 'c'])

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write your tests
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request