kaminari 0.12.4 → 0.13.0
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/.travis.yml +6 -0
- data/CHANGELOG +62 -0
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.rdoc +81 -14
- data/kaminari.gemspec +18 -5
- data/lib/generators/kaminari/templates/kaminari_config.rb +1 -0
- data/lib/generators/kaminari/views_generator.rb +1 -1
- data/lib/kaminari.rb +73 -2
- data/lib/kaminari/config.rb +3 -1
- data/lib/kaminari/helpers/action_view_extension.rb +74 -20
- data/lib/kaminari/helpers/paginator.rb +23 -5
- data/lib/kaminari/helpers/sinatra_helpers.rb +119 -0
- data/lib/kaminari/hooks.rb +35 -0
- data/lib/kaminari/models/active_record_extension.rb +12 -15
- data/lib/kaminari/models/active_record_model_extension.rb +20 -0
- data/lib/kaminari/models/active_record_relation_methods.rb +23 -13
- data/lib/kaminari/models/array_extension.rb +33 -15
- data/lib/kaminari/models/data_mapper_collection_methods.rb +15 -0
- data/lib/kaminari/models/data_mapper_extension.rb +48 -0
- data/lib/kaminari/models/mongo_mapper_extension.rb +3 -3
- data/lib/kaminari/models/mongoid_criteria_methods.rb +15 -10
- data/lib/kaminari/models/mongoid_extension.rb +7 -5
- data/lib/kaminari/models/page_scope_methods.rb +27 -26
- data/lib/kaminari/models/plucky_criteria_methods.rb +9 -12
- data/lib/kaminari/railtie.rb +2 -31
- data/lib/kaminari/sinatra.rb +13 -0
- data/lib/kaminari/version.rb +1 -1
- data/spec/config/config_spec.rb +1 -1
- data/spec/fake_app.rb +4 -0
- data/spec/fake_gem.rb +6 -0
- data/spec/helpers/action_view_extension_spec.rb +105 -10
- data/spec/helpers/helpers_spec.rb +1 -1
- data/spec/helpers/sinatra_helpers_spec.rb +174 -0
- data/spec/helpers/tags_spec.rb +1 -1
- data/spec/models/active_record_relation_methods_spec.rb +1 -1
- data/spec/models/array_spec.rb +17 -1
- data/spec/models/data_mapper_spec.rb +181 -0
- data/spec/models/default_per_page_spec.rb +1 -1
- data/spec/models/mongo_mapper_spec.rb +14 -11
- data/spec/models/mongoid_spec.rb +63 -6
- data/spec/models/scopes_spec.rb +154 -140
- data/spec/{acceptance → requests}/users_spec.rb +3 -2
- data/spec/spec_helper.rb +3 -1
- data/spec/spec_helper_for_sinatra.rb +13 -0
- data/spec/support/matchers.rb +6 -0
- metadata +263 -170
- data/spec/acceptance/acceptance_helper.rb +0 -5
- data/spec/acceptance/support/helpers.rb +0 -5
- data/spec/acceptance/support/paths.rb +0 -9
data/.travis.yml
ADDED
data/CHANGELOG
CHANGED
@@ -1,3 +1,65 @@
|
|
1
|
+
== 0.13.0
|
2
|
+
|
3
|
+
* Rails 3.2 ready! #180 [slbug]
|
4
|
+
|
5
|
+
* DataMapper support! #149 [NoICE, Ragmaanir]
|
6
|
+
|
7
|
+
* Sinatra & Padrino support! #179 [udzura, mlightner, aereal]
|
8
|
+
|
9
|
+
* Added mongoid embedded documents support! #155 [yuki24]
|
10
|
+
|
11
|
+
* Added `each_relevant_page` that only visits pages in the inner or outer
|
12
|
+
windows #154 [cbeer]
|
13
|
+
Performance improved, particularly with very large number of pages.
|
14
|
+
|
15
|
+
* Memoize count for AR when calling `total_count` #138 [sarmiena]
|
16
|
+
Increases performance for large datasets.
|
17
|
+
|
18
|
+
* Added `page_entries_info` view helper #140 [jeffreyiacono]
|
19
|
+
Example:
|
20
|
+
<%= page_entries_info @posts %>
|
21
|
+
#=> Displaying posts 6 - 10 of 26 in total
|
22
|
+
|
23
|
+
* Added `link_to_next_page` helper method that simply links to the next page
|
24
|
+
Example:
|
25
|
+
<%= link_to_next_page @posts, 'More' %>
|
26
|
+
#=> <a href="/posts?page=7" rel="next">More</a>
|
27
|
+
|
28
|
+
* Let one override the `rel` attribute for 'link_to_next_page` helper #177
|
29
|
+
[webmat]
|
30
|
+
|
31
|
+
* Added `total_count` param for PaginatableArray. Useful for when working with
|
32
|
+
RSolr #141 [samdalton]
|
33
|
+
|
34
|
+
* Changed `Kaminari.paginate_array` API to take a Hash `options`
|
35
|
+
And specifying :limit & :offset immediately builds a pagination ready object
|
36
|
+
Example:
|
37
|
+
# the following two are equivalent. Use whichever you like
|
38
|
+
Kaminari.paginate_array((1..100).to_a, limit: 10, offset: 10)
|
39
|
+
Kaminari.paginate_array((1..100).to_a).page(2).per(10)
|
40
|
+
|
41
|
+
* Added `padding` method to skip an arbitrary record count #60 [aaronjensen]
|
42
|
+
Example:
|
43
|
+
User.page(2).per(10).padding(3) # this will return users 14..23
|
44
|
+
|
45
|
+
* Made the pagination method name (defaulted to `page`) configurable #57, #162
|
46
|
+
Example:
|
47
|
+
# you can use the config file and its generator for this
|
48
|
+
Kaminari.config.page_method_name = :paging
|
49
|
+
Article.paging(3).per(30)
|
50
|
+
|
51
|
+
* Only add extensions to direct descendents of ActiveRecord::Base #108
|
52
|
+
[seejohnrun]
|
53
|
+
|
54
|
+
* AR models that were subclassed before Kaminari::ActiveRecordExtension is
|
55
|
+
included pick up the extensions #119 [pivotal-casebook]
|
56
|
+
|
57
|
+
* Avoid overwriting AR::Base inherited method #165 [briandmcnabb]
|
58
|
+
|
59
|
+
* Stopped depending on Rails gem #159 [alsemyonov]
|
60
|
+
|
61
|
+
* introduced Travis CI #181 [hsbt]
|
62
|
+
|
1
63
|
== 0.12.4
|
2
64
|
|
3
65
|
* Support for config.param_name as lambda #102 [ajrkerr]
|
data/{LICENSE.txt → MIT-LICENSE}
RENAMED
File without changes
|
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Kaminari
|
2
2
|
|
3
|
-
A Scope & Engine based, clean, powerful, customizable and sophisticated paginator for
|
3
|
+
A Scope & Engine based, clean, powerful, customizable and sophisticated paginator for modern web app frameworks and ORMs
|
4
4
|
|
5
5
|
|
6
6
|
== Features
|
@@ -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, MongoMapper) and multiple template engines (ERB, Haml).
|
22
|
+
Kaminari supports multiple ORMs (ActiveRecord, Mongoid, MongoMapper) multiple web frameworks (Rails, Sinatra), 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.
|
@@ -27,15 +27,17 @@ The pagination helper outputs the HTML5 <nav> tag by default. Plus, the helper s
|
|
27
27
|
|
28
28
|
== Supported versions
|
29
29
|
|
30
|
-
* Ruby 1.8.7, 1.9.2, 1.9.3 (trunk)
|
30
|
+
* Ruby 1.8.7, 1.9.2, 1.9.3, 2.0 (trunk)
|
31
31
|
|
32
|
-
* Rails 3.0.x, 3.1 (edge)
|
32
|
+
* Rails 3.0.x, 3.1, 3.2, 4.0 (edge)
|
33
33
|
|
34
|
-
* Haml 3
|
34
|
+
* Haml 3+
|
35
35
|
|
36
|
-
* Mongoid 2
|
36
|
+
* Mongoid 2+
|
37
37
|
|
38
|
-
* MongoMapper 0.9
|
38
|
+
* MongoMapper 0.9+
|
39
|
+
|
40
|
+
* DataMapper 1.1.0+
|
39
41
|
|
40
42
|
== Install
|
41
43
|
|
@@ -61,6 +63,12 @@ Then bundle:
|
|
61
63
|
User.page(7).per(50)
|
62
64
|
Note that the +per+ scope is not directly defined on the models but is just a method defined on the page scope. This is absolutely reasonable because you will never actually use +per_page+ without specifying the +page+ number.
|
63
65
|
|
66
|
+
* the +padding+ scope
|
67
|
+
|
68
|
+
Occasionally you need to padding a number of records that is not a multiple of the page size.
|
69
|
+
User.page(7).per(50).padding(3)
|
70
|
+
Note that the +padding+ scope also is not directly defined on the models.
|
71
|
+
|
64
72
|
=== General configuration options
|
65
73
|
|
66
74
|
You can configure the following default values by overriding these values using <tt>Kaminari.configure</tt> method.
|
@@ -69,11 +77,18 @@ You can configure the following default values by overriding these values using
|
|
69
77
|
outer_window # 0 by default
|
70
78
|
left # 0 by default
|
71
79
|
right # 0 by default
|
80
|
+
page_method_name # :page by default
|
81
|
+
param_name # :page by default
|
72
82
|
|
73
|
-
There's a handy generator that generates the default configuration file into config/
|
83
|
+
There's a handy generator that generates the default configuration file into config/initializers directory.
|
74
84
|
Run the following generator command, then edit the generated file.
|
75
85
|
% rails g kaminari:config
|
76
86
|
|
87
|
+
* changing +page_method_name+
|
88
|
+
|
89
|
+
You can change the method name `page` to `bonzo` or `plant` or whatever you like, in order to play nice with existing `page` method or association or scope or any other plugin that defines `page` method on your models.
|
90
|
+
|
91
|
+
|
77
92
|
=== Configuring default +per_page+ value for each model
|
78
93
|
|
79
94
|
* +paginates_per+
|
@@ -99,7 +114,12 @@ Run the following generator command, then edit the generated file.
|
|
99
114
|
|
100
115
|
This will render several <tt>?page=N</tt> pagination links surrounded by an HTML5 <+nav+> tag.
|
101
116
|
|
102
|
-
===
|
117
|
+
=== Helpers
|
118
|
+
|
119
|
+
* the +paginate+ helper method
|
120
|
+
|
121
|
+
<%= paginate @users %>
|
122
|
+
This would output several pagination links such as <tt>« First ‹ Prev ... 2 3 4 5 6 7 8 9 10 ... Next › Last »</tt>
|
103
123
|
|
104
124
|
* specifing the "inner window" size (4 by default)
|
105
125
|
|
@@ -131,16 +151,28 @@ Run the following generator command, then edit the generated file.
|
|
131
151
|
<%= paginate @users, :remote => true %>
|
132
152
|
This would add <tt>data-remote="true"</tt> to all the links inside.
|
133
153
|
|
154
|
+
* the +link_to_next_page+ helper method
|
155
|
+
|
156
|
+
<%= link_to_next_page @items, 'Next Page' %>
|
157
|
+
This simply renders a link to the next page. This would be helpful for creating "Twitter like" pagination feature.
|
158
|
+
|
159
|
+
* the +page_entries_info+ helper method
|
160
|
+
|
161
|
+
<%= page_entries_info @users %>
|
162
|
+
This renders a helpful message with numbers of displayed vs. total entries.
|
163
|
+
|
134
164
|
=== I18n and labels
|
135
165
|
|
136
|
-
The default labels for 'previous', '...' and 'next' are stored in the I18n yaml inside the engine, and rendered through I18n API. You can switch the label value per I18n.locale for your internationalized application.
|
166
|
+
The default labels for 'first', 'last', 'previous', '...' and 'next' are stored in the I18n yaml inside the engine, and rendered through I18n API. You can switch the label value per I18n.locale for your internationalized application.
|
137
167
|
Keys and the default values are the following. You can override them by adding to a YAML file in your <tt>Rails.root/config/locales</tt> directory.
|
138
168
|
|
139
169
|
en:
|
140
170
|
views:
|
141
171
|
pagination:
|
142
|
-
|
143
|
-
|
172
|
+
first: "« First"
|
173
|
+
last: "Last »"
|
174
|
+
previous: "‹ Prev"
|
175
|
+
next: "Next ›"
|
144
176
|
truncate: "..."
|
145
177
|
|
146
178
|
=== Customizing the pagination helper
|
@@ -191,10 +223,42 @@ Kaminari includes a handy template generator.
|
|
191
223
|
=== Paginating a generic Array object
|
192
224
|
|
193
225
|
Kaminari provides an Array wrapper class that adapts a generic Array object to the <tt>paginate</tt> view helper.
|
194
|
-
However, the <tt>paginate</tt> helper doesn't automatically handle your Array object (this is
|
226
|
+
However, the <tt>paginate</tt> helper doesn't automatically handle your Array object (this is intentional and by design).
|
195
227
|
<tt>Kaminari::paginate_array</tt> method converts your Array object into a paginatable Array that accepts <tt>page</tt> method.
|
196
228
|
Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
|
197
229
|
|
230
|
+
You can specify the `total_count` value through options Hash. This would be helpful when handling an Array-ish object that has a different `count` value from actual `count` such as RSolr search result.
|
231
|
+
|
232
|
+
|
233
|
+
== Creating friendly URLs and caching
|
234
|
+
|
235
|
+
Because of the `page` parameter and Rails 3 routing, you can easily generate SEO and user-friendly URLs. For any resource you'd like to paginate, just add the following to your `routes.rb`:
|
236
|
+
|
237
|
+
resources :my_resources do
|
238
|
+
get 'page/:page', :action => :index, :on => :collection
|
239
|
+
end
|
240
|
+
|
241
|
+
This will create URLs like `/my_resources/page/33` instead of `/my_resources?page=33`. This is now a friendly URL, but it also has other added benefits...
|
242
|
+
|
243
|
+
Because the `page` parameter is now a URL segment, we can leverage on Rails page caching[http://guides.rubyonrails.org/caching_with_rails.html#page-caching]!
|
244
|
+
|
245
|
+
NOTE: In this example, I've pointed the route to my `:index` action. You may have defined a custom pagination action in your controller - you should point `:action => :your_custom_action` instead.
|
246
|
+
|
247
|
+
|
248
|
+
== Sinatra/Padrino support
|
249
|
+
|
250
|
+
Since version 0.13.0, kaminari started to support Sinatra or Sinatra-based frameworks experimentally.
|
251
|
+
|
252
|
+
To use kaminari and its helpers with these frameworks,
|
253
|
+
|
254
|
+
require 'kaminari/sinatra'
|
255
|
+
|
256
|
+
or edit gemfile:
|
257
|
+
|
258
|
+
gem 'kaminari', :require => 'kaminari/sinatra'
|
259
|
+
|
260
|
+
More features are coming, and again, this is still experimental. Please let us know if you found anything wrong with the Sinatra support.
|
261
|
+
|
198
262
|
|
199
263
|
== For more information
|
200
264
|
|
@@ -202,6 +266,9 @@ Check out Kaminari recipes on the GitHub Wiki for more advanced tips and techniq
|
|
202
266
|
https://github.com/amatsuda/kaminari/wiki/Kaminari-recipes
|
203
267
|
|
204
268
|
|
269
|
+
== Build Status {<img src="https://secure.travis-ci.org/amatsuda/kaminari.png"/>}[http://travis-ci.org/amatsuda/kaminari]
|
270
|
+
|
271
|
+
|
205
272
|
== Questions, Feedback
|
206
273
|
|
207
274
|
Feel free to message me on Github (amatsuda) or Twitter (@a_matsuda) ☇☇☇ :)
|
@@ -214,4 +281,4 @@ Feel free to message me on Github (amatsuda) or Twitter (@a_matsuda) ☇☇☇
|
|
214
281
|
|
215
282
|
== Copyright
|
216
283
|
|
217
|
-
Copyright (c) 2011 Akira Matsuda. See LICENSE
|
284
|
+
Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.
|
data/kaminari.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ['Akira Matsuda']
|
10
10
|
s.email = ['ronnie@dio.jp']
|
11
11
|
s.homepage = 'https://github.com/amatsuda/kaminari'
|
12
|
-
s.summary = 'A pagination engine plugin for Rails 3'
|
13
|
-
s.description = 'Kaminari is a Scope & Engine based, clean, powerful, customizable and sophisticated paginator for Rails 3'
|
12
|
+
s.summary = 'A pagination engine plugin for Rails 3 or other modern frameworks'
|
13
|
+
s.description = 'Kaminari is a Scope & Engine based, clean, powerful, agnostic, customizable and sophisticated paginator for Rails 3'
|
14
14
|
|
15
15
|
s.rubyforge_project = 'kaminari'
|
16
16
|
|
@@ -21,15 +21,28 @@ Gem::Specification.new do |s|
|
|
21
21
|
|
22
22
|
s.licenses = ['MIT']
|
23
23
|
|
24
|
-
|
24
|
+
%w{ activesupport actionpack railties }.each do |gem|
|
25
|
+
s.add_dependency gem, ['>= 3.0.0']
|
26
|
+
end
|
25
27
|
s.add_development_dependency 'bundler', ['>= 1.0.0']
|
26
28
|
s.add_development_dependency 'sqlite3', ['>= 0']
|
29
|
+
%w{ activerecord activemodel }.each do |gem|
|
30
|
+
s.add_development_dependency gem, ['>= 3.0.0']
|
31
|
+
end
|
32
|
+
s.add_development_dependency 'sinatra', ['>= 1.3']
|
27
33
|
s.add_development_dependency 'mongoid', ['>= 2']
|
28
34
|
s.add_development_dependency 'mongo_mapper', ['>= 0.9']
|
35
|
+
s.add_development_dependency 'dm-core', ['>= 1.1.0']
|
36
|
+
s.add_development_dependency 'dm-migrations', ['>= 1.1.0']
|
37
|
+
s.add_development_dependency 'dm-aggregates', ['>= 1.1.0']
|
38
|
+
s.add_development_dependency 'dm-sqlite-adapter', ['>= 1.1.0']
|
29
39
|
s.add_development_dependency 'rspec', ['>= 0']
|
30
40
|
s.add_development_dependency 'rspec-rails', ['>= 0']
|
31
41
|
s.add_development_dependency 'rr', ['>= 0']
|
32
|
-
s.add_development_dependency '
|
33
|
-
s.add_development_dependency 'capybara', ['>= 0']
|
42
|
+
s.add_development_dependency 'capybara', ['>= 1.0']
|
34
43
|
s.add_development_dependency 'database_cleaner', ['>= 0']
|
44
|
+
s.add_development_dependency 'padrino-helpers', ['~> 0.10']
|
45
|
+
s.add_development_dependency 'rack-test', ['>= 0']
|
46
|
+
s.add_development_dependency 'sinatra-contrib', ['~> 1.3']
|
47
|
+
s.add_development_dependency 'nokogiri', ['>= 0']
|
35
48
|
end
|
@@ -6,7 +6,7 @@ module Kaminari
|
|
6
6
|
class ViewsGenerator < Rails::Generators::NamedBase
|
7
7
|
source_root File.expand_path('../../../../app/views/kaminari', __FILE__)
|
8
8
|
|
9
|
-
class_option :template_engine, :type => :string, :aliases => '-e', :desc => 'Template engine for the views. Available options are "erb" and "
|
9
|
+
class_option :template_engine, :type => :string, :aliases => '-e', :desc => 'Template engine for the views. Available options are "erb", "haml", and "slim".'
|
10
10
|
|
11
11
|
def self.banner #:nodoc:
|
12
12
|
<<-BANNER.chomp
|
data/lib/kaminari.rb
CHANGED
@@ -1,2 +1,73 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Kaminari
|
2
|
+
def self.frameworks
|
3
|
+
frameworks = []
|
4
|
+
case
|
5
|
+
when rails? then frameworks << 'rails'
|
6
|
+
when sinatra? then frameworks << 'sinatra/base'
|
7
|
+
end
|
8
|
+
frameworks
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.load_framework!
|
12
|
+
show_warning if frameworks.empty?
|
13
|
+
frameworks.each do |framework|
|
14
|
+
begin
|
15
|
+
require framework
|
16
|
+
rescue NameError => e
|
17
|
+
raise "Failed to load framework #{framework.inspect}. Have you added it to Gemfile?"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.show_warning
|
23
|
+
$stderr.puts <<-EOC
|
24
|
+
warning: no framework detected.
|
25
|
+
would you check out if your Gemfile appropriately configured?
|
26
|
+
---- e.g. ----
|
27
|
+
when Rails:
|
28
|
+
gem 'rails'
|
29
|
+
gem 'kaminari'
|
30
|
+
|
31
|
+
when Sinatra/Padrino:
|
32
|
+
gem 'kaminari', :require => 'kaminari/sinatra'
|
33
|
+
|
34
|
+
EOC
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.load_kaminari!
|
38
|
+
require 'kaminari/config'
|
39
|
+
require 'kaminari/helpers/action_view_extension'
|
40
|
+
require 'kaminari/helpers/paginator'
|
41
|
+
require 'kaminari/models/page_scope_methods'
|
42
|
+
require 'kaminari/models/configuration_methods'
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.hook!
|
46
|
+
load_framework!
|
47
|
+
load_kaminari!
|
48
|
+
require 'kaminari/hooks'
|
49
|
+
if rails?
|
50
|
+
require 'kaminari/railtie'
|
51
|
+
require 'kaminari/engine'
|
52
|
+
elsif sinatra?
|
53
|
+
require 'kaminari/sinatra'
|
54
|
+
else
|
55
|
+
Kaminari::Hooks.init!
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.load!
|
60
|
+
hook!
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
def self.rails?
|
65
|
+
defined?(::Rails)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.sinatra?
|
69
|
+
defined?(::Sinatra)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
Kaminari.load!
|
data/lib/kaminari/config.rb
CHANGED
@@ -22,10 +22,11 @@ module Kaminari
|
|
22
22
|
config_accessor :outer_window
|
23
23
|
config_accessor :left
|
24
24
|
config_accessor :right
|
25
|
+
config_accessor :page_method_name
|
25
26
|
config_accessor :param_name
|
26
27
|
|
27
28
|
def param_name
|
28
|
-
config.param_name.respond_to?(:call) ? config.param_name.call
|
29
|
+
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
|
29
30
|
end
|
30
31
|
end
|
31
32
|
|
@@ -36,6 +37,7 @@ module Kaminari
|
|
36
37
|
config.outer_window = 0
|
37
38
|
config.left = 0
|
38
39
|
config.right = 0
|
40
|
+
config.page_method_name = :page
|
39
41
|
config.param_name = :page
|
40
42
|
end
|
41
43
|
end
|
@@ -1,25 +1,79 @@
|
|
1
1
|
module Kaminari
|
2
|
+
# = Helpers
|
2
3
|
module ActionViewExtension
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
4
|
+
# A helper that renders the pagination links.
|
5
|
+
#
|
6
|
+
# <%= paginate @articles %>
|
7
|
+
#
|
8
|
+
# ==== Options
|
9
|
+
# * <tt>:window</tt> - The "inner window" size (4 by default).
|
10
|
+
# * <tt>:outer_window</tt> - The "outer window" size (0 by default).
|
11
|
+
# * <tt>:left</tt> - The "left outer window" size (0 by default).
|
12
|
+
# * <tt>:right</tt> - The "right outer window" size (0 by default).
|
13
|
+
# * <tt>:params</tt> - url_for parameters for the links (:controller, :action, etc.)
|
14
|
+
# * <tt>:param_name</tt> - parameter name for page number in the links (:page by default)
|
15
|
+
# * <tt>:remote</tt> - Ajax? (false by default)
|
16
|
+
# * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
|
17
|
+
def paginate(scope, options = {}, &block)
|
18
|
+
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)
|
19
|
+
paginator.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
# A simple "Twitter like" pagination link that creates a link to the next page.
|
23
|
+
#
|
24
|
+
# ==== Examples
|
25
|
+
# Basic usage:
|
26
|
+
#
|
27
|
+
# <%= link_to_next_page @items, 'Next Page' %>
|
28
|
+
#
|
29
|
+
# Ajax:
|
30
|
+
#
|
31
|
+
# <%= link_to_next_page @items, 'Next Page', :remote => true %>
|
32
|
+
#
|
33
|
+
# By default, it renders nothing if there are no more results on the next page.
|
34
|
+
# You can customize this output by passing a block.
|
35
|
+
#
|
36
|
+
# <%= link_to_next_page @users, 'Next Page' do %>
|
37
|
+
# <span>No More Pages</span>
|
38
|
+
# <% end %>
|
39
|
+
def link_to_next_page(scope, name, options = {}, &block)
|
40
|
+
params = options.delete(:params) || {}
|
41
|
+
param_name = options.delete(:param_name) || Kaminari.config.param_name
|
42
|
+
link_to_unless scope.last_page?, name, params.merge(param_name => (scope.current_page + 1)), options.reverse_merge(:rel => 'next') do
|
43
|
+
block.call if block
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Renders a helpful message with numbers of displayed vs. total entries.
|
48
|
+
# Ported from mislav/will_paginate
|
49
|
+
#
|
50
|
+
# ==== Examples
|
51
|
+
# Basic usage:
|
52
|
+
#
|
53
|
+
# <%= page_entries_info @posts %>
|
54
|
+
# #-> Displaying posts 6 - 10 of 26 in total
|
55
|
+
#
|
56
|
+
# By default, the message will use the humanized class name of objects
|
57
|
+
# in collection: for instance, "project types" for ProjectType models.
|
58
|
+
# Override this with the <tt>:entry_name</tt> parameter:
|
59
|
+
#
|
60
|
+
# <%= page_entries_info @posts, :entry_name => 'item' %>
|
61
|
+
# #-> Displaying items 6 - 10 of 26 in total
|
62
|
+
def page_entries_info(collection, options = {})
|
63
|
+
entry_name = options[:entry_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
|
64
|
+
if collection.num_pages < 2
|
65
|
+
case collection.total_count
|
66
|
+
when 0; "No #{entry_name.pluralize} found"
|
67
|
+
when 1; "Displaying <b>1</b> #{entry_name}"
|
68
|
+
else; "Displaying <b>all #{collection.total_count}</b> #{entry_name.pluralize}"
|
69
|
+
end
|
70
|
+
else
|
71
|
+
offset = (collection.current_page - 1) * collection.limit_value
|
72
|
+
%{Displaying #{entry_name.pluralize} <b>%d - %d</b> of <b>%d</b> in total} % [
|
73
|
+
offset + 1,
|
74
|
+
offset + collection.count,
|
75
|
+
collection.total_count
|
76
|
+
]
|
23
77
|
end
|
24
78
|
end
|
25
79
|
end
|