GithubHelp home page GithubHelp logo

mongoid_acts_as_list's Introduction

ActsAsList for Mongoid

Description

Mongoid::ActsAsList provides the ability of ordering and sorting a number of objects in a list using Mongoid as an ODM.

Build Status Dependency Status

Install

Place the following in your Gemfile:

gem 'mongoid_acts_as_list', '~> 0.0.5'

Then run bundle install

Configure

Configure defaults values used by ActsAsList:

Mongoid::ActsAsList.configure do |config|

  # These are the default values. Modify as you see fit:

  config.default_position_field = :position
  config.start_list_at = 0

end

Make sure it is loaded before calling acts_as_list. You can place this code in an initializer file for example.

Use

Activate ActsAsList in your models.

In has_many/belongs_to associations, you will need to provide a :scope option:

class List
  include Mongoid::Document

  has_many :items
end

class Item
  include Mongoid::Document
  include Mongoid::ActsAsList

  belongs_to :list
  acts_as_list scope: :list
end

On embedded document, the scope option is not necessary:

class List
  include Mongoid::Document

  embeds_many :items
end

class Item
  include Mongoid::Document
  include Mongoid::ActsAsList

  embedded_in :list
  acts_as_list
end

The public API is composed of the following methods. All of the API from the original ActiveRecord ActsAsList gem is also available. Check the source and documentation to find out more!

## Class Methods

list.items.order_by_position #=> returns all items in `list` ordered by position

## Instance Methods

item.move to: 2              #=> moves item to position number 2
item.move to: :start         #=> moves item to the first position in the list
item.move to: :end           #=> moves item to the last position in the list
item.move before: other_item #=> moves item before other_item
item.move after:  other_item #=> moves item after other_item
item.move forward:  3        #=> move item 3 positions closer to the end of the list
item.move backward: 2        #=> move item 2 positions closer to the start of the list
item.move :forward           #=> same as item.move(forward:  1)
item.move :backward          #=> same as item.move(backward: 1)

item.in_list?                #=> true
item.remove_from_list        #=> sets the position to nil and reorders other items
item.not_in_list?            #=> true

item.first?
item.last?

item.next_item               #=> returns the item immediately following `item` in the list
item.previous_item           #=> returns the item immediately preceding `item` in the list

## Original ActsAsList #move_ methods

item.move_higher             #=> same as item.move(:backward)
item.move_lower              #=> same as item.move(:forward)
item.move_to_top             #=> same as item.move(to: :start)
item.move_to_bottom          #=> same as item.move(to: :end)
item.increment_position      #=> increments the position number without affecting other items
item.decrement_position      #=> decrements the position number without affecting other items
item.insert_at 3             #=> same as item.move(to: 3)

Requirements

Tested with Mongoid 2.4.6 on Ruby 1.9.3-p125, Rails 3.2.2, and Mongo 2.x

Contributing

  • Open up an issue on Github
  • Fork the project
  • Start a feature/bugfix branch
  • Start writing tests
  • Commit and push until all tests are green and you are happy with your contribution
  • Send a pull request mentionning the issue number (i.e. "Fixes #123")

Copyright

Copyright (c) 2012 Olivier Melcher, released under the MIT license

mongoid_acts_as_list's People

Contributors

jesson avatar olivoil avatar scr1pt avatar

Watchers

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