kaminari 0.12.1 → 0.12.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kaminari might be problematic. Click here for more details.

data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ == 0.12.2
2
+
3
+ * Added MongoMapper Support #101 [hamin]
4
+
5
+ * Add first_page? and last_page? to page_scope_methods #51 [holinnn]
6
+
7
+ * Make sure that the paginate helper always returns a String #99 [Draiken]
8
+
9
+ * Don't remove includes scopes from count if they are needed #100 [flop]
10
+
1
11
  == 0.12.1
2
12
 
3
13
  * Slim template support #93 [detrain]
@@ -9,9 +19,6 @@ of Ruby 1.8.7 #91 [Skulli]
9
19
 
10
20
  * _paginate.html.erb isn't rendered with custom theme #97 [danlunde]
11
21
 
12
- * I don't know why but this version seems like working with Haml 3.1.0 #96
13
- [FlyboyArt]
14
-
15
22
  == 0.12.0
16
23
 
17
24
  * General configuration options #41 #62 [javierv, iain]
@@ -19,7 +19,7 @@ No special collection class or anything for the paginated values, instead using
19
19
  As the whole pagination helper is basically just a collection of links and non-links, Kaminari renders each of them through its own partial template inside the Engine. So, you can easily modify their behaviour, style or whatever by overriding partial templates.
20
20
 
21
21
  === ORM & template engine agnostic
22
- Kaminari supports multiple ORMs (ActiveRecord, Mongoid) and multiple template engines (ERB, Haml).
22
+ Kaminari supports multiple ORMs (ActiveRecord, Mongoid, MongoMapper) and multiple template engines (ERB, Haml).
23
23
 
24
24
  === Modern
25
25
  The pagination helper outputs the HTML5 <nav> tag by default. Plus, the helper supports Rails 3 unobtrusive Ajax.
@@ -33,7 +33,9 @@ The pagination helper outputs the HTML5 <nav> tag by default. Plus, the helper s
33
33
 
34
34
  * Haml 3
35
35
 
36
- * Mongoid 2 (beta)
36
+ * Mongoid 2
37
+
38
+ * MongoMapper 0.9
37
39
 
38
40
  == Install
39
41
 
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency 'bundler', ['>= 1.0.0']
26
26
  s.add_development_dependency 'sqlite3', ['>= 0']
27
27
  s.add_development_dependency 'mongoid', ['>= 2']
28
+ s.add_development_dependency 'mongo_mapper', ['>= 0.9']
28
29
  s.add_development_dependency 'rspec', ['>= 0']
29
30
  s.add_development_dependency 'rspec-rails', ['>= 0']
30
31
  s.add_development_dependency 'rr', ['>= 0']
@@ -18,7 +18,8 @@ module Kaminari
18
18
  # * <tt>:remote</tt> - Ajax? (false by default)
19
19
  # * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
20
20
  def paginate(scope, options = {}, &block)
21
- Kaminari::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :num_pages => scope.num_pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false)
21
+ paginator = Kaminari::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :num_pages => scope.num_pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false)
22
+ paginator.to_s
22
23
  end
23
24
  end
24
25
  end
@@ -4,7 +4,7 @@ module Kaminari
4
4
  module InstanceMethods
5
5
  # a workaround for AR 3.0.x that returns 0 for #count when page > 1
6
6
  # if +limit_value+ is specified, load all the records and count them
7
- if Rails.version < '3.1'
7
+ if ActiveRecord::VERSION::STRING < '3.1'
8
8
  def count #:nodoc:
9
9
  limit_value ? length : super
10
10
  end
@@ -12,8 +12,11 @@ module Kaminari
12
12
 
13
13
  def total_count #:nodoc:
14
14
  # #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
15
- c = except(:offset, :limit, :includes, :order).count
15
+ c = except(:offset, :limit, :order)
16
+ # Remove includes only if they are irrelevant
17
+ c = c.except(:includes) unless references_eager_loaded_tables?
16
18
  # .group returns an OrderdHash that responds to #count
19
+ c = c.count
17
20
  c.respond_to?(:count) ? c.count : c
18
21
  end
19
22
  end
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), 'plucky_criteria_methods')
2
+
3
+ module Kaminari
4
+ module MongoMapperExtension
5
+ module Document
6
+ extend ActiveSupport::Concern
7
+ include Kaminari::ConfigurationMethods
8
+
9
+ included do
10
+ # Fetch the values at the specified page number
11
+ # Model.page(5)
12
+ scope :page, Proc.new {|num|
13
+ limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -21,6 +21,16 @@ module Kaminari
21
21
  def current_page
22
22
  (offset_value / limit_value) + 1
23
23
  end
24
+
25
+ # First page of the collection ?
26
+ def first_page?
27
+ current_page == 1
28
+ end
29
+
30
+ # Last page of the collection?
31
+ def last_page?
32
+ current_page >= num_pages
33
+ end
24
34
  end
25
35
  end
26
36
  end
@@ -0,0 +1,18 @@
1
+ module Kaminari
2
+ module PluckyCriteriaMethods
3
+ extend ActiveSupport::Concern
4
+ module InstanceMethods
5
+ def limit_value #:nodoc:
6
+ options[:limit]
7
+ end
8
+
9
+ def offset_value #:nodoc:
10
+ options[:skip]
11
+ end
12
+
13
+ def total_count #:nodoc:
14
+ count
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,7 @@
1
1
  require 'rails'
2
2
  # ensure ORMs are loaded *before* initializing Kaminari
3
3
  begin; require 'mongoid'; rescue LoadError; end
4
+ begin; require 'mongo_mapper'; rescue LoadError; end
4
5
 
5
6
  require File.join(File.dirname(__FILE__), 'config')
6
7
  require File.join(File.dirname(__FILE__), 'helpers/action_view_extension')
@@ -20,10 +21,16 @@ module Kaminari
20
21
  ::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
21
22
  ::Mongoid::Criteria.send :include, Kaminari::MongoidExtension::Criteria
22
23
  end
24
+ if defined? ::MongoMapper
25
+ require File.join(File.dirname(__FILE__), 'models/mongo_mapper_extension')
26
+ ::MongoMapper::Document.send :include, Kaminari::MongoMapperExtension::Document
27
+ ::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
28
+ ::Plucky::Query.send :include, Kaminari::PageScopeMethods
29
+ end
23
30
  require File.join(File.dirname(__FILE__), 'models/array_extension')
24
31
  ActiveSupport.on_load(:action_view) do
25
32
  ::ActionView::Base.send :include, Kaminari::ActionViewExtension
26
33
  end
27
34
  end
28
35
  end
29
- end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Kaminari
2
- VERSION = '0.12.1'
2
+ VERSION = '0.12.2'
3
3
  end
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+ include Kaminari::ActionViewExtension
3
+
4
+ describe 'Kaminari::ActionViewExtension' do
5
+ describe '#paginate' do
6
+ before do
7
+ @author = User.create! :name => 'author'
8
+ @books = 2.times.map { @author.books_authored.create! }
9
+ @books = Book.page(1)
10
+ end
11
+ subject { paginate( @books ) }
12
+ it { should be_a(String) }
13
+
14
+ context "escaping the pagination for javascript" do
15
+ it "should escape for javascript" do
16
+ lambda { escape_javascript( paginate( @books ) ) }.should_not raise_error
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -4,7 +4,11 @@ describe Kaminari::ActiveRecordRelationMethods do
4
4
  describe '#total_count' do
5
5
  before do
6
6
  @author = User.create! :name => 'author'
7
- @books = 2.times.map { @author.books_authored.create! }
7
+ @author2 = User.create! :name => 'author2'
8
+ @author3 = User.create! :name => 'author3'
9
+ @books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
10
+ @books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
11
+ @books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
8
12
  @readers = 4.times.map { User.create! :name => 'reader' }
9
13
  @books.each {|book| book.readers << @readers }
10
14
  end
@@ -14,5 +18,11 @@ describe Kaminari::ActiveRecordRelationMethods do
14
18
  @author.readers.by_read_count.page(1).total_count.should == @readers.size
15
19
  end
16
20
  end
21
+ context "when the scope use conditions on includes" do
22
+ it "should keep includes and successfully count the results" do
23
+ # Only @author and @author2 have books titled with the title00x partern
24
+ User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
25
+ end
26
+ end
17
27
  end
18
28
  end
@@ -0,0 +1,80 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+ require 'mongo_mapper'
3
+ require File.expand_path('../../lib/kaminari/models/mongo_mapper_extension', File.dirname(__FILE__))
4
+
5
+ describe Kaminari::MongoMapperExtension do
6
+ before :all do
7
+ MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
8
+ MongoMapper.database = "kaminari_test"
9
+ class Developer
10
+ include ::MongoMapper::Document
11
+ key :salary, Integer
12
+ end
13
+ end
14
+ before do
15
+ stub(subject).count { 300 } # in order to avoid DB access...
16
+ end
17
+
18
+ describe '#page' do
19
+ context 'page 1' do
20
+ subject { Developer.page(1) }
21
+ it { should be_a Plucky::Query }
22
+ its(:current_page) { should == 1 }
23
+ its(:limit_value) { should == 25 }
24
+ its(:num_pages) { should == 12 }
25
+ it { should skip(0) }
26
+ end
27
+
28
+ context 'page 2' do
29
+ subject { Developer.page 2 }
30
+ it { should be_a Plucky::Query }
31
+ its(:current_page) { should == 2 }
32
+ its(:limit_value) { should == 25 }
33
+ its(:num_pages) { should == 12 }
34
+ it { should skip 25 }
35
+ end
36
+
37
+ context 'page "foobar"' do
38
+ subject { Developer.page 'foobar' }
39
+ it { should be_a Plucky::Query }
40
+ its(:current_page) { should == 1 }
41
+ its(:limit_value) { should == 25 }
42
+ its(:num_pages) { should == 12 }
43
+ it { should skip 0 }
44
+ end
45
+
46
+ context 'with criteria before' do
47
+ it "should have the proper criteria source" do
48
+ Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
49
+ end
50
+
51
+ subject { Developer.where(:salary => 1).page 2 }
52
+ its(:current_page) { should == 2 }
53
+ its(:limit_value) { should == 25 }
54
+ its(:num_pages) { should == 12 }
55
+ it { should skip 25 }
56
+ end
57
+
58
+ context 'with criteria after' do
59
+ it "should have the proper criteria source" do
60
+ Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
61
+ end
62
+
63
+ subject { Developer.page(2).where(:salary => 1) }
64
+ its(:current_page) { should == 2 }
65
+ its(:limit_value) { should == 25 }
66
+ its(:num_pages) { should == 12 }
67
+ it { should skip 25 }
68
+ end
69
+
70
+ end
71
+
72
+ describe '#per' do
73
+ subject { Developer.page(2).per(10) }
74
+ it { should be_a Plucky::Query }
75
+ its(:current_page) { should == 2 }
76
+ its(:limit_value) { should == 10 }
77
+ its(:num_pages) { should == 30 }
78
+ it { should skip 10 }
79
+ end
80
+ end
@@ -94,6 +94,30 @@ describe Kaminari::ActiveRecordExtension do
94
94
  end
95
95
  end
96
96
 
97
+ describe '#first_page?' do
98
+ context 'on first page' do
99
+ subject { User.page(1).per(10) }
100
+ its(:first_page?) { should == true }
101
+ end
102
+
103
+ context 'not on first page' do
104
+ subject { User.page(5).per(10) }
105
+ its(:first_page?) { should == false }
106
+ end
107
+ end
108
+
109
+ describe '#last_page?' do
110
+ context 'on last page' do
111
+ subject { User.page(10).per(10) }
112
+ its(:last_page?) { should == true }
113
+ end
114
+
115
+ context 'not on last page' do
116
+ subject { User.page(1).per(10) }
117
+ its(:last_page?) { should == false }
118
+ end
119
+ end
120
+
97
121
  describe '#count' do
98
122
  context 'page 1' do
99
123
  subject { User.page }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaminari
3
3
  version: !ruby/object:Gem::Version
4
- hash: 45
4
+ hash: 43
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 12
9
- - 1
10
- version: 0.12.1
9
+ - 2
10
+ version: 0.12.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Akira Matsuda
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-25 00:00:00 Z
18
+ date: 2011-04-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rails
@@ -78,21 +78,22 @@ dependencies:
78
78
  type: :development
79
79
  version_requirements: *id004
80
80
  - !ruby/object:Gem::Dependency
81
- name: rspec
81
+ name: mongo_mapper
82
82
  prerelease: false
83
83
  requirement: &id005 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
- hash: 3
88
+ hash: 25
89
89
  segments:
90
90
  - 0
91
- version: "0"
91
+ - 9
92
+ version: "0.9"
92
93
  type: :development
93
94
  version_requirements: *id005
94
95
  - !ruby/object:Gem::Dependency
95
- name: rspec-rails
96
+ name: rspec
96
97
  prerelease: false
97
98
  requirement: &id006 !ruby/object:Gem::Requirement
98
99
  none: false
@@ -106,7 +107,7 @@ dependencies:
106
107
  type: :development
107
108
  version_requirements: *id006
108
109
  - !ruby/object:Gem::Dependency
109
- name: rr
110
+ name: rspec-rails
110
111
  prerelease: false
111
112
  requirement: &id007 !ruby/object:Gem::Requirement
112
113
  none: false
@@ -120,7 +121,7 @@ dependencies:
120
121
  type: :development
121
122
  version_requirements: *id007
122
123
  - !ruby/object:Gem::Dependency
123
- name: steak
124
+ name: rr
124
125
  prerelease: false
125
126
  requirement: &id008 !ruby/object:Gem::Requirement
126
127
  none: false
@@ -134,7 +135,7 @@ dependencies:
134
135
  type: :development
135
136
  version_requirements: *id008
136
137
  - !ruby/object:Gem::Dependency
137
- name: capybara
138
+ name: steak
138
139
  prerelease: false
139
140
  requirement: &id009 !ruby/object:Gem::Requirement
140
141
  none: false
@@ -148,7 +149,7 @@ dependencies:
148
149
  type: :development
149
150
  version_requirements: *id009
150
151
  - !ruby/object:Gem::Dependency
151
- name: database_cleaner
152
+ name: capybara
152
153
  prerelease: false
153
154
  requirement: &id010 !ruby/object:Gem::Requirement
154
155
  none: false
@@ -161,6 +162,20 @@ dependencies:
161
162
  version: "0"
162
163
  type: :development
163
164
  version_requirements: *id010
165
+ - !ruby/object:Gem::Dependency
166
+ name: database_cleaner
167
+ prerelease: false
168
+ requirement: &id011 !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ hash: 3
174
+ segments:
175
+ - 0
176
+ version: "0"
177
+ type: :development
178
+ version_requirements: *id011
164
179
  description: Kaminari is a Scope & Engine based, clean, powerful, customizable and sophisticated paginator for Rails 3
165
180
  email:
166
181
  - ronnie@dio.jp
@@ -216,9 +231,11 @@ files:
216
231
  - lib/kaminari/models/active_record_relation_methods.rb
217
232
  - lib/kaminari/models/array_extension.rb
218
233
  - lib/kaminari/models/configuration_methods.rb
234
+ - lib/kaminari/models/mongo_mapper_extension.rb
219
235
  - lib/kaminari/models/mongoid_criteria_methods.rb
220
236
  - lib/kaminari/models/mongoid_extension.rb
221
237
  - lib/kaminari/models/page_scope_methods.rb
238
+ - lib/kaminari/models/plucky_criteria_methods.rb
222
239
  - lib/kaminari/railtie.rb
223
240
  - lib/kaminari/version.rb
224
241
  - spec/acceptance/acceptance_helper.rb
@@ -227,11 +244,13 @@ files:
227
244
  - spec/acceptance/users_spec.rb
228
245
  - spec/config/config_spec.rb
229
246
  - spec/fake_app.rb
247
+ - spec/helpers/action_view_extension_spec.rb
230
248
  - spec/helpers/helpers_spec.rb
231
249
  - spec/helpers/tags_spec.rb
232
250
  - spec/models/active_record_relation_methods_spec.rb
233
251
  - spec/models/array_spec.rb
234
252
  - spec/models/default_per_page_spec.rb
253
+ - spec/models/mongo_mapper_spec.rb
235
254
  - spec/models/mongoid_spec.rb
236
255
  - spec/models/scopes_spec.rb
237
256
  - spec/spec_helper.rb
@@ -277,11 +296,13 @@ test_files:
277
296
  - spec/acceptance/users_spec.rb
278
297
  - spec/config/config_spec.rb
279
298
  - spec/fake_app.rb
299
+ - spec/helpers/action_view_extension_spec.rb
280
300
  - spec/helpers/helpers_spec.rb
281
301
  - spec/helpers/tags_spec.rb
282
302
  - spec/models/active_record_relation_methods_spec.rb
283
303
  - spec/models/array_spec.rb
284
304
  - spec/models/default_per_page_spec.rb
305
+ - spec/models/mongo_mapper_spec.rb
285
306
  - spec/models/mongoid_spec.rb
286
307
  - spec/models/scopes_spec.rb
287
308
  - spec/spec_helper.rb