mongoid_orderable 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/.travis.yml +11 -0
- data/Gemfile +7 -0
- data/README.md +3 -0
- data/lib/mongoid/orderable.rb +16 -9
- data/lib/mongoid_orderable.rb +14 -7
- data/lib/mongoid_orderable/mongoid/contexts/enumerable.rb +2 -4
- data/lib/mongoid_orderable/mongoid/contexts/mongo.rb +2 -2
- data/lib/mongoid_orderable/mongoid/contextual/memory.rb +15 -0
- data/lib/mongoid_orderable/version.rb +1 -1
- data/mongoid_orderable.gemspec +1 -0
- data/spec/mongoid/orderable_spec.rb +7 -3
- data/spec/spec_helper.rb +17 -7
- metadata +35 -7
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use 1.9.
|
1
|
+
rvm use 1.9.3@mongoid_orderable --create
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[![Build Status](https://secure.travis-ci.org/pyromaniac/mongoid_orderable.png)](http://travis-ci.org/pyromaniac/mongoid_orderable)
|
2
|
+
|
1
3
|
# What?
|
2
4
|
|
3
5
|
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.
|
|
7
9
|
* It uses native mongo batch increment feature
|
8
10
|
* It supports assignable api
|
9
11
|
* It proper assingns position while moving document between scopes
|
12
|
+
* It supports both mongoid 2 and 3
|
10
13
|
|
11
14
|
# How?
|
12
15
|
|
data/lib/mongoid/orderable.rb
CHANGED
@@ -13,15 +13,22 @@ module Mongoid::Orderable
|
|
13
13
|
configuration[:scope] = "#{configuration[:scope]}_id".to_sym if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
|
14
14
|
|
15
15
|
field configuration[:column], :type => Integer
|
16
|
-
|
16
|
+
if configuration[:index]
|
17
|
+
if MongoidOrderable.mongoid2?
|
18
|
+
index configuration[:column]
|
19
|
+
else
|
20
|
+
index(configuration[:column] => 1)
|
21
|
+
end
|
22
|
+
end
|
17
23
|
|
18
24
|
case configuration[:scope]
|
19
25
|
when Symbol then
|
20
|
-
scope :orderable_scope, lambda { |document|
|
26
|
+
scope :orderable_scope, lambda { |document|
|
27
|
+
where(configuration[:scope] => document.send(configuration[:scope])) }
|
21
28
|
when Proc then
|
22
29
|
scope :orderable_scope, configuration[:scope]
|
23
30
|
else
|
24
|
-
scope :orderable_scope, lambda { |document| where }
|
31
|
+
scope :orderable_scope, lambda { |document| where({}) }
|
25
32
|
end
|
26
33
|
|
27
34
|
define_method :orderable_column do
|
@@ -86,7 +93,7 @@ module Mongoid::Orderable
|
|
86
93
|
end
|
87
94
|
|
88
95
|
def remove_from_list
|
89
|
-
orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column
|
96
|
+
orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column, -1)
|
90
97
|
end
|
91
98
|
|
92
99
|
private
|
@@ -113,7 +120,7 @@ private
|
|
113
120
|
|
114
121
|
def apply_position target_position
|
115
122
|
if persisted? && !embedded? && orderable_scope_changed?
|
116
|
-
self.class.find(_id).remove_from_list
|
123
|
+
self.class.unscoped.find(_id).remove_from_list
|
117
124
|
self.orderable_position = nil
|
118
125
|
end
|
119
126
|
|
@@ -122,10 +129,10 @@ private
|
|
122
129
|
target_position = target_position_to_position target_position
|
123
130
|
|
124
131
|
unless in_list?
|
125
|
-
orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column
|
132
|
+
orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column, 1)
|
126
133
|
else
|
127
|
-
orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column
|
128
|
-
orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column
|
134
|
+
orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column, 1) if target_position < orderable_position
|
135
|
+
orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column, -1) if target_position > orderable_position
|
129
136
|
end
|
130
137
|
|
131
138
|
self.orderable_position = target_position
|
@@ -148,7 +155,7 @@ private
|
|
148
155
|
|
149
156
|
def bottom_orderable_position
|
150
157
|
@bottom_orderable_position = begin
|
151
|
-
max = orderable_scoped.
|
158
|
+
max = orderable_scoped.distinct(orderable_column).map(&:to_i).max.to_i
|
152
159
|
in_list? ? max : max.next
|
153
160
|
end
|
154
161
|
end
|
data/lib/mongoid_orderable.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
|
+
module MongoidOrderable
|
2
|
+
def self.mongoid2?
|
3
|
+
Mongoid.const_defined? :Contexts
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
1
7
|
require 'mongoid'
|
2
8
|
require 'mongoid_orderable/version'
|
3
|
-
require 'mongoid_orderable/mongoid/contexts/mongo'
|
4
|
-
require 'mongoid_orderable/mongoid/contexts/enumerable'
|
5
|
-
require 'mongoid_orderable/mongoid/criteria'
|
6
|
-
|
7
|
-
require 'mongoid/orderable'
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
if MongoidOrderable.mongoid2?
|
11
|
+
require 'mongoid_orderable/mongoid/contexts/mongo'
|
12
|
+
require 'mongoid_orderable/mongoid/contexts/enumerable'
|
13
|
+
require 'mongoid_orderable/mongoid/criteria'
|
14
|
+
else
|
15
|
+
require 'mongoid_orderable/mongoid/contextual/memory'
|
11
16
|
end
|
17
|
+
|
18
|
+
require 'mongoid/orderable'
|
@@ -2,11 +2,9 @@ module MongoidOrderable #:nodoc:
|
|
2
2
|
module Mongoid #:nodoc:
|
3
3
|
module Contexts #:nodoc:
|
4
4
|
module Enumerable #:nodoc:
|
5
|
-
def inc
|
5
|
+
def inc attribute, value
|
6
6
|
iterate do |doc|
|
7
|
-
|
8
|
-
doc.inc(attribute, value)
|
9
|
-
end
|
7
|
+
doc.inc(attribute, value)
|
10
8
|
end
|
11
9
|
end
|
12
10
|
end
|
@@ -2,10 +2,10 @@ module MongoidOrderable #:nodoc:
|
|
2
2
|
module Mongoid #:nodoc:
|
3
3
|
module Contexts #:nodoc:
|
4
4
|
module Mongo #:nodoc:
|
5
|
-
def inc
|
5
|
+
def inc attribute, value
|
6
6
|
klass.collection.update(
|
7
7
|
selector,
|
8
|
-
{ "$inc" =>
|
8
|
+
{ "$inc" => {attribute => value} },
|
9
9
|
:multi => true,
|
10
10
|
:safe => ::Mongoid.persist_in_safe_mode
|
11
11
|
)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module MongoidOrderable #:nodoc:
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Contextual #:nodoc:
|
4
|
+
module Memory #:nodoc:
|
5
|
+
def inc attribute, value
|
6
|
+
each do |document|
|
7
|
+
document.inc(attribute, value)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Mongoid::Contextual::Memory.send :include, MongoidOrderable::Mongoid::Contextual::Memory
|
data/mongoid_orderable.gemspec
CHANGED
@@ -61,7 +61,7 @@ describe Mongoid::Orderable do
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def positions
|
64
|
-
SimpleOrderable.
|
64
|
+
SimpleOrderable.all.map(&:position).sort
|
65
65
|
end
|
66
66
|
|
67
67
|
it 'should have proper position column' do
|
@@ -70,7 +70,11 @@ describe Mongoid::Orderable do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'should have index on position column' do
|
73
|
-
|
73
|
+
if MongoidOrderable.mongoid2?
|
74
|
+
SimpleOrderable.index_options[:position].should_not be_nil
|
75
|
+
else
|
76
|
+
SimpleOrderable.index_options[{:position => 1}].should_not be_nil
|
77
|
+
end
|
74
78
|
end
|
75
79
|
|
76
80
|
it 'should set proper position while creation' do
|
@@ -261,7 +265,7 @@ describe Mongoid::Orderable do
|
|
261
265
|
end
|
262
266
|
|
263
267
|
def positions
|
264
|
-
EmbedsOrderable.order_by(:position).all.map { |eo| eo.embedded_orderables.
|
268
|
+
EmbedsOrderable.order_by(:position => 1).all.map { |eo| eo.embedded_orderables.map(&:position).sort }
|
265
269
|
end
|
266
270
|
|
267
271
|
it 'should set proper position while creation' do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
require 'bundler'
|
2
2
|
Bundler.require
|
3
3
|
|
4
|
-
|
4
|
+
DATABASE_NAME = "mongoid_#{Process.pid}"
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
if MongoidOrderable.mongoid2?
|
7
|
+
Mongoid.configure do |config|
|
8
|
+
# database = Mongo::Connection.new.db DATABASE_NAME
|
9
|
+
# database.add_user "mongoid", "test"
|
10
|
+
config.master = Mongo::Connection.new.db DATABASE_NAME
|
11
|
+
config.logger = nil
|
12
|
+
end
|
13
|
+
else
|
14
|
+
Mongoid.configure do |config|
|
15
|
+
config.connect_to DATABASE_NAME
|
16
|
+
end
|
11
17
|
end
|
12
18
|
|
13
19
|
RSpec.configure do |config|
|
@@ -22,6 +28,10 @@ RSpec.configure do |config|
|
|
22
28
|
end
|
23
29
|
|
24
30
|
config.after(:suite) do
|
25
|
-
|
31
|
+
if MongoidOrderable.mongoid2?
|
32
|
+
Mongoid.master.connection.drop_database DATABASE_NAME
|
33
|
+
else
|
34
|
+
Mongoid.default_session.drop
|
35
|
+
end
|
26
36
|
end
|
27
37
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_orderable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,27 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: rspec
|
16
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
17
33
|
none: false
|
18
34
|
requirements:
|
19
35
|
- - ! '>='
|
@@ -21,10 +37,15 @@ dependencies:
|
|
21
37
|
version: '0'
|
22
38
|
type: :development
|
23
39
|
prerelease: false
|
24
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
25
46
|
- !ruby/object:Gem::Dependency
|
26
47
|
name: mongoid
|
27
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
28
49
|
none: false
|
29
50
|
requirements:
|
30
51
|
- - ! '>='
|
@@ -32,7 +53,12 @@ dependencies:
|
|
32
53
|
version: '0'
|
33
54
|
type: :runtime
|
34
55
|
prerelease: false
|
35
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
36
62
|
description: Gem allows mongoid model behave as orderable list
|
37
63
|
email:
|
38
64
|
- kinwizard@gmail.com
|
@@ -43,6 +69,7 @@ files:
|
|
43
69
|
- .gitignore
|
44
70
|
- .rspec
|
45
71
|
- .rvmrc
|
72
|
+
- .travis.yml
|
46
73
|
- Gemfile
|
47
74
|
- README.md
|
48
75
|
- Rakefile
|
@@ -50,6 +77,7 @@ files:
|
|
50
77
|
- lib/mongoid_orderable.rb
|
51
78
|
- lib/mongoid_orderable/mongoid/contexts/enumerable.rb
|
52
79
|
- lib/mongoid_orderable/mongoid/contexts/mongo.rb
|
80
|
+
- lib/mongoid_orderable/mongoid/contextual/memory.rb
|
53
81
|
- lib/mongoid_orderable/mongoid/criteria.rb
|
54
82
|
- lib/mongoid_orderable/version.rb
|
55
83
|
- mongoid_orderable.gemspec
|
@@ -75,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
103
|
version: '0'
|
76
104
|
requirements: []
|
77
105
|
rubyforge_project: mongoid_orderable
|
78
|
-
rubygems_version: 1.8.
|
106
|
+
rubygems_version: 1.8.24
|
79
107
|
signing_key:
|
80
108
|
specification_version: 3
|
81
109
|
summary: Acts as list mongoid implementation
|