Giter Club home page Giter Club logo

acts_as_list_mongoid's Introduction

Mongoid Acts as list

This is a port of the classic +acts_as_list+ to Mongoid.

This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list. If you do not specify custom position +column+ in the options, a key named +pos+ will be used automatically.

Installation

gem install acts_as_list_mongoid

Usage

See the /specs folder specs that demontrate the API. Usage examples are located in the /examples folder.

Update 26, Nov 2010

The gem doesn't seem to work with the latest versions of Mongoid (> beta14), please help fix this ;) Usage has been simplified using a suggestion by 'KieranP'

To make a class Act as List, simply do:

  include ActsAsList::Mongoid   

And it will automatically set up a field and call acts_as_list with that field. By default the field name is :position. You can change the defaut position_column name used: ActsAsList::Mongoid.default_position_column = :pos. For this class variable to be effetive, it should be set before calling include ActsAsList::Mongoid.

Example

  require 'mongoid'
  require 'mongoid_embedded_helper'

  Mongoid.configure.master = Mongo::Connection.new.db('acts_as_list-test')

  class Item
    include Mongoid::Document
    include Mongoid::Timestamps
    include ActsAsList::Mongoid 
    
    field :number, :type => Integer
    
    embedded_in :list, :inverse_of => :items
  end    

  class List
    include Mongoid::Document
    field :name, :type => String
    embeds_many :items
  end


  todo_list = List.new :name => 'My todo list'

  %w{'clean', 'wash', 'repair'}.each do |name| 
    todo_item = Item.new(:name => name)
    todo_list.items << todo_item
  end  
  todo_list.items.init_list! # IMPORTANT!!!

  todo_list.items.first.move(:bottom)
  todo_list.items.last.move(:higher)

Overriding defaults

By default, when including ActsAsList::Mongoid, the field is set to :pos and the acts_as_list column to :pos. To change this:

  include ActsAsList::Mongoid   
  
  field :pos, :type => Integer
  acts_as_list :column => :pos

List initialization

In order for the list items to be initialized properly, it is necessary to call the method init_list! on the collection in order for the position of each list item to be set to an initial position.

+Example:+ todo_list.items.init_list!

New move API borrowed from Data Mapper in-list plugin

item.move(:highest)          # moves to top of list.
item.move(:lowest)           # moves to bottom of list.
item.move(:top)              # moves to top of list.
item.move(:bottom)           # moves to bottom of list.
item.move(:up)               # moves one up (:higher and :up is the same) within the scope.
item.move(:down)             # moves one up (:lower and :down is the same) within the scope.
item.move(:to => position)   # moves item to a specific position.
item.move(:above => other)   # moves item above the other item.*
item.move(:below => other)
## Running the specs

rspec spec


acts_as_list_mongoid's People

Contributors

kamui avatar kristianmandrup avatar manishmohanlal avatar rickenharp avatar rudy-on-rails avatar saberma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

acts_as_list_mongoid's Issues

move(:bottom) results in 'wrong number of arguments' - mongoid 3.1.2

ruby-1.9.3-p385
mongoid-3.1.2
mongodb version v2.4.0

embedded documents

move(:top) works

# document 'doc' has many embedded documents 'eds'
e = doc.eds.last
e.move(:top)
d.eds.map(&:position)
   => [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1] 
doc.reload
doc.eds.map(&:position)
   => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 

move(:bottom) crashes

doc.eds.map(&:position)
   => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 
e = doc.eds.first
e.move(:bottom)
# fails. see stacktrace below.
doc.eds.map(&:position)
   => [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] 

Stacktrace

ArgumentError: wrong number of arguments (0 for 1)
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/mongoid-3.1.2/lib/mongoid/contextual/memory.rb:225:in `sort'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:19:in `sort'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/acts_as_list_mongoid-0.2.5.1/lib/mongoid/acts_as_list.rb:107:in `order_by_position'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/acts_as_list_mongoid-0.2.5.1/lib/mongoid/acts_as_list.rb:304:in `bottom_item'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/acts_as_list_mongoid-0.2.5.1/lib/mongoid/acts_as_list.rb:293:in `bottom_position_in_list'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/acts_as_list_mongoid-0.2.5.1/lib/mongoid/acts_as_list.rb:309:in `assume_bottom_position'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/acts_as_list_mongoid-0.2.5.1/lib/mongoid/acts_as_list.rb:193:in `move_to_bottom'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/acts_as_list_mongoid-0.2.5.1/lib/mongoid/acts_as_list.rb:77:in `move'
from (irb):51
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /Users/oma/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

Define columns automatically

It would be great if all that was needed was

include Mongoid::ActsAsList

Then it'd automatically create a position field, and initialize the code without having to call field yourself or calling acts_as_list.

Is this possible?

undefined method 'set_allowed?'

First I want to thanks for porting this gem. Whenever I try to us this on a document in my app, I keep receiving the error undefined method 'set_allowed?'. I've forked your repo and run the tests and am getting the same error. I've tried to trace the stack, reviewing the code in both of your other gems, mongoid_embedded_helper and mongoid_adjust, but I can't seem to figure out where the 'set_allowed?' method is being called. I'd be happy to try and create a patch if you could point me in the right direction. My app is using rails 3.1.0.rc4 with mongoid 2.0.2. Thanks!

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.