Skip to content

Commit

Permalink
Add two helpers method for pagination module + write documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yacine Petitprez committed Jun 24, 2018
1 parent c1ae30e commit 888e2bc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
3 changes: 2 additions & 1 deletion manual/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ If you're in hurry and already at ease with Active Record pattern, a simple stan
- [Event Hooks](model/Hooks.md)
- [Migrations](migration/Migration.md)
- [Polymorphism](model/Polymorphism.md)
- [Different database connections](model/MultiConnection.md)

## Querying

Expand All @@ -29,4 +30,4 @@ If you're in hurry and already at ease with Active Record pattern, a simple stan
- [Expression engine](querying/ExpressionEngine.md)
- [Usage of SQL builder](querying/RequestBuilding.md)
- [Transactions And savepoints](querying/Transaction.md)

- [Pagination](querying/Pagination.md)
57 changes: 57 additions & 0 deletions manual/querying/Pagination.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Clear offers methods to manage pagination of your models

Simply use `paginate` method on your collection query:


### Methods used for pagination

## `paginate`

Used to start pagination mode for your models

```crystal
users = User.query.paginate(1, per_page: 25)
```

## `per_page`

Return the number of model per page

```crystal
users.per_page #25
```

## `current_page`

Return the current selected page.


## `total_pages`

Return the number of pages

## `previous_page`, `next_page`, `last_page?`, `first_page?`

Return the previous, next page or `nil` if current page is the first page.
Return `true` or `false` whether the current page is the first or the last.


## Example

```ecr
<div class='my-table'>
<% @users.each do |u| %>
<#...>
<% end %>
<!-- Add pagination widget -->
<% unless @users.first_page? %>
<a href="...?page=<%[email protected]_page%>">Previous</a>
<% end %>
<% unless @users.last_page? %>
<a href="...?page=<%[email protected]_page%>">Next</a>
<a href="...?page=<%[email protected]_pages%>">Last</a>
<% end %>
</div>
```


10 changes: 9 additions & 1 deletion src/clear/sql/query/with_pagination.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Clear::SQL::Query::WithPagination

macro included
property total_entries : Int64? = nil
end
end

# Maybe this goes on the Collection?
def paginate(page : Int32 = DEFAULT_PAGE, per_page : Int32 = DEFAULT_LIMIT)
Expand Down Expand Up @@ -49,6 +49,14 @@ module Clear::SQL::Query::WithPagination
current_page < total_pages ? (current_page + 1) : nil
end

def last_page?
next_page.nil?
end

def first_page?
current_page <= 1
end

# Helper method that is true when someone tries to fetch a page with a
# larger number than the last page. Can be used in combination with flashes
# and redirecting.
Expand Down

0 comments on commit 888e2bc

Please sign in to comment.