mongoid-pagination 0.1.0

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.
@@ -0,0 +1,3 @@
1
+ .rspec
2
+ Gemfile.lock
3
+ pkg
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - ree
5
+ - ruby-head
6
+ - rbx
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem 'bson_ext'
6
+ gem 'rake'
7
+
8
+ group :test do
9
+ gem 'rspec'
10
+ end
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2012 Alex Sharp
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
@@ -0,0 +1,15 @@
1
+ ## What
2
+
3
+ Adds pagination scopes to your mongoid models.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ class Person
9
+ include Mongoid::Document
10
+ include Mongoid::Pagination
11
+ end
12
+
13
+ Person.paginate(:page => 2, :limit => 25) # limit and offset
14
+ Person.per_page(25) # just does a limit
15
+ ```
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require 'mongoid'
2
+ require 'mongoid/pagination'
@@ -0,0 +1,38 @@
1
+ module Mongoid
2
+ module Pagination
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ # Paginate the results
8
+ #
9
+ # @param [Hash] opts
10
+ # @option [Integer] :page (default: 1)
11
+ # @option [Integer] :limit (default: 25)
12
+ #
13
+ # @return [Mongoid::Criteria]
14
+ def paginate(opts = {})
15
+ return criteria if opts[:limit].nil? and opts[:page].nil?
16
+
17
+ limit = (opts[:limit] || 25).to_i
18
+ page = (opts[:page] || 1).to_i
19
+
20
+ if page > 1
21
+ offset = (page - 1) * limit
22
+ else
23
+ offset = 0
24
+ end
25
+
26
+ per_page(limit).offset(offset)
27
+ end
28
+
29
+ # Limit the result set
30
+ #
31
+ # @param [Integer] page_limit the max number of results to return
32
+ # @return [Mongoid::Criteria]
33
+ def per_page(page_limit = 25)
34
+ limit(page_limit.to_i)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Pagination
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid/pagination/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid-pagination"
7
+ s.version = Mongoid::Pagination::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Alex Sharp"]
10
+ s.email = ["ajsharp@gmail.com"]
11
+ s.homepage = "https://github.com/ajsharp/mongoid-pagination"
12
+ s.summary = %q{A simple pagination module for Mongoid}
13
+ s.description = %q{A simple pagination module for Mongoid}
14
+
15
+ s.rubyforge_project = s.name
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'mongoid'
23
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Pagination do
4
+ class Person
5
+ include Mongoid::Document
6
+ include Mongoid::Pagination
7
+ end
8
+
9
+ describe ".paginate" do
10
+ let!(:one) { Person.create! }
11
+ let!(:two) { Person.create! }
12
+ let!(:three) { Person.create! }
13
+ let!(:four) { Person.create! }
14
+
15
+ context "parameter defaults and massaging" do
16
+ describe "when no params are passed in" do
17
+ subject { Person.paginate }
18
+
19
+ it "does not set the skip param by default" do
20
+ subject.options[:skip].should be_nil
21
+ end
22
+
23
+ it "defaults the limit param to 25" do
24
+ subject.options[:limit].should be_nil
25
+ end
26
+
27
+ it "returns the criteria unmodified if the limit param is not passed in" do
28
+ criteria = Person.where(:name => 'someone')
29
+ expect {
30
+ criteria.paginate
31
+ }.not_to change { criteria.options }
32
+ end
33
+ end
34
+
35
+ describe "when passed a page param but no limit" do
36
+ subject { Person.paginate(:page => 1) }
37
+
38
+ it "defaults the limit to 25" do
39
+ subject.options[:limit].should == 25
40
+ end
41
+
42
+ it "sets the offset to 0" do
43
+ subject.options[:skip].should == 0
44
+ end
45
+ end
46
+
47
+ describe "when passed a limit param but no page" do
48
+ subject { Person.paginate(:limit => 100) }
49
+
50
+ it "defaults the page to 0" do
51
+ subject.options[:skip].should == 0
52
+ end
53
+
54
+ it "sets the limit to 100" do
55
+ subject.options[:limit].should == 100
56
+ end
57
+ end
58
+
59
+ it "sets the skip param to 0 if passed 0" do
60
+ Person.paginate(:page => 0).options[:skip].should == 0
61
+ end
62
+
63
+ it "sets the skip param to 0 if passed a string of 0" do
64
+ Person.paginate(:page => '0').options[:skip].should == 0
65
+ end
66
+
67
+ it "sets the skip param to 0 if the passed a string of 1" do
68
+ Person.paginate(:page => '1').options[:skip].should == 0
69
+ end
70
+
71
+ it "limits when passed a string param" do
72
+ Person.paginate(:limit => '1').to_a.size.should == 1
73
+ end
74
+
75
+ it "correctly sets criteria options" do
76
+ Person.paginate(:limit => 10, :page => 3).options.should == {:limit => 10, :skip => 20}
77
+ end
78
+ end
79
+
80
+ context "results" do
81
+ it "paginates correctly on the first page" do
82
+ Person.paginate(:page => 1, :limit => 2).to_a.should == [one, two]
83
+ end
84
+
85
+ it "paginates correctly on the second page" do
86
+ Person.paginate(:page => 2, :limit => 2).to_a.should == [three, four]
87
+ end
88
+ end
89
+
90
+ it "paginates the result set based on the limit and page params" do
91
+ # Person.
92
+ end
93
+ end
94
+
95
+ describe ".per_page" do
96
+ before do
97
+ 2.times { Person.create! }
98
+ end
99
+
100
+ it "limits the results by the per page param" do
101
+ Person.per_page(1).to_a.size.should == 1
102
+ end
103
+
104
+ it "works for string params" do
105
+ Person.per_page('1').to_a.size.should == 1
106
+ end
107
+
108
+ it "defaults to 25" do
109
+ Person.per_page.options[:limit].should == 25
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.setup :default, :test
5
+ Bundler.require :default, :test
6
+
7
+ $:.push(File.expand_path(File.dirname(__FILE__)))
8
+ require './lib/mongoid-pagination'
9
+
10
+ Mongoid.database = Mongo::Connection.new.db('mongoid-pagination_test')
11
+
12
+ RSpec.configure do |c|
13
+ c.before(:each) do
14
+ Mongoid.database.collections.each { |c| c.drop unless c.name =~ /system\.indexes$/}
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-pagination
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Sharp
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: &70283948946940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70283948946940
25
+ description: A simple pagination module for Mongoid
26
+ email:
27
+ - ajsharp@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .travis.yml
34
+ - Gemfile
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - lib/mongoid-pagination.rb
39
+ - lib/mongoid/pagination.rb
40
+ - lib/mongoid/pagination/version.rb
41
+ - mongoid-pagination.gemspec
42
+ - spec/pagination_spec.rb
43
+ - spec/spec_helper.rb
44
+ homepage: https://github.com/ajsharp/mongoid-pagination
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project: mongoid-pagination
64
+ rubygems_version: 1.8.15
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: A simple pagination module for Mongoid
68
+ test_files:
69
+ - spec/pagination_spec.rb
70
+ - spec/spec_helper.rb