hanami-pagination 0.1.0 → 0.2.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: 0986664b4be2c0f014eeb429da1f582bd329b527
4
- data.tar.gz: 1d78af3d6e59e319fe8bba542eb3239b7aa28e45
3
+ metadata.gz: aba6404915cdeb83f938c0c092062a1df5d4ce25
4
+ data.tar.gz: d4ef8a7276cb2231ec38f67c3c4a210fc114040d
5
5
  SHA512:
6
- metadata.gz: 4953cc0cd70890fc472c73a71430b791e5f9ec6e3108ccb41ffcf2f229e3b938f552f6535933e8fd3173356ad465eaa39028808c3e596c9270d79ef361f69db3
7
- data.tar.gz: 43ccef361c6eca6aa2d9359683cfb47d62e8becb82d99df7d00c1eaadcf4fecc4e0ca48a764250988c75bb2665754eb4e44da3e27e9465abe73eb4b28f4087b6
6
+ metadata.gz: 4423ba26ea9d10ccf6ae25bec80318dadae2c96256cbad8d551dc46f2cb7df24e081791806b7d9d33f010b31677b3e4e5970a6d24943812305f43f739cfc4672
7
+ data.tar.gz: 76f525ecc258ef16ab55427784fc53802afca5ff99c551d8fa3190af58907167c752f7ad6e32a19ae2b063f8347ab67b5a04ba38bd3cca16f38fcb8cb48e31f0
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,10 @@
1
+ # Hanami-pagination changes
2
+
3
+ HEAD
4
+ -----------
5
+
6
+ - Introduce `MockPager` class for easily testing your app
7
+ - Introduce `pager#total_pages` for getting total pages count
8
+
9
+ 0.1.0
10
+ -----------
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Hanami::Pagination
2
- Pagination gem for your hanami applications
2
+ Pagination gem for your hanami applications. Based on ROM::Pagination plugin.
3
3
 
4
4
  ## Installation
5
5
 
@@ -24,12 +24,10 @@ Include pagination helpers to view and action:
24
24
  module Web::Controllers::Books
25
25
  class Index
26
26
  include Web::Action
27
-
28
- # include Pagination::Action module
29
27
  include Hanami::Pagination::Action
30
28
 
31
29
  def call(params)
32
- ...
30
+ # ...
33
31
  end
34
32
  end
35
33
  end
@@ -40,8 +38,6 @@ end
40
38
  module Web::Views::Books
41
39
  class Index
42
40
  include Web::View
43
-
44
- # include Pagination::View module
45
41
  include Hanami::Pagination::View
46
42
  end
47
43
  end
@@ -60,7 +56,7 @@ Now you have special methods for working with pagination in your app.
60
56
 
61
57
  ### Action
62
58
  #### `all_for_page`
63
- This helper takes rom/hanami relation and sets `pager` expose. Returns array. Example:
59
+ This helper takes **only rom/hanami relation** and sets `pager` expose. Returns array. Example:
64
60
 
65
61
  ```ruby
66
62
  module Web::Controllers::Books
@@ -72,7 +68,7 @@ module Web::Controllers::Books
72
68
 
73
69
  def call(params)
74
70
  repo = BookRepository.new
75
- @books = all_for_page(repo.all)
71
+ @books = all_for_page(repo.books)
76
72
  end
77
73
  end
78
74
  end
@@ -90,7 +86,7 @@ module Web::Controllers::Books
90
86
 
91
87
  def call(params)
92
88
  repo = BookRepository.new
93
- @books = all_for_page(repo.all)
89
+ @books = all_for_page(repo.books)
94
90
  end
95
91
 
96
92
  def limit
@@ -106,15 +102,58 @@ When you include `Pagination::Action` to your action you get `pager` getter with
106
102
  - `next_page`
107
103
  - `prev_page`
108
104
  - `total`
109
- - `next_page`
105
+ - `total_pages`
110
106
  - `current_page?`
111
107
  - `pages_range`
112
108
  - `all_pages`
113
109
  - `first_page?`
114
110
  - `last_page?`
111
+ - `previous_page_path`
112
+ - `next_page_path`
113
+ - `n_page_path`
114
+ - `paginate`
115
115
 
116
116
 
117
117
  ### View
118
+
119
+ #### `paginate(page)`
120
+
121
+ Returns `<nav>` tag with links to first, last and closest pages. For example:
122
+
123
+ ```ruby
124
+ paginate(:items) # where `:items` is a named route
125
+ ```
126
+
127
+ when there is 11 pages, will returns:
128
+
129
+ ```html
130
+ <nav class="pagination">
131
+ <a href="/items?page=1" class="pagination-first-page">
132
+ 1
133
+ </a>
134
+ <span class="pagination-ellipsis">
135
+ ...
136
+ </span>
137
+ <a href="/items?page=4" class="pagination-previous-page">
138
+ 4
139
+ </a>
140
+ <span class="pagination-current-page">
141
+ 5
142
+ </span>
143
+ <a href="/items?page=6" class="pagination-next-page">
144
+ 6
145
+ </a>
146
+ <span class="pagination-ellipsis">
147
+ ...
148
+ </span>
149
+ <a href="/items?page=11" class="pagination-last-page">
150
+ 11
151
+ </a>
152
+ </nav>
153
+
154
+ ```
155
+ Every elements has special css-classes, so it is easy to change pagination look.
156
+
118
157
  #### `next_page_url`
119
158
  Returns string with url to next page. Example:
120
159
 
@@ -136,7 +175,50 @@ Returns string with url to specific page. Example:
136
175
  page_url(4) # => '/books?page=4'
137
176
  ```
138
177
 
178
+ #### `previous_page_path(page)`
179
+ Returns string with `page` path and current `params` to prev page. Example:
180
+
181
+ ```ruby
182
+ # params => { status: 'active' }
183
+ # pager.current_page?(2) => true
184
+ previous_page_path(:books) # => '/books?status=active&page=1'
185
+ ```
186
+
187
+ #### `next_page_path(page)`
188
+ Returns string with `page` path and current `params` to next page. Example:
189
+
190
+ ```ruby
191
+ # params => { status: 'inactive' }
192
+ # pager.current_page?(1) => true
193
+ previous_page_path(:users) # => '/books?status=inactive&page=2'
194
+ ```
195
+
196
+ #### `n_page_path(page, n)`
197
+ Returns string with `page` path and current `params` to specific page. Example:
198
+
199
+ ```ruby
200
+ # params => { status: 'active' }
201
+ previous_page_path(:books, 10) # => '/books?status=active&page=10'
202
+ ```
203
+
204
+ ### Testing
205
+
206
+ You can use `Hanami::Pagination::MockPager` class for testing you apps.
207
+
208
+ #### View testing
209
+ ```ruby
210
+ RSpec.describe Web::Views::Books::Show do
211
+ let(:mock_pager) { Hanami::Pagination::MockPager.new(current_page, total_pages) }
212
+ let(:pager) { Hanami::Pagination::Pager.new(mock_pager) }
213
+ let(:exposures) { Hash[pager: pager] }
214
+
215
+ let(:current_page) { 1 }
216
+ let(:total_pages) { 10 }
217
+
218
+ # ...
219
+ end
220
+ ```
221
+
139
222
  ## License
140
223
 
141
224
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
142
-
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.require_paths = ["lib"]
23
23
 
24
24
  spec.add_dependency "hanami-model", "~> 1.0"
25
+ spec.add_dependency "hanami-helpers", "~> 1.1"
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.13"
27
28
  spec.add_development_dependency "rake", "~> 10.0"
@@ -3,6 +3,7 @@ require 'hanami/pagination/pager'
3
3
  require 'hanami/pagination/action'
4
4
  require 'hanami/pagination/version'
5
5
  require 'hanami/pagination/extentions'
6
+ require 'hanami/pagination/mock_pager'
6
7
 
7
8
  module Hanami
8
9
  module Pagination
@@ -1,7 +1,7 @@
1
- module Hanami
2
- class Repository
3
- def self.enable_pagination!
4
- container.relation(relation).class.use(:pagination)
5
- end
1
+ require "hanami/repository"
2
+
3
+ Hanami::Repository.class_eval do
4
+ def self.enable_pagination!
5
+ container.relations[relation].class.use(:pagination)
6
6
  end
7
7
  end
@@ -0,0 +1,36 @@
1
+ module Hanami
2
+ module Pagination
3
+ class MockPager
4
+ attr_reader :current_page, :total_pages
5
+
6
+ def initialize(current_page, total_pages)
7
+ @current_page = current_page
8
+ @total_pages = total_pages
9
+ end
10
+
11
+ def next_page
12
+ [current_page + 1, total_pages].min
13
+ end
14
+
15
+ def prev_page
16
+ [current_page - 1, 1].max
17
+ end
18
+
19
+ def total
20
+ total_pages
21
+ end
22
+
23
+ def total_pages
24
+ @total_pages
25
+ end
26
+
27
+ def first_page?
28
+ current_page == 1
29
+ end
30
+
31
+ def last_page?
32
+ current_page == total_pages
33
+ end
34
+ end
35
+ end
36
+ end
@@ -19,8 +19,12 @@ module Hanami
19
19
  pager.total
20
20
  end
21
21
 
22
- def next_page
23
- pager.next_page
22
+ def total_pages
23
+ pager.total_pages
24
+ end
25
+
26
+ def current_page
27
+ pager.current_page
24
28
  end
25
29
 
26
30
  def current_page?(page)
@@ -1,5 +1,5 @@
1
1
  module Hanami
2
2
  module Pagination
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,6 +1,10 @@
1
+ require 'hanami/helpers'
2
+
1
3
  module Hanami
2
4
  module Pagination
3
5
  module View
6
+ include Hanami::Helpers
7
+
4
8
  def next_page_url
5
9
  page_url(pager.next_page)
6
10
  end
@@ -12,6 +16,70 @@ module Hanami
12
16
  def page_url(page)
13
17
  "#{params.env['REQUEST_PATH']}?page=#{page}"
14
18
  end
19
+
20
+ def previous_page_path(page)
21
+ routes.path(page, **params, page: pager.prev_page)
22
+ end
23
+
24
+ def next_page_path(page)
25
+ routes.path(page, **params, page: pager.next_page)
26
+ end
27
+
28
+ def n_page_path(page, n)
29
+ routes.path(page, **params, page: n)
30
+ end
31
+
32
+ def paginate(page)
33
+ html.nav(class: 'pagination') do
34
+ content = []
35
+
36
+ content << first_page_tag(page) unless pager.first_page?
37
+ content << ellipsis_tag if pager.current_page > 3
38
+ content << previous_page_tag(page) if pager.current_page > 2
39
+ content << current_page_tag
40
+ content << next_page_tag(page) if (pager.total_pages - pager.current_page) > 1
41
+ content << ellipsis_tag if (pager.total_pages - pager.current_page) > 3
42
+ content << last_page_tag(page) unless pager.last_page?
43
+
44
+ raw(content.map(&:to_s).join)
45
+ end
46
+ end
47
+
48
+ def first_page_tag(page)
49
+ html.a(href: n_page_path(page, 1), class: 'pagination-first-page') do
50
+ '1'
51
+ end
52
+ end
53
+
54
+ def previous_page_tag(page)
55
+ html.a(href: previous_page_path(page), class: 'pagination-previous-page') do
56
+ pager.prev_page
57
+ end
58
+ end
59
+
60
+ def current_page_tag
61
+ html.span(class: 'pagination-current-page') do
62
+ pager.current_page
63
+ end
64
+ end
65
+
66
+ def last_page_tag(page)
67
+ html.a(href: n_page_path(page, pager.total_pages), class: 'pagination-last-page') do
68
+ pager.total_pages
69
+ end
70
+ end
71
+
72
+ def next_page_tag(page)
73
+ html.a(href: next_page_path(page), class: 'pagination-next-page') do
74
+ pager.next_page
75
+ end
76
+ end
77
+
78
+ def ellipsis_tag
79
+ html.span(class: 'pagination-ellipsis') do
80
+ '...'
81
+ end
82
+ end
15
83
  end
16
84
  end
17
85
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Davydov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-02 00:00:00.000000000 Z
11
+ date: 2018-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hanami-model
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hanami-helpers
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -74,7 +88,9 @@ extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
90
  - ".gitignore"
91
+ - ".rspec"
77
92
  - ".travis.yml"
93
+ - CHANGES.md
78
94
  - CODE_OF_CONDUCT.md
79
95
  - Gemfile
80
96
  - LICENSE.txt
@@ -84,6 +100,7 @@ files:
84
100
  - lib/hanami/pagination.rb
85
101
  - lib/hanami/pagination/action.rb
86
102
  - lib/hanami/pagination/extentions.rb
103
+ - lib/hanami/pagination/mock_pager.rb
87
104
  - lib/hanami/pagination/pager.rb
88
105
  - lib/hanami/pagination/version.rb
89
106
  - lib/hanami/pagination/view.rb
@@ -107,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
124
  version: '0'
108
125
  requirements: []
109
126
  rubyforge_project:
110
- rubygems_version: 2.5.1
127
+ rubygems_version: 2.6.14
111
128
  signing_key:
112
129
  specification_version: 4
113
130
  summary: Pagination in your hanami apps