diff --git a/manual/README.md b/manual/README.md index eefa09d69..040147bda 100644 --- a/manual/README.md +++ b/manual/README.md @@ -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 @@ -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) \ No newline at end of file diff --git a/manual/querying/Pagination.md b/manual/querying/Pagination.md new file mode 100644 index 000000000..c4bb0736c --- /dev/null +++ b/manual/querying/Pagination.md @@ -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 +
+``` + + diff --git a/src/clear/sql/query/with_pagination.cr b/src/clear/sql/query/with_pagination.cr index 6f980852c..39bb1a9f6 100644 --- a/src/clear/sql/query/with_pagination.cr +++ b/src/clear/sql/query/with_pagination.cr @@ -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) @@ -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.