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 CHANGED
@@ -1 +1 @@
1
- rvm use 1.9.2@mongoid_orderable --create
1
+ rvm use 1.9.3@mongoid_orderable --create
@@ -0,0 +1,11 @@
1
+ script: "bundle exec rake spec"
2
+
3
+ rvm:
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - ruby-head
7
+ #- rbx-19mode
8
+
9
+ env:
10
+ - MONGOID_VERSION=2
11
+ - MONGOID_VERSION='~> 3.0.0.rc'
data/Gemfile CHANGED
@@ -2,3 +2,10 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in mongoid_orderable.gemspec
4
4
  gemspec
5
+
6
+ case version = ENV['MONGOID_VERSION'] || "~> 3.0.0.rc"
7
+ when /2/
8
+ gem "mongoid", "~> 2.4.0"
9
+ else
10
+ gem "mongoid", version
11
+ end
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
 
@@ -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
- index configuration[:column] if configuration[:index]
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| where(configuration[:scope] => document.send(configuration[:scope])) }
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 => -1)
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 => 1)
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 => 1) if target_position < orderable_position
128
- orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column => -1) if target_position > orderable_position
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.max(orderable_column).to_i
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
@@ -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
- module MongoidOrderable
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 attributes = {}
5
+ def inc attribute, value
6
6
  iterate do |doc|
7
- attributes.each do |attribute, value|
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 attributes = {}
5
+ def inc attribute, value
6
6
  klass.collection.update(
7
7
  selector,
8
- { "$inc" => attributes },
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
@@ -1,3 +1,3 @@
1
1
  module MongoidOrderable
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  # specify any dependencies here; for example:
22
+ s.add_development_dependency "rake"
22
23
  s.add_development_dependency "rspec"
23
24
  s.add_runtime_dependency "mongoid"
24
25
  end
@@ -61,7 +61,7 @@ describe Mongoid::Orderable do
61
61
  end
62
62
 
63
63
  def positions
64
- SimpleOrderable.order_by(:position, :asc).map(&:position)
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
- SimpleOrderable.index_options[:position].should_not be_nil
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.order_by(:position).map(&:position) }
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
@@ -1,13 +1,19 @@
1
1
  require 'bundler'
2
2
  Bundler.require
3
3
 
4
- DATABASE_ID = Process.pid
4
+ DATABASE_NAME = "mongoid_#{Process.pid}"
5
5
 
6
- Mongoid.configure do |config|
7
- database = Mongo::Connection.new.db("mongoid_#{DATABASE_ID}")
8
- database.add_user("mongoid", "test")
9
- config.master = database
10
- config.logger = nil
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
- Mongoid.master.connection.drop_database("mongoid_#{DATABASE_ID}")
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.0.0
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-02-14 00:00:00.000000000Z
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: &70092185456800 !ruby/object:Gem::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: *70092185456800
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: &70092185456320 !ruby/object:Gem::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: *70092185456320
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.10
106
+ rubygems_version: 1.8.24
79
107
  signing_key:
80
108
  specification_version: 3
81
109
  summary: Acts as list mongoid implementation