foliate 1.0.0 → 1.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.
- checksums.yaml +4 -4
- data/README.md +30 -19
- data/lib/foliate/config.rb +3 -2
- data/lib/foliate/controller.rb +3 -3
- data/lib/foliate/pagination.rb +46 -11
- data/lib/foliate/railtie.rb +1 -0
- data/lib/foliate/version.rb +1 -1
- data/lib/generators/foliate/install/install_generator.rb +1 -0
- data/lib/generators/foliate/install/templates/views/page_input.html.erb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1379d91804fb3050affdd4a02ff646f2365715ac
|
4
|
+
data.tar.gz: ce950722db7f2aca60ee3cfa3bdd51fc738d6538
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7aab7c69bd58aeafcb664fb318e358c41e1fa7df0705cc5c402d8f1b53bc0114998ea4555b2439143d1d9f0635ba1371a8dee1b8afcd0ade86c785b2be2e7e3
|
7
|
+
data.tar.gz: efbb16cee1e2ec271223ddd8395f9ebbdae7e92e7b51e6fafb6b20321ea793c9dfd14781d31fec07f74be929f8cb57f658b0a1b6993547cdcc88bf17dc591e75
|
data/README.md
CHANGED
@@ -6,8 +6,8 @@ Pagination for Ruby on Rails.
|
|
6
6
|
to a cleaner and much simpler implementation.
|
7
7
|
|
8
8
|
**How is it different?** Instead of extending ActiveRecord, or
|
9
|
-
|
10
|
-
|
9
|
+
defining singleton methods on result sets at runtime, *foliate* adds a
|
10
|
+
single method to ActionController:
|
11
11
|
|
12
12
|
```ruby
|
13
13
|
## app/controllers/posts_controller.rb
|
@@ -37,41 +37,52 @@ Do something with @posts here...
|
|
37
37
|
<%= render @pagination %>
|
38
38
|
```
|
39
39
|
|
40
|
-
**
|
40
|
+
**What does it look like?** By default, something like this:
|
41
41
|
|
42
42
|
<img src="screenshots/page_input.png" alt="page input pagination">
|
43
43
|
|
44
44
|
|
45
|
-
##
|
45
|
+
## Appearance
|
46
46
|
|
47
|
-
|
48
|
-
*foliate* installation generator creates
|
47
|
+
The *foliate* installation generator creates
|
49
48
|
"app/views/pagination/_pagination.html.erb" and
|
50
49
|
"app/assets/stylesheets/pagination.css" in your project directory.
|
51
|
-
These files can be edited to
|
50
|
+
These files can be freely edited to suit your needs.
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
## Records per page
|
53
|
+
|
54
|
+
By default, *foliate* allots `Foliate.config.default_per_page` records
|
55
|
+
per page, which can be configured in "config/initializers/foliate.rb".
|
56
|
+
However, this can also be overridden on a per-Controller basis using the
|
57
|
+
`per_page:` argument:
|
57
58
|
|
58
59
|
```ruby
|
59
60
|
@posts = paginate(Post, per_page: 50)
|
60
61
|
```
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
63
|
+
## Page param name
|
64
|
+
|
65
|
+
*foliate* uses the `:page` query param (i.e. `params[:page]`) to dictate
|
66
|
+
the current page. This can be configured by setting
|
67
|
+
`Foliate.config.page_param` in "config/initializers/foliate.rb". Doing
|
68
|
+
so will properly affect both the `paginate` method and the `Pagination`
|
69
|
+
object.
|
70
|
+
|
71
|
+
## Avoiding SQL count on very large tables
|
72
|
+
|
73
|
+
To determine the total number of pages, *foliate* performs a SQL count
|
74
|
+
query. On very large tables, count queries can have a noticeable
|
75
|
+
performance cost. If you have a more performant method of estimating
|
76
|
+
total record count, you can prevent the count query by specifying the
|
77
|
+
`total_records:` argument:
|
65
78
|
|
66
79
|
```ruby
|
67
80
|
@posts = paginate(Post, total_records: Post.cached_count)
|
68
81
|
```
|
69
82
|
|
70
|
-
|
71
|
-
page?** That can be configured with `Foliate.config.page_param`, which
|
72
|
-
is set in "config/initializers/foliate.rb".
|
83
|
+
## Full documentation
|
73
84
|
|
74
|
-
|
85
|
+
For more information, see the
|
75
86
|
[full documentation](http://www.rubydoc.info/gems/foliate/).
|
76
87
|
|
77
88
|
|
@@ -86,7 +97,7 @@ gem "foliate"
|
|
86
97
|
Then execute:
|
87
98
|
|
88
99
|
```bash
|
89
|
-
$ bundle
|
100
|
+
$ bundle install
|
90
101
|
```
|
91
102
|
|
92
103
|
And finally, run the installation generator:
|
data/lib/foliate/config.rb
CHANGED
@@ -6,8 +6,8 @@ module Foliate
|
|
6
6
|
attr_accessor :default_per_page
|
7
7
|
|
8
8
|
# @return [Symbol]
|
9
|
-
#
|
10
|
-
# +:page+)
|
9
|
+
# request query param name for specifying page numbers (defaults
|
10
|
+
# to +:page+)
|
11
11
|
attr_accessor :page_param
|
12
12
|
|
13
13
|
def initialize
|
@@ -24,6 +24,7 @@ module Foliate
|
|
24
24
|
end
|
25
25
|
|
26
26
|
# @param c [Foliate::Config]
|
27
|
+
# @return [Foliate::Config]
|
27
28
|
def self.config=(c)
|
28
29
|
@config = c
|
29
30
|
end
|
data/lib/foliate/controller.rb
CHANGED
@@ -20,7 +20,7 @@ module Foliate
|
|
20
20
|
#
|
21
21
|
# By default, if +records+ is an ActiveRecord::Relation, computes
|
22
22
|
# the total number of pages by performing a SQL count query. If the
|
23
|
-
# underlying table is very large, this query
|
23
|
+
# underlying table is very large, this query can have a noticeable
|
24
24
|
# performance cost. This can be circumvented by specifying
|
25
25
|
# +total_records:+, using an estimated or cached record count.
|
26
26
|
#
|
@@ -28,10 +28,10 @@ module Foliate
|
|
28
28
|
# @posts = paginate(Post)
|
29
29
|
#
|
30
30
|
# @example paginated search
|
31
|
-
# @posts = paginate(Post.where(
|
31
|
+
# @posts = paginate(Post.where(author: params[:author]))
|
32
32
|
#
|
33
33
|
# @example user-specified per_page
|
34
|
-
# @posts = paginate(Post, per_page: params[:
|
34
|
+
# @posts = paginate(Post, per_page: params[:per])
|
35
35
|
#
|
36
36
|
# @example simple cached count
|
37
37
|
# count = Rails.cache.fetch("Post/count", expires_in: 5.minutes){ Post.count }
|
data/lib/foliate/pagination.rb
CHANGED
@@ -27,28 +27,54 @@ module Foliate
|
|
27
27
|
# originating request query params
|
28
28
|
attr_accessor :query_params
|
29
29
|
|
30
|
-
# Computes the total number of pages based on
|
31
|
-
#
|
30
|
+
# Computes the total number of pages based on {#total_records} and
|
31
|
+
# {#per_page}.
|
32
32
|
#
|
33
33
|
# @return [Integer]
|
34
34
|
def total_pages
|
35
35
|
(total_records / per_page.to_f).ceil
|
36
36
|
end
|
37
37
|
|
38
|
-
# Computes the record set offset based on
|
39
|
-
#
|
38
|
+
# Computes the record set offset based on {#current_page} and
|
39
|
+
# {#per_page}.
|
40
40
|
#
|
41
41
|
# @return [Integer]
|
42
42
|
def offset
|
43
43
|
(current_page - 1) * per_page
|
44
44
|
end
|
45
45
|
|
46
|
+
# Iterates through each {#query_params} as name-value pairs. Nested
|
47
|
+
# Hashes and Arrays are iterated as well. The +name+ of nested a
|
48
|
+
# +value+ will reflect its nesting in the same way as when
|
49
|
+
# converting a nested Hash to a query string via +Hash#to_query+.
|
50
|
+
# Intended for use when generating form fields.
|
51
|
+
#
|
52
|
+
# If no block is given, an Enumerator is returned.
|
53
|
+
#
|
54
|
+
# @overload each_query_param
|
55
|
+
# @yieldparam name [String]
|
56
|
+
# @yieldparam value [String]
|
57
|
+
# @return [nil]
|
58
|
+
#
|
59
|
+
# @overload each_query_param
|
60
|
+
# @return [Enumerator]
|
61
|
+
def each_query_param
|
62
|
+
if block_given?
|
63
|
+
ParamsHelper.to_keyed_pairs(query_params).each do |pair|
|
64
|
+
yield pair[:name], pair[:value]
|
65
|
+
end
|
66
|
+
nil
|
67
|
+
else
|
68
|
+
to_enum(__method__)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
46
72
|
# Returns linking params for a specified +page_number+. The
|
47
73
|
# returned Hash is intended for use with +link_to+, +url_for+, etc.
|
48
74
|
#
|
49
75
|
# @example usage in view
|
50
|
-
# link_to "First", pagination.params_for_page(1)
|
51
|
-
# link_to "Last", pagination.params_for_page(pagination.total_pages)
|
76
|
+
# link_to "First", @pagination.params_for_page(1)
|
77
|
+
# link_to "Last", @pagination.params_for_page(@pagination.total_pages)
|
52
78
|
#
|
53
79
|
# @param page_number [Integer]
|
54
80
|
# @return [Hash]
|
@@ -58,15 +84,14 @@ module Foliate
|
|
58
84
|
) }
|
59
85
|
end
|
60
86
|
|
61
|
-
# Indicates if there is
|
62
|
-
# page.
|
87
|
+
# Indicates if there is a page expected before the current page.
|
63
88
|
#
|
64
89
|
# @return [Boolean]
|
65
90
|
def prev?
|
66
91
|
current_page > 1
|
67
92
|
end
|
68
93
|
|
69
|
-
# Indicates if there is
|
94
|
+
# Indicates if there is a page expected after the current page.
|
70
95
|
#
|
71
96
|
# @return [Boolean]
|
72
97
|
def next?
|
@@ -77,7 +102,7 @@ module Foliate
|
|
77
102
|
# false. See also {#params_for_page}.
|
78
103
|
#
|
79
104
|
# @example usage in view
|
80
|
-
# link_to_if pagination.prev?, "Previous", pagination.prev_page_params
|
105
|
+
# link_to_if @pagination.prev?, "Previous", @pagination.prev_page_params
|
81
106
|
#
|
82
107
|
# @return [Hash, nil]
|
83
108
|
def prev_page_params
|
@@ -88,7 +113,7 @@ module Foliate
|
|
88
113
|
# false. See also {#params_for_page}.
|
89
114
|
#
|
90
115
|
# @example usage in view
|
91
|
-
# link_to_if pagination.next?, "Next", pagination.next_page_params
|
116
|
+
# link_to_if @pagination.next?, "Next", @pagination.next_page_params
|
92
117
|
#
|
93
118
|
# @return [Hash, nil]
|
94
119
|
def next_page_params
|
@@ -107,5 +132,15 @@ module Foliate
|
|
107
132
|
"pagination/pagination"
|
108
133
|
end
|
109
134
|
|
135
|
+
module ParamsHelper
|
136
|
+
extend ActionView::Helpers::UrlHelper
|
137
|
+
|
138
|
+
def self.to_keyed_pairs(params)
|
139
|
+
to_form_params(params)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
private_constant :ParamsHelper
|
144
|
+
|
110
145
|
end
|
111
146
|
end
|
data/lib/foliate/railtie.rb
CHANGED
data/lib/foliate/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foliate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Hefner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 5.1
|
19
|
+
version: '5.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 5.1
|
26
|
+
version: '5.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.5.2.1
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Rails pagination
|