README.md in hanami-pagination-0.1.0 vs README.md in hanami-pagination-0.2.0
- old
+ new
@@ -1,7 +1,7 @@
# Hanami::Pagination
-Pagination gem for your hanami applications
+Pagination gem for your hanami applications. Based on ROM::Pagination plugin.
## Installation
Add this line to your application's Gemfile:
@@ -22,28 +22,24 @@
```ruby
# in action
module Web::Controllers::Books
class Index
include Web::Action
-
- # include Pagination::Action module
include Hanami::Pagination::Action
def call(params)
- ...
+ # ...
end
end
end
```
```ruby
# in view
module Web::Views::Books
class Index
include Web::View
-
- # include Pagination::View module
include Hanami::Pagination::View
end
end
```
@@ -58,11 +54,11 @@
## Usage
Now you have special methods for working with pagination in your app.
### Action
#### `all_for_page`
-This helper takes rom/hanami relation and sets `pager` expose. Returns array. Example:
+This helper takes **only rom/hanami relation** and sets `pager` expose. Returns array. Example:
```ruby
module Web::Controllers::Books
class Index
include Web::Action
@@ -70,11 +66,11 @@
expose :books
def call(params)
repo = BookRepository.new
- @books = all_for_page(repo.all)
+ @books = all_for_page(repo.books)
end
end
end
```
@@ -88,11 +84,11 @@
expose :books
def call(params)
repo = BookRepository.new
- @books = all_for_page(repo.all)
+ @books = all_for_page(repo.books)
end
def limit
25
end
@@ -104,19 +100,62 @@
When you include `Pagination::Action` to your action you get `pager` getter with `Hanami::Pagination::Pager` instance. Please check source code for this class. In future I'll add full documentation. Now we support this methods:
- `next_page`
- `prev_page`
- `total`
-- `next_page`
+- `total_pages`
- `current_page?`
- `pages_range`
- `all_pages`
- `first_page?`
- `last_page?`
+- `previous_page_path`
+- `next_page_path`
+- `n_page_path`
+- `paginate`
### View
+
+#### `paginate(page)`
+
+Returns `<nav>` tag with links to first, last and closest pages. For example:
+
+```ruby
+paginate(:items) # where `:items` is a named route
+```
+
+when there is 11 pages, will returns:
+
+```html
+<nav class="pagination">
+ <a href="/items?page=1" class="pagination-first-page">
+ 1
+ </a>
+ <span class="pagination-ellipsis">
+ ...
+ </span>
+ <a href="/items?page=4" class="pagination-previous-page">
+ 4
+ </a>
+ <span class="pagination-current-page">
+ 5
+ </span>
+ <a href="/items?page=6" class="pagination-next-page">
+ 6
+ </a>
+ <span class="pagination-ellipsis">
+ ...
+ </span>
+ <a href="/items?page=11" class="pagination-last-page">
+ 11
+ </a>
+</nav>
+
+```
+Every elements has special css-classes, so it is easy to change pagination look.
+
#### `next_page_url`
Returns string with url to next page. Example:
```ruby
next_page_url # => '/books?page=3'
@@ -134,9 +173,52 @@
```ruby
page_url(4) # => '/books?page=4'
```
+#### `previous_page_path(page)`
+Returns string with `page` path and current `params` to prev page. Example:
+
+```ruby
+# params => { status: 'active' }
+# pager.current_page?(2) => true
+previous_page_path(:books) # => '/books?status=active&page=1'
+```
+
+#### `next_page_path(page)`
+Returns string with `page` path and current `params` to next page. Example:
+
+```ruby
+# params => { status: 'inactive' }
+# pager.current_page?(1) => true
+previous_page_path(:users) # => '/books?status=inactive&page=2'
+```
+
+#### `n_page_path(page, n)`
+Returns string with `page` path and current `params` to specific page. Example:
+
+```ruby
+# params => { status: 'active' }
+previous_page_path(:books, 10) # => '/books?status=active&page=10'
+```
+
+### Testing
+
+You can use `Hanami::Pagination::MockPager` class for testing you apps.
+
+#### View testing
+```ruby
+RSpec.describe Web::Views::Books::Show do
+ let(:mock_pager) { Hanami::Pagination::MockPager.new(current_page, total_pages) }
+ let(:pager) { Hanami::Pagination::Pager.new(mock_pager) }
+ let(:exposures) { Hash[pager: pager] }
+
+ let(:current_page) { 1 }
+ let(:total_pages) { 10 }
+
+ # ...
+end
+```
+
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
-