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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e632e2cdf928b30535057da735d185e35d5bb80
4
- data.tar.gz: dcaed853a25f108a2838ad7c686a9e389e55cdb2
3
+ metadata.gz: 1379d91804fb3050affdd4a02ff646f2365715ac
4
+ data.tar.gz: ce950722db7f2aca60ee3cfa3bdd51fc738d6538
5
5
  SHA512:
6
- metadata.gz: b34de5919e49576e9a4c27b84488d179a2a021e618dcbbec69707121023a12fb2ca69eb9a2d0e3058687602bbe2dcde50ffe4b5a058239fe2d2efe006f4ff32a
7
- data.tar.gz: 3b8719a28e535fd8ba94b0ec5bcf78a43451d01ba34172c87389bf05217100aceb042e21888a57232dffec8732c40aa483620427fb95adcb64656128527c708d
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
- dynamically defining methods on objects after instantiating them,
10
- *foliate* adds a single method to ActionController:
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
- **How does that look?** By default, something like this:
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
- ## More Information
45
+ ## Appearance
46
46
 
47
- **How do I customize the appearance of the pagination?** Running the
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 meet your needs.
50
+ These files can be freely edited to suit your needs.
52
51
 
53
- **How many records are in each page?** By default, *foliate* allots
54
- `Foliate.config.default_per_page` records per page, which is set in
55
- "config/initializers/foliate.rb". However, this can be overridden by
56
- passing `per_page:` to `paginate`:
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
- **What if I have a very large table, and don't want to incur a count of
63
- all records for each page?** The `paginate` method accepts a
64
- `total_records:` argument, which will prevent a SQL count when set:
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
- **What if I want a different param than `:page` to dictate the current
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
- **For even more information,** see the
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:
@@ -6,8 +6,8 @@ module Foliate
6
6
  attr_accessor :default_per_page
7
7
 
8
8
  # @return [Symbol]
9
- # URL query param name for specifying page numbers (defaults to
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
@@ -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 could have a noticeable
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(status: params[:show_only]))
31
+ # @posts = paginate(Post.where(author: params[:author]))
32
32
  #
33
33
  # @example user-specified per_page
34
- # @posts = paginate(Post, per_page: params[:at_a_time])
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 }
@@ -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 +total_records+ and
31
- # +per_page+.
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 +current_page+ and
39
- # +per_page+.
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 an expected page previous to the current
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 an expected page beyond the current page.
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
@@ -2,6 +2,7 @@ require "rails/railtie"
2
2
  require "foliate/controller"
3
3
 
4
4
  module Foliate
5
+ # @!visibility private
5
6
  class Railtie < Rails::Railtie
6
7
  initializer :foliate do |app|
7
8
  ActiveSupport.on_load :action_controller do
@@ -1,3 +1,3 @@
1
1
  module Foliate
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -2,6 +2,7 @@ require "foliate/config"
2
2
  require "rails/generators/base"
3
3
 
4
4
  module Foliate
5
+ # @!visibility private
5
6
  module Generators
6
7
  class InstallGenerator < Rails::Generators::Base
7
8
  source_root File.join(__dir__, "templates")
@@ -5,7 +5,7 @@
5
5
 
6
6
  <span class="foliate-i-of-n">
7
7
  <form method="get" class="foliate-form">
8
- <% pagination.query_params.each do |name, value| %>
8
+ <% pagination.each_query_param do |name, value| %>
9
9
  <input type="hidden" name="<%= name %>" value="<%= value %>">
10
10
  <% end %>
11
11
 
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.0.0
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: 2017-11-11 00:00:00.000000000 Z
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.4
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.4
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.6.13
96
+ rubygems_version: 2.5.2.1
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Rails pagination