Skip to content

Commit

Permalink
return complete URIs on pagination links
Browse files Browse the repository at this point in the history
  • Loading branch information
bacarini committed Aug 10, 2015
1 parent a61ec32 commit 018b71c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 16 deletions.
8 changes: 4 additions & 4 deletions docs/howto/add_pagination_links.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ ex:
}
],
"links": {
"first": "?page=1&per_page=1",
"prev": "?page=2&per_page=1",
"next": "?page=4&per_page=1",
"last": "?page=13&per_page=1"
"first": "http://example.com/articles?page=1&per_page=1",
"prev": "http://example.com/articles?page=2&per_page=1",
"next": "http://example.com/articles?page=4&per_page=1",
"last": "http://example.com/articles?page=13&per_page=1"
}
}
```
5 changes: 5 additions & 0 deletions lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def get_serializer(resource, options = {})
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
options[:adapter] = false
end
options[:original_url] = original_url
ActiveModel::SerializableResource.serialize(resource, options) do |serializable_resource|
if serializable_resource.serializer?
serializable_resource.serialization_scope ||= serialization_scope
Expand Down Expand Up @@ -57,5 +58,9 @@ def serialization_scope(scope)
self._serialization_scope = scope
end
end

def original_url
request.original_url.sub(/\?.*$/, "")
end
end
end
4 changes: 2 additions & 2 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def serializable_hash(options = nil)
end
end

include_pagination_links if serializer.pagination
include_pagination_links if serializer.options[:pagination]
else
@hash[:data] = attributes_for_serializer(serializer, options)
add_resource_relationships(@hash[:data], serializer)
Expand Down Expand Up @@ -168,7 +168,7 @@ def include_pagination_links
end

def page_links
@links ||= JsonApi::PaginationLinks.new(serializer.resource).page_links
@links ||= JsonApi::PaginationLinks.new(serializer.resource, serializer.options).page_links
end

def links?
Expand Down
16 changes: 13 additions & 3 deletions lib/active_model/serializer/adapter/json_api/pagination_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ class JsonApi < Adapter
class PaginationLinks
FIRST_PAGE = 1

attr_reader :collection
attr_reader :collection, :options

def initialize(collection)
def initialize(collection, options={})
raise_unless_any_gem_installed
@collection = collection
@options = options
end

def page_links
Expand All @@ -20,7 +21,7 @@ def page_links

def build_links
pages_from.each_with_object({}) do |(key, value), hash|
hash[key] = "?page=#{value}&per_page=#{collection.size}"
hash[key] = "#{url}?page=#{value}&per_page=#{collection.size}"
end
end

Expand All @@ -45,6 +46,15 @@ def raise_unless_any_gem_installed
raise "AMS relies on either Kaminari or WillPaginate." +
"Please install either dependency by adding one of those to your Gemfile"
end

def url
return default_url unless options && options[:links] && options[:links][:self]
options[:links][:self]
end

def default_url
options[:original_url]
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ArraySerializer
include Enumerable
delegate :each, to: :@objects

attr_reader :root, :meta, :meta_key, :pagination, :resource
attr_reader :root, :meta, :meta_key, :options, :resource

def initialize(objects, options = {})
@root = options[:root]
Expand All @@ -24,7 +24,7 @@ def initialize(objects, options = {})
end
@meta = options[:meta]
@meta_key = options[:meta_key]
@pagination = options[:pagination]
@options = options
end

def json_key
Expand Down
16 changes: 12 additions & 4 deletions test/action_controller/json_api/pagination_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,37 @@ def render_array_omitting_pagination_options
tests PaginationTestController

def test_render_pagination_links_with_will_paginate
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=1&per_page=1", "next"=>"?page=3&per_page=1", "last"=>"?page=3&per_page=1"}
expected_links = {"first"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=1&per_page=1",
"prev"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=1&per_page=1",
"next"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=3&per_page=1",
"last"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=3&per_page=1"}

get :render_pagination_using_will_paginate, page: 2, per_page: 1
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_render_only_last_and_next_pagination_links
expected_links = {"next"=>"?page=2&per_page=2", "last"=>"?page=2&per_page=2"}
expected_links = {"next"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=2&per_page=2",
"last"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=2&per_page=2"}
get :render_pagination_using_will_paginate, page: 1, per_page: 2
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_render_pagination_links_with_kaminari
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=1&per_page=1", "next"=>"?page=3&per_page=1", "last"=>"?page=3&per_page=1"}
expected_links = {"first"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=1&per_page=1",
"prev"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=1&per_page=1",
"next"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=3&per_page=1",
"last"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=3&per_page=1"}
get :render_pagination_using_kaminari, page: 2, per_page: 1
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_render_only_prev_and_first_pagination_links
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=2&per_page=1"}
expected_links = {"first"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=1&per_page=1",
"prev"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=2&per_page=1"}
get :render_pagination_using_kaminari, page: 3, per_page: 1
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
Expand Down
6 changes: 6 additions & 0 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ def test_warn_overridding_use_adapter_as_falsy_on_controller_instance
def use_adapter?
false
end
def original_url
"http://example.com/"
end
}.new
assert_match /adapter: false/, (capture(:stderr) {
controller.get_serializer(Profile.new)
Expand All @@ -415,6 +418,9 @@ def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
def use_adapter?
true
end
def original_url
"http://example.com/"
end
}.new
assert_equal "", (capture(:stderr) {
controller.get_serializer(Profile.new)
Expand Down
2 changes: 1 addition & 1 deletion test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_json_key_with_root_and_no_serializers

def test_pagination_attr_readers
serializer = ArraySerializer.new(@resource, pagination: true)
assert_equal serializer.pagination, true
assert_equal serializer.options[:pagination], true
end

def test_resource
Expand Down

0 comments on commit 018b71c

Please sign in to comment.