will_paginate_couchrest 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 alex
1
+ Copyright (c) 2009 Sam Lown @ Autofiscal S.L.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -27,8 +27,18 @@ Automatically generate views with an extra reduce method used to generate the to
27
27
 
28
28
  end
29
29
 
30
- @users = User.paginate_by_nickname :page => 1, :per_page => 10, :key => 'Fred'
30
+ # Parameter :per_page must be provided or an error will be raised.
31
+ # Parameter :page will assume 1 if nil.
32
+ @users = User.paginate_by_nickname :page => 1, :per_page => 10, :key => 'Lorena'
31
33
 
34
+ # For pagination on all documents
35
+ @users = User.paginate_all :per_page => 10
36
+
37
+ # Standard views are created at the same time
38
+ @users = User.by_nickname :key => 'Lorena'
39
+
40
+
41
+ # Use standard will_paginate method or redefine as you see fit
32
42
  will_paginate @users
33
43
 
34
44
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -10,10 +10,10 @@ module CouchRest
10
10
  end
11
11
 
12
12
  module ClassMethods
13
- # Define a CouchDB paginate view. The name of the view will be the concatenation
13
+ # Define a CouchDB will paginate view. The name of the view will be the concatenation
14
14
  # of <tt>paginate_by</tt> and the keys joined by <tt>_and_</tt>
15
- #
16
- # ==== Example views:
15
+ #
16
+ # ==== Example paginated views:
17
17
  #
18
18
  # class Post
19
19
  # # view with default options
@@ -64,7 +64,6 @@ module CouchRest
64
64
  #
65
65
  # For further details on <tt>view_by</tt>'s other options, please see the
66
66
  # standard documentation.
67
-
68
67
  def paginated_view_by(*keys)
69
68
 
70
69
  # Prepare the Traditional view
@@ -101,9 +100,18 @@ module CouchRest
101
100
  paginated_view('#{view_name}', options)
102
101
  end
103
102
  RUBY_EVAL
104
-
105
103
  end
106
104
 
105
+ ##
106
+ # Generate a Will Paginate collection from all the available
107
+ # documents stored with a matching couchrest-type.
108
+ #
109
+ # Requires no declaration as the 'all' view is built into couchrest
110
+ # Extended Documents.
111
+ #
112
+ def paginate_all(options = {})
113
+ paginated_view(:all, options)
114
+ end
107
115
 
108
116
  protected
109
117
 
@@ -117,14 +125,21 @@ module CouchRest
117
125
 
118
126
  ::WillPaginate::Collection.create( options[:page], options[:per_page] ) do |pager|
119
127
  # perform view count first (should create designs if missing)
120
- total = view( view_name, options.update(:reduce => true) )['rows'].pop
121
- pager.total_entries = total ? total['value'] : 0
122
- results = paginate(
123
- options.merge(
124
- :design_doc => self.to_s, :view_name => view_name,
125
- :include_docs => true, :reduce => false
126
- )
128
+ if view_name.to_sym == :all
129
+ pager.total_entries = count()
130
+ else
131
+ total = view( view_name, options.update(:reduce => true) )['rows'].pop
132
+ pager.total_entries = total ? total['value'] : 0
133
+ end
134
+ p_options = options.merge(
135
+ :design_doc => self.to_s, :view_name => view_name,
136
+ :include_docs => true
127
137
  )
138
+ # Only provide the reduce parameter when necessary. This is when the view has
139
+ # been set with a reduce method and requires the reduce boolean parameter
140
+ # to be either true or false on all requests.
141
+ p_options[:reduce] = false unless view_name.to_sym == :all
142
+ results = paginate(p_options)
128
143
  pager.replace( results )
129
144
  end
130
145
  end
@@ -26,6 +26,22 @@ describe CouchRest::Mixins::WillPaginate do
26
26
  # @some_doc.stub(:id).and_return(123)
27
27
  end
28
28
 
29
+ it "should accept request when no results" do
30
+ docs = SomeDoc.paginate_by_name(:per_page => 5)
31
+ docs.total_entries.should eql(0)
32
+ end
33
+
34
+ it "should accept request without page" do
35
+ docs = nil
36
+ lambda { docs = SomeDoc.paginate_by_name(:per_page => 5) }.should_not raise_error
37
+ docs.current_page.should eql(1)
38
+ end
39
+
40
+ it "should throw an exception when missing per_page parameter" do
41
+ lambda { SomeDoc.paginate_by_name() }.should raise_error
42
+ end
43
+
44
+
29
45
  describe "performing pagination with lots of documents" do
30
46
 
31
47
  before(:each) do
@@ -38,7 +54,7 @@ describe CouchRest::Mixins::WillPaginate do
38
54
 
39
55
  it "should produce a will paginate collection" do
40
56
  docs = SomeDoc.paginate_by_name( :page => 1, :per_page => 5 )
41
- docs.class.should eql(::WillPaginate::Collection)
57
+ docs.should be_a_kind_of(::WillPaginate::Collection)
42
58
  docs.total_pages.should eql(4)
43
59
  docs.first.name.should eql('document 00')
44
60
  docs.length.should eql(5)
@@ -52,8 +68,13 @@ describe CouchRest::Mixins::WillPaginate do
52
68
  docs.last.name.should eql('document 09')
53
69
  end
54
70
 
55
-
56
-
71
+ it "should perform paginate on all entries" do
72
+ docs = SomeDoc.paginate_all(:page => 1, :per_page => 5)
73
+ docs.first.class.should eql(SomeDoc)
74
+ docs.total_pages.should eql(4)
75
+ docs.total_entries.should eql(20)
76
+ docs.length.should eql(5)
77
+ end
57
78
 
58
79
  end
59
80
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: will_paginate_couchrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - samlown
@@ -9,29 +14,36 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-04 00:00:00 +00:00
17
+ date: 2010-03-05 00:00:00 +01:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: couchrest
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 35
23
30
  version: "0.35"
24
- version:
31
+ type: :runtime
32
+ version_requirements: *id001
25
33
  - !ruby/object:Gem::Dependency
26
34
  name: rspec
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
30
37
  requirements:
31
38
  - - ">="
32
39
  - !ruby/object:Gem::Version
40
+ segments:
41
+ - 1
42
+ - 2
43
+ - 9
33
44
  version: 1.2.9
34
- version:
45
+ type: :development
46
+ version_requirements: *id002
35
47
  description: generate views specifically with support for using will_paginate with them
36
48
  email: me@samlown.com
37
49
  executables: []
@@ -64,21 +76,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
76
  requirements:
65
77
  - - ">="
66
78
  - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
67
81
  version: "0"
68
- version:
69
82
  required_rubygems_version: !ruby/object:Gem::Requirement
70
83
  requirements:
71
84
  - - ">="
72
85
  - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
73
88
  version: "0"
74
- version:
75
89
  requirements: []
76
90
 
77
91
  rubyforge_project:
78
- rubygems_version: 1.3.5
92
+ rubygems_version: 1.3.6
79
93
  signing_key:
80
94
  specification_version: 3
81
95
  summary: adds will-paginate support to CouchRest
82
96
  test_files:
83
- - spec/will_paginate_couchrest_spec.rb
84
97
  - spec/spec_helper.rb
98
+ - spec/will_paginate_couchrest_spec.rb