Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to configure the cursor/param name #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,25 @@ new comments are posted, as the cache key instead.
When a controller action sets an ETag and uses geared pagination, the current page and gear ratios are
automatically included in the ETag.


## Configuration

The `cursor_name` configuration option determines the name of the query parameter used for pagination cursors in the URLs generated by Geared Pagination. By default, the `cursor_name` is set to `:page`.

If you want to use a different query parameter name for the pagination cursor, you can set the `cursor_name` configuration option to a different value:

```ruby
GearedPagination.configure do |config|
config.cursor_name = :cursor
end
```

Creating links is now possible using the cursor parameter:

```ruby
<%= link_to "Next page", messages_path(cursor: @page.next_param) %>
```


## License
Geared Pagination is released under the [MIT License](https://opensource.org/licenses/MIT).
6 changes: 5 additions & 1 deletion lib/geared_pagination/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def geared_page?
end

def current_page_param
params[:page]
params[page_param_name]
end

def page_param_name
GearedPagination.config.cursor_name
end
end
end
26 changes: 22 additions & 4 deletions lib/geared_pagination/engine.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
require 'singleton'
require 'rails/engine'
require 'geared_pagination/controller'

class GearedPagination::Engine < ::Rails::Engine
initializer :geared_pagination do |app|
ActiveSupport.on_load :action_controller do
ActionController::Base.send :include, GearedPagination::Controller
module GearedPagination
class << self
def configure
yield config
end

def config
@_config ||= Configuration.new
end
end

class Configuration
include ActiveSupport::Configurable
config_accessor :cursor_name, default: :page
end

class Engine < ::Rails::Engine
initializer :geared_pagination do |app|
ActiveSupport.on_load :action_controller do
ActionController::Base.send :include, GearedPagination::Controller
end
end
end
end
6 changes: 5 additions & 1 deletion lib/geared_pagination/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ def link_header(rel:, page:)

def uri(page:)
Addressable::URI.parse(request.url).tap do |uri|
uri.query_values = (uri.query_values || {}).merge("page" => page)
uri.query_values = (uri.query_values || {}).merge("#{cursor_name}".to_sym => page)
end.to_s
end

def cursor_name
GearedPagination.config.cursor_name
end
end
end
59 changes: 59 additions & 0 deletions test/cursor_name_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'test_helper'
require 'active_support/core_ext/string/inquiry'
require 'geared_pagination/headers'
require 'geared_pagination/recordset'

class GearedPagination::HeadersTest < ActiveSupport::TestCase
Request = Struct.new(:url, :format)
Controller = Struct.new(:request, :headers)

setup do
create_recordings

@controller_serving_json = Controller.new(Request.new("http://example.com/recordset.json", "json".inquiry), {})
@controller_serving_html = Controller.new(Request.new("http://example.com/recordset", "html".inquiry), {})

@single_page_recordset = GearedPagination::Recordset.new(Recording.all, per_page: 1000)
@many_page_recordset = GearedPagination::Recordset.new(Recording.all, per_page: 1)
end

test "total count with custom cursor name" do
GearedPagination::Headers.new(page: @many_page_recordset.page(1), controller: @controller_serving_json).apply
assert_equal Recording.all.count.to_s, @controller_serving_json.headers["X-Total-Count"]
end

test "no link for html requests with custom cursor name" do
GearedPagination::Headers.new(page: @many_page_recordset.page(1), controller: @controller_serving_html).apply
assert @controller_serving_html.headers["Link"].nil?
end

test "no link for json request with single page with custom cursor name" do
GearedPagination::Headers.new(page: @single_page_recordset.page(1), controller: @controller_serving_json).apply
assert @controller_serving_html.headers["Link"].nil?
end

test "links for json request with multiple pages with custom cursor name" do
begin
GearedPagination.configure do |config|
config.cursor_name = :cursor
end

GearedPagination::Headers.new(page: @many_page_recordset.page(1), controller: @controller_serving_json).apply
assert_equal '<http://example.com/recordset.json?cursor=2>; rel="next"',
@controller_serving_json.headers["Link"]

GearedPagination::Headers.new(page: @many_page_recordset.page(2), controller: @controller_serving_json).apply
assert_equal '<http://example.com/recordset.json?cursor=3>; rel="next"',
@controller_serving_json.headers["Link"]
ensure
GearedPagination.configure do |config|
config.cursor_name = :page
end
end
end

test "no link for json request with multiple pages on last page with custom cursor name" do
GearedPagination::Headers.new(page: @many_page_recordset.page(Recording.all.count), controller: @controller_serving_json).apply
assert @controller_serving_json.headers["Link"].nil?
end
end