mongomodel 0.4.7 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,6 +38,7 @@ module MongoModel
38
38
  autoload :MongoOptions, 'mongomodel/support/mongo_options'
39
39
  autoload :MongoOrder, 'mongomodel/support/mongo_order'
40
40
  autoload :MongoOperator, 'mongomodel/support/mongo_operator'
41
+ autoload :Paginator, 'mongomodel/support/paginator'
41
42
  autoload :Scope, 'mongomodel/support/scope'
42
43
  autoload :Types, 'mongomodel/support/types'
43
44
  autoload :Configuration, 'mongomodel/support/configuration'
@@ -0,0 +1,39 @@
1
+ module MongoModel
2
+ class Paginator < Array
3
+ attr_reader :current_page, :per_page, :total_entries
4
+
5
+ def initialize(scope, page, per_page)
6
+ @current_page = page.to_i
7
+ @per_page = per_page.to_i
8
+
9
+ super(scope.offset(offset).limit(per_page))
10
+
11
+ # Try to autodetect total entries
12
+ if total_entries.nil? && size < per_page && (current_page == 1 or size > 0)
13
+ @total_entries = offset + size
14
+ else
15
+ @total_entries = scope.count
16
+ end
17
+ end
18
+
19
+ def total_pages
20
+ total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil
21
+ end
22
+
23
+ def previous_page
24
+ current_page > 1 ? (current_page - 1) : nil
25
+ end
26
+
27
+ def next_page
28
+ current_page < total_pages ? (current_page + 1) : nil
29
+ end
30
+
31
+ def out_of_bounds?
32
+ current_page > total_pages
33
+ end
34
+
35
+ def offset
36
+ (current_page - 1) * per_page
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,3 @@
1
- require 'will_paginate/collection'
2
-
3
1
  module MongoModel
4
2
  class Scope
5
3
  module Pagination
@@ -7,10 +5,7 @@ module MongoModel
7
5
  page = options[:page] || 1
8
6
  per_page = options[:per_page] || klass.per_page
9
7
 
10
- WillPaginate::Collection.create(page, per_page) do |pager|
11
- pager.replace offset(pager.offset).limit(pager.per_page)
12
- pager.total_entries ||= count
13
- end
8
+ Paginator.new(self, page, per_page)
14
9
  end
15
10
  end
16
11
  end
@@ -1,3 +1,3 @@
1
1
  module MongoModel
2
- VERSION = "0.4.7"
2
+ VERSION = "0.4.8"
3
3
  end
@@ -17,7 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency "activesupport", ">= 3.1"
18
18
  s.add_dependency "activemodel", ">= 3.1"
19
19
  s.add_dependency "mongo", "~> 1.5"
20
- s.add_dependency "will_paginate", "~> 2.3.15"
21
20
 
22
21
  s.add_development_dependency "bundler", ">= 1.0.0"
23
22
  s.add_development_dependency "rspec", "~> 2.8"
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ module MongoModel
4
+ describe Paginator do
5
+ let(:entries) { [stub] * 10 }
6
+ let(:scope) { stub(:count => 35, :limit => entries).as_null_object }
7
+ let(:page) { 2 }
8
+ let(:per_page) { 10 }
9
+
10
+ subject { Paginator.new(scope, page, per_page) }
11
+
12
+ it { should be_a_kind_of(Array) }
13
+
14
+ its(:total_entries) { should == 35 }
15
+ its(:total_pages) { should == 4 }
16
+
17
+ its(:current_page) { should == 2 }
18
+ its(:previous_page) { should == 1 }
19
+ its(:next_page) { should == 3 }
20
+ its(:offset) { should == 10 }
21
+
22
+ its(:size) { should == 10 }
23
+
24
+ it { should_not be_out_of_bounds }
25
+
26
+ context "first page" do
27
+ let(:page) { 1 }
28
+
29
+ its(:previous_page) { should be_nil }
30
+ its(:next_page) { should == 2 }
31
+ its(:offset) { should == 0 }
32
+ end
33
+
34
+ context "last page" do
35
+ let(:entries) { [stub] * 5 }
36
+ before { scope.stub(:count => nil) }
37
+
38
+ let(:page) { 4 }
39
+
40
+ its(:previous_page) { should == 3 }
41
+ its(:next_page) { should be_nil }
42
+ end
43
+
44
+ context "no entries" do
45
+ before { scope.stub(:count => 0) }
46
+
47
+ its(:total_pages) { should == 1 }
48
+ end
49
+
50
+ context "out of bounds" do
51
+ let(:page) { 5 }
52
+
53
+ it { should be_out_of_bounds }
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongomodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-22 00:00:00.000000000 Z
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -59,22 +59,6 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.5'
62
- - !ruby/object:Gem::Dependency
63
- name: will_paginate
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ~>
68
- - !ruby/object:Gem::Version
69
- version: 2.3.15
70
- type: :runtime
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: 2.3.15
78
62
  - !ruby/object:Gem::Dependency
79
63
  name: bundler
80
64
  requirement: !ruby/object:Gem::Requirement
@@ -207,6 +191,7 @@ files:
207
191
  - lib/mongomodel/support/mongo_operator.rb
208
192
  - lib/mongomodel/support/mongo_options.rb
209
193
  - lib/mongomodel/support/mongo_order.rb
194
+ - lib/mongomodel/support/paginator.rb
210
195
  - lib/mongomodel/support/reference.rb
211
196
  - lib/mongomodel/support/scope.rb
212
197
  - lib/mongomodel/support/scope/batches.rb
@@ -282,6 +267,7 @@ files:
282
267
  - spec/mongomodel/support/mongo_operator_spec.rb
283
268
  - spec/mongomodel/support/mongo_options_spec.rb
284
269
  - spec/mongomodel/support/mongo_order_spec.rb
270
+ - spec/mongomodel/support/paginator_spec.rb
285
271
  - spec/mongomodel/support/property_spec.rb
286
272
  - spec/mongomodel/support/scope_spec.rb
287
273
  - spec/spec.opts
@@ -310,6 +296,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
310
296
  - - ! '>='
311
297
  - !ruby/object:Gem::Version
312
298
  version: '0'
299
+ segments:
300
+ - 0
301
+ hash: -1652112125246390451
313
302
  required_rubygems_version: !ruby/object:Gem::Requirement
314
303
  none: false
315
304
  requirements:
@@ -323,4 +312,3 @@ signing_key:
323
312
  specification_version: 3
324
313
  summary: MongoDB ORM for Ruby/Rails
325
314
  test_files: []
326
- has_rdoc: