{"id":13778275,"url":"https://github.com/davydovanton/hanami-pagination","last_synced_at":"2025-04-17T02:43:20.512Z","repository":{"id":56875774,"uuid":"92930562","full_name":"davydovanton/hanami-pagination","owner":"davydovanton","description":null,"archived":false,"fork":false,"pushed_at":"2018-06-08T16:36:26.000Z","size":29,"stargazers_count":14,"open_issues_count":5,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-26T21:21:06.363Z","etag":null,"topics":["hanami","pagination","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davydovanton.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-31T09:41:44.000Z","updated_at":"2024-04-26T09:58:56.000Z","dependencies_parsed_at":"2022-08-20T10:40:35.206Z","dependency_job_id":null,"html_url":"https://github.com/davydovanton/hanami-pagination","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fhanami-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fhanami-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fhanami-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fhanami-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davydovanton","download_url":"https://codeload.github.com/davydovanton/hanami-pagination/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249304803,"owners_count":21247926,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hanami","pagination","ruby"],"created_at":"2024-08-03T18:00:52.609Z","updated_at":"2025-04-17T02:43:20.489Z","avatar_url":"https://github.com/davydovanton.png","language":"Ruby","funding_links":[],"categories":["Hanami Gem List"],"sub_categories":["Pagination"],"readme":"# Hanami::Pagination\nPagination gem for your hanami applications. Based on ROM::Pagination plugin.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'hanami-pagination'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install hanami-pagination\n\nInclude pagination helpers to view and action:\n\n```ruby\n# in action\nmodule Web::Controllers::Books\n  class Index\n    include Web::Action\n    include Hanami::Pagination::Action\n\n    def call(params)\n      # ...\n    end\n  end\nend\n```\n\n```ruby\n# in view\nmodule Web::Views::Books\n  class Index\n    include Web::View\n    include Hanami::Pagination::View\n  end\nend\n```\n\nAfter that you need to enable pagination for each repository class:\n```ruby\n# in config/initializers/enable_pagination.rb\nBookRepository.enable_pagination!\nPostRepository.enable_pagination!\n# etc\n```\n\n## Usage\nNow you have special methods for working with pagination in your app.\n\n### Action\n#### `all_for_page`\nThis helper takes **only rom/hanami relation** and sets `pager` expose. Returns array. Example:\n\n```ruby\nmodule Web::Controllers::Books\n  class Index\n    include Web::Action\n    include Hanami::Pagination::Action\n\n    expose :books\n\n    def call(params)\n      repo = BookRepository.new\n      @books = all_for_page(repo.books)\n    end\n  end\nend\n```\n\nAlso you can set `limit` (default 100) for each action:\n\n```ruby\nmodule Web::Controllers::Books\n  class Index\n    include Web::Action\n    include Hanami::Pagination::Action\n\n    expose :books\n\n    def call(params)\n      repo = BookRepository.new\n      @books = all_for_page(repo.books)\n    end\n\n    def limit\n      25\n    end\n  end\nend\n```\n\n#### `pager` expose\nWhen 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:\n\n- `next_page`\n- `prev_page`\n- `total`\n- `total_pages`\n- `current_page?`\n- `pages_range`\n- `all_pages`\n- `first_page?`\n- `last_page?`\n- `previous_page_path`\n- `next_page_path`\n- `n_page_path`\n- `paginate`\n\n\n### View\n\n#### `paginate(page)`\n\nReturns `\u003cnav\u003e` tag with links to first, last and closest pages. For example:\n\n```ruby\npaginate(:items) # where `:items` is a named route\n```\n\nwhen there is 11 pages, will returns:\n\n```html\n\u003cnav class=\"pagination\"\u003e\n  \u003ca href=\"/items?page=1\" class=\"pagination-first-page\"\u003e\n    1\n  \u003c/a\u003e\n  \u003cspan class=\"pagination-ellipsis\"\u003e\n    ...\n  \u003c/span\u003e\n  \u003ca href=\"/items?page=4\" class=\"pagination-previous-page\"\u003e\n    4\n  \u003c/a\u003e\n  \u003cspan class=\"pagination-current-page\"\u003e\n    5\n  \u003c/span\u003e\n  \u003ca href=\"/items?page=6\" class=\"pagination-next-page\"\u003e\n    6\n  \u003c/a\u003e\n  \u003cspan class=\"pagination-ellipsis\"\u003e\n    ...\n  \u003c/span\u003e\n  \u003ca href=\"/items?page=11\" class=\"pagination-last-page\"\u003e\n    11\n  \u003c/a\u003e\n\u003c/nav\u003e\n\n```\nEvery elements has special css-classes, so it is easy to change pagination look.\n\n#### `next_page_url`\nReturns string with url to next page. Example:\n\n```ruby\nnext_page_url # =\u003e '/books?page=3'\n```\n\n#### `prev_page_url`\nReturns string with url to prev page. Example:\n\n```ruby\nprev_page_url # =\u003e '/books?page=1'\n```\n\n#### `page_url(page)`\nReturns string with url to specific page. Example:\n\n```ruby\npage_url(4) # =\u003e '/books?page=4'\n```\n\n#### `previous_page_path(page)`\nReturns string with `page` path and current `params` to prev page. Example:\n\n```ruby\n# params =\u003e { status: 'active' }\n# pager.current_page?(2) =\u003e true\nprevious_page_path(:books) # =\u003e '/books?status=active\u0026page=1'\n```\n\n#### `next_page_path(page)`\nReturns string with `page` path and current `params` to next page. Example:\n\n```ruby\n# params =\u003e { status: 'inactive' }\n# pager.current_page?(1) =\u003e true\nprevious_page_path(:users) # =\u003e '/books?status=inactive\u0026page=2'\n```\n\n#### `n_page_path(page, n)`\nReturns string with `page` path and current `params` to specific page. Example:\n\n```ruby\n# params =\u003e { status: 'active' }\nprevious_page_path(:books, 10) # =\u003e '/books?status=active\u0026page=10'\n```\n\n### Testing\n\nYou can use `Hanami::Pagination::MockPager` class for testing you apps.\n\n#### View testing\n```ruby\nRSpec.describe Web::Views::Books::Show do\n  let(:mock_pager) { Hanami::Pagination::MockPager.new(current_page, total_pages) }\n  let(:pager) { Hanami::Pagination::Pager.new(mock_pager) }\n  let(:exposures) { Hash[pager: pager] }\n\n  let(:current_page) { 1 }\n  let(:total_pages) { 10 }\n\n  # ...\nend\n```\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavydovanton%2Fhanami-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavydovanton%2Fhanami-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavydovanton%2Fhanami-pagination/lists"}