Giter VIP home page Giter VIP logo

minimal_tags's Introduction

Build Status Code Climate Test Coverage Inline docs


MinimalTags

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.

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

Sequel

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

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 will 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'])

Partial tags

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)

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

minimal_tags's People

Stargazers

Dan avatar

Watchers

Dan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.