Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added mongoid 3 support

  • Loading branch information...
commit b60a53a3b8715b421ac0640efbdab25e281f7140 1 parent 1864afe
@pyromaniac authored
View
2  .rvmrc
@@ -1 +1 @@
-rvm use 1.9.2@mongoid_orderable --create
+rvm use 1.9.3@mongoid_orderable --create
View
8 .travis.yml
@@ -0,0 +1,8 @@
+rvm:
+ - 1.9.2
+ - 1.9.3
+ - ruby-head
+ #- rbx-19mode
+
+env:
+ - MONGOID_VERSION=2
View
7 Gemfile
@@ -2,3 +2,10 @@ source "http://rubygems.org"
# Specify your gem's dependencies in mongoid_orderable.gemspec
gemspec
+
+case version = ENV['MONGOID_VERSION'] || "~> 3.0.0.rc"
+when /2/
+ gem "mongoid", "~> 2.4.0"
+else
+ gem "mongoid", version
+end
View
3  README.md
@@ -1,3 +1,5 @@
+[![Build Status](https://secure.travis-ci.org/pyromaniac/mongoid_orderable.png)](http://travis-ci.org/pyromaniac/mongoid_orderable)
+
# What?
Mongoid::Orderable is a ordered list implementation for your mongoid models.
@@ -7,6 +9,7 @@ Mongoid::Orderable is a ordered list implementation for your mongoid models.
* It uses native mongo batch increment feature
* It supports assignable api
* It proper assingns position while moving document between scopes
+* It supports both mongoid 2 and 3
# How?
View
23 lib/mongoid/orderable.rb
@@ -13,15 +13,22 @@ def orderable options = {}
configuration[:scope] = "#{configuration[:scope]}_id".to_sym if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
field configuration[:column], :type => Integer
- index configuration[:column] if configuration[:index]
+ if configuration[:index]
+ if MongoidOrderable.mongoid2?
+ index configuration[:column]
+ else
+ index(configuration[:column] => 1)
+ end
+ end
case configuration[:scope]
when Symbol then
- scope :orderable_scope, lambda { |document| where(configuration[:scope] => document.send(configuration[:scope])) }
+ scope :orderable_scope, lambda { |document|
+ where(configuration[:scope] => document.send(configuration[:scope])) }
when Proc then
scope :orderable_scope, configuration[:scope]
else
- scope :orderable_scope, lambda { |document| where }
+ scope :orderable_scope, lambda { |document| where({}) }
end
define_method :orderable_column do
@@ -86,7 +93,7 @@ def add_to_list
end
def remove_from_list
- orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column => -1)
+ orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column, -1)
end
private
@@ -122,10 +129,10 @@ def apply_position target_position
target_position = target_position_to_position target_position
unless in_list?
- orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column => 1)
+ orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column, 1)
else
- orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column => 1) if target_position < orderable_position
- orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column => -1) if target_position > orderable_position
+ orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column, 1) if target_position < orderable_position
+ orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column, -1) if target_position > orderable_position
end
self.orderable_position = target_position
@@ -148,7 +155,7 @@ def target_position_to_position target_position
def bottom_orderable_position
@bottom_orderable_position = begin
- max = orderable_scoped.max(orderable_column).to_i
+ max = orderable_scoped.distinct(orderable_column).map(&:to_i).max.to_i
in_list? ? max : max.next
end
end
View
21 lib/mongoid_orderable.rb
@@ -1,11 +1,18 @@
+module MongoidOrderable
+ def self.mongoid2?
+ Mongoid.const_defined? :Contexts
+ end
+end
+
require 'mongoid'
require 'mongoid_orderable/version'
-require 'mongoid_orderable/mongoid/contexts/mongo'
-require 'mongoid_orderable/mongoid/contexts/enumerable'
-require 'mongoid_orderable/mongoid/criteria'
-
-require 'mongoid/orderable'
-module MongoidOrderable
-
+if MongoidOrderable.mongoid2?
+ require 'mongoid_orderable/mongoid/contexts/mongo'
+ require 'mongoid_orderable/mongoid/contexts/enumerable'
+ require 'mongoid_orderable/mongoid/criteria'
+else
+ require 'mongoid_orderable/mongoid/contextual/memory'
end
+
+require 'mongoid/orderable'
View
6 lib/mongoid_orderable/mongoid/contexts/enumerable.rb
@@ -2,11 +2,9 @@ module MongoidOrderable #:nodoc:
module Mongoid #:nodoc:
module Contexts #:nodoc:
module Enumerable #:nodoc:
- def inc attributes = {}
+ def inc attribute, value
iterate do |doc|
- attributes.each do |attribute, value|
- doc.inc(attribute, value)
- end
+ doc.inc(attribute, value)
end
end
end
View
4 lib/mongoid_orderable/mongoid/contexts/mongo.rb
@@ -2,10 +2,10 @@ module MongoidOrderable #:nodoc:
module Mongoid #:nodoc:
module Contexts #:nodoc:
module Mongo #:nodoc:
- def inc attributes = {}
+ def inc attribute, value
klass.collection.update(
selector,
- { "$inc" => attributes },
+ { "$inc" => {attribute => value} },
:multi => true,
:safe => ::Mongoid.persist_in_safe_mode
)
View
15 lib/mongoid_orderable/mongoid/contextual/memory.rb
@@ -0,0 +1,15 @@
+module MongoidOrderable #:nodoc:
+ module Mongoid #:nodoc:
+ module Contextual #:nodoc:
+ module Memory #:nodoc:
+ def inc attribute, value
+ each do |document|
+ document.inc(attribute, value)
+ end
+ end
+ end
+ end
+ end
+end
+
+Mongoid::Contextual::Memory.send :include, MongoidOrderable::Mongoid::Contextual::Memory
View
10 spec/mongoid/orderable_spec.rb
@@ -61,7 +61,7 @@ class NoIndexOrderable
end
def positions
- SimpleOrderable.order_by(:position, :asc).map(&:position)
+ SimpleOrderable.all.map(&:position).sort
end
it 'should have proper position column' do
@@ -70,7 +70,11 @@ def positions
end
it 'should have index on position column' do
- SimpleOrderable.index_options[:position].should_not be_nil
+ if MongoidOrderable.mongoid2?
+ SimpleOrderable.index_options[:position].should_not be_nil
+ else
+ SimpleOrderable.index_options[{:position => 1}].should_not be_nil
+ end
end
it 'should set proper position while creation' do
@@ -261,7 +265,7 @@ def positions
end
def positions
- EmbedsOrderable.order_by(:position).all.map { |eo| eo.embedded_orderables.order_by(:position).map(&:position) }
+ EmbedsOrderable.order_by(:position => 1).all.map { |eo| eo.embedded_orderables.map(&:position).sort }
end
it 'should set proper position while creation' do
View
24 spec/spec_helper.rb
@@ -1,13 +1,19 @@
require 'bundler'
Bundler.require
-DATABASE_ID = Process.pid
+DATABASE_NAME = "mongoid_#{Process.pid}"
-Mongoid.configure do |config|
- database = Mongo::Connection.new.db("mongoid_#{DATABASE_ID}")
- database.add_user("mongoid", "test")
- config.master = database
- config.logger = nil
+if MongoidOrderable.mongoid2?
+ Mongoid.configure do |config|
+ # database = Mongo::Connection.new.db DATABASE_NAME
+ # database.add_user "mongoid", "test"
+ config.master = Mongo::Connection.new.db DATABASE_NAME
+ config.logger = nil
+ end
+else
+ Mongoid.configure do |config|
+ config.connect_to DATABASE_NAME
+ end
end
RSpec.configure do |config|
@@ -22,6 +28,10 @@
end
config.after(:suite) do
- Mongoid.master.connection.drop_database("mongoid_#{DATABASE_ID}")
+ if MongoidOrderable.mongoid2?
+ Mongoid.master.connection.drop_database DATABASE_NAME
+ else
+ Mongoid.default_session.drop
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.