mongoid-flags

Simple document flagging system for mongoid v3. HEAVILY INSPIRED by mongoid-simple-roles

Install

Add this line to your Gemfile:

gem 'mongoid-flags'

And then execute:

bundle install

Or install it yourself:

gem install mongoid-flags

Usage

Model

class User
  include Mongoid::Document
  include Mongoid::Document::Flagable
  
  field :name, type: String
end

Instance Methods

Let’s first create an instance of the User model

user = User.create(name: 'John Doe')

add_flag(flag) will add a flag to the instance. You need to explicitly call .save to persist the changes.

user.add_flag('suspended')
=> ["suspended"]
user.reload
user.has_flag?('suspended')
=> false

add_flag!(flag) is same as add_flag but it automatically saves the instance.

user.add_flag!('suspended')
=> ["suspended"]
user.reload
user.has_flag?('suspended')
=> true

has_flag?(flag) checks if a flag exist and returns true or false.

user.add_flag!('suspended')
=> ["suspended"]
user.has_flag?('suspended')
=> true
user.has_flag?('underage')
=> false

remove_flag(flag) removes a flag. You need to explicitly call .save to persist the changes.

user.remove_flag('suspended')
=> []
user.reload
user.has_flag?('suspended')
=> true

remove_flag!(flag) same as remove_flag but it automatically saves the instance.

user.remove_flag!('suspended')
=> []
user.reload
user.has_flag?('suspended')
=> false

Class Methods

find_by_flags(flags) performs a query based on the flags provided and returns an array of objects (if any). The argument can either be a String or an Array.

User.find_by_flags('suspended')
=> [<User>]

User.find_by_flags(['suspended', 'underage'])
=> [<User>]

Contributing to mongoid-flags

Authors

Copyright © 2012 Thiago Jackiw. See LICENSE.txt for further details.