mongoid_taggable

by wilkerlucio

wilkerlucio / mongoid_taggable

Mongoid taggable behaviour

198 Stars 72 Forks Last release: Not found Apache License 2.0 68 Commits 4 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

h1. Mongoid Taggable

Mongoid Taggable provides some helpers to create taggable documents.

h2. Installation

You can install from rubygems:

bc. gem install mongoid_taggable

or in Gemfile:

bc. gem 'mongoid_taggable'

or as a Rails Plugin:

bc. script/plugin install git://github.com/wilkerlucio/mongoid_taggable.git

h2. Mongoid 2 vs Mongoid 3

All gem versions less than 1.x only support Mongoid 2 while versions 1.x and greater support Mongoid 3.

h2. Basic Usage

To make a document taggable you need to include @Mongoid::[email protected] into your document:

bc.. class Post include Mongoid::Document include Mongoid::Taggable

field :title field :content end

p. And in your form:

bc.. <% formfor @post do |f| %>

<%= f.label :title %>
<%= f.text

field :title %>

<%= f.label :content %>
<%= f.textarea :content %>

<%= f.label :tags %>
<%= f.textfield :tags %>

Send

<% end %>

p. In this case, the text fields for tags should receive the list of tags separated by comma (See below for how to change the separator).

p. Then your document will have the @[email protected] and @tags[email protected] getter and setter. @[email protected] is as a plain string with tags separated by comma, and @tags[email protected] an array representation.

h2. Finding Objects by Tag

p. Tagged models get a scope called @tagged[email protected], @taggedwith[email protected], and @tagged[email protected]:

bc.. MyModel.taggedwith('foo') MyModel.published.taggedwith('foo').count MyModel.taggedwithall('foo', 'bar') MyModel.taggedwithall(['foo', 'bar']) MyModel.taggedwithany('foo', 'bar') MyModel.taggedwithany(['foo', 'bar'])

h2. Tags Indexing

This module will automatically create an index of tags and their counts for you after saving the document. This can be used for a tag cloud. See the following example to understand how to use it:

bc.. Post.create!(:tags => "food,ant,bee") Post.create!(:tags => "juice,food,bee,zip") Post.create!(:tags => "honey,strip,food")

Post.tags # will retrieve ["ant", "bee", "food", "honey", "juice", "strip", "zip"] Post.tagswithweight # will retrieve:

[

['ant', 1],

['bee', 2],

['food', 3],

['honey', 1],

['juice', 1],

['strip', 1],

['zip', 1]

]

p. The tags index is unaffected by default_scopes. If enabled, all tags will be indexed regardless of other attribute values. p. If you don't want to use this feature, it is good to disable it to improve performance:

bc.. class Post include Mongoid::Document include Mongoid::Taggable

disabletagsindex! # will disable index creation

field :title field :content end

h2. Changing default separator

To change the default separator you may call the @[email protected] class method:

bc.. class Post include Mongoid::Document include Mongoid::Taggable

tags_separator ';' # will change tags separator to ;

field :title field :content end

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.