GithubHelp home page GithubHelp logo

navneet35371 / mongoid_taggable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shortcast/mongoid_taggable

0.0 2.0 0.0 155 KB

Mongoid taggable behaviour

Home Page: http://wilkerlucio.github.com/mongoid_taggable/

License: Apache License 2.0

Ruby 100.00%

mongoid_taggable's Introduction

Mongoid Taggable

Mongoid Taggable provides some helpers to create taggable documents.

Installation

You can install from rubygems:

gem install mongoid_taggable

or in Gemfile:

gem 'mongoid_taggable'

or as a Rails Plugin:

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

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.

Basic Usage

To make a document taggable you need to include Mongoid::Taggable into your document:

class Post
  include Mongoid::Document
  include Mongoid::Taggable

  field :title
  field :content
end

And in your form:

<% form_for @post do |f| %>
  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </p>
  <p>
    <%= f.label :tags %><br />
    <%= f.text_field :tags %>
  </p>
  <p>
    <button type="submit">Send</button>
  </p>
<% end %>

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).

Then your document will have the tags and tags_array getter and setter. tags is as a plain string with tags separated by comma, and tags_array an array representation.

Finding Objects by Tag

Tagged models get a scope called tagged_with, tagged_with_all, and tagged_with_any:

MyModel.tagged_with('foo')
MyModel.published.tagged_with('foo').count
MyModel.tagged_with_all('foo', 'bar')
MyModel.tagged_with_all(['foo', 'bar'])
MyModel.tagged_with_any('foo', 'bar')
MyModel.tagged_with_any(['foo', 'bar'])

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:

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.tags_with_weight # will retrieve:
# [
#   ['ant', 1],
#   ['bee', 2],
#   ['food', 3],
#   ['honey', 1],
#   ['juice', 1],
#   ['strip', 1],
#   ['zip', 1]
# ]

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:

class Post
  include Mongoid::Document
  include Mongoid::Taggable

  disable_tags_index! # will disable index creation

  field :title
  field :content
end

Changing default separator

To change the default separator you may call the tags_separator class method:

class Post
  include Mongoid::Document
  include Mongoid::Taggable

  tags_separator ';' # will change tags separator to ;

  field :title
  field :content
end

mongoid_taggable's People

Contributors

wilkerlucio avatar ches avatar astjohn avatar florian95 avatar nu7hatch avatar shingara avatar gboruk avatar yairgo avatar pvertenten avatar tomk32 avatar

Watchers

James Cloos avatar Navneet suman 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.