MinimalTags is a tiny gem for adding tagging support to Mongoid, ActiveRecord and Sequel.
Multiple tag fields can be used, each with their own way of formatting.
Please Note: ActiveRecord and Sequel both require PostgreSQL.
Add to your Gemfile:
gem 'minimal_tags'
There is nothing else to add, fields will be created when you define your tag fields.
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
Add the following migration for each tag field you want.
Sequel.migration do
change do
add_column :posts, :tags, 'text[]', default: [], null: false
add_index :posts, :tags, type: :gin
end
end
Also enable the pg_array
and pg_array_ops
extensions:
DB = Sequel.connect('postgres://...')
DB.extension :pg_array
Sequel.extension :pg_array_ops
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 will be used.
MinimalTags.default_formatter = UnderscoreTagFormatter.new
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'])
Sequel only for now
Find with partial tag match:
Document.partial_tags('hello').all
Array of matching tags:
Document.partial_tags('hello').select_map(:mt_tags)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Write your tests
- Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request