Skip to content

Commit

Permalink
Pass serialization context into adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Apr 1, 2016
1 parent 2c26019 commit ec824c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
6 changes: 3 additions & 3 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def success_document(options)

if is_collection && serializer.paginated?
hash[:links] ||= {}
hash[:links].update(pagination_links_for(serializer, options))
hash[:links].update(pagination_links_for(serializer))
end

hash
Expand Down Expand Up @@ -498,8 +498,8 @@ def links_for(serializer)
# end
# prs:
# https://github.com/rails-api/active_model_serializers/pull/1041
def pagination_links_for(serializer, options)
PaginationLinks.new(serializer.object, options).as_json
def pagination_links_for(serializer)
PaginationLinks.new(serializer.object, instance_options).as_json
end

# {http://jsonapi.org/format/#document-meta Docment Meta}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ class PaginationLinks

attr_reader :collection, :context

def initialize(collection, options)
def initialize(collection, adapter_options)
@collection = collection
@options = options
@context = options.fetch(:serialization_context)
@adapter_options = adapter_options
@context = adapter_options.fetch(:serialization_context)
end

def as_json
per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
pages_from.each_with_object({}) do |(key, value), hash|
params = query_parameters.merge(page: { number: value, size: per_page }).to_query

hash[key] = "#{url(options)}?#{params}"
hash[key] = "#{url(adapter_options)}?#{params}"
end
end

protected

attr_reader :options
attr_reader :adapter_options

private

Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers/serializable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ActiveModelSerializers
class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links, :serialization_context])
include ActiveModelSerializers::Logging

delegate :serializable_hash, :as_json, :to_json, to: :adapter
Expand Down
38 changes: 17 additions & 21 deletions test/adapter/json_api/pagination_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ def mock_request(query_parameters = {}, original_url = URI)
context.expect(:request_url, original_url)
context.expect(:query_parameters, query_parameters)
context.expect(:key_transform, nil)
@options = {}
@options[:serialization_context] = context
end

def load_adapter(paginated_collection, options = {})
options = options.merge(adapter: :json_api)
ActiveModelSerializers::SerializableResource.new(paginated_collection, options)
def load_adapter(paginated_collection, mock_request = nil)
render_options = { adapter: :json_api }
if mock_request
render_options[:serialization_context] = mock_request
end
serializable(paginated_collection, render_options)
end

def using_kaminari(page = 2)
Expand Down Expand Up @@ -103,43 +104,38 @@ def expected_response_with_last_page_pagination_links
end

def test_pagination_links_using_kaminari
adapter = load_adapter(using_kaminari)
adapter = load_adapter(using_kaminari, mock_request)

mock_request
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
end

def test_pagination_links_using_will_paginate
adapter = load_adapter(using_will_paginate)
adapter = load_adapter(using_will_paginate, mock_request)

mock_request
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
end

def test_pagination_links_with_additional_params
adapter = load_adapter(using_will_paginate)
adapter = load_adapter(using_will_paginate, mock_request({ test: 'test' }))

mock_request({ test: 'test' })
assert_equal expected_response_with_pagination_links_and_additional_params,
adapter.serializable_hash(@options)
adapter.serializable_hash
end

def test_last_page_pagination_links_using_kaminari
adapter = load_adapter(using_kaminari(3))
adapter = load_adapter(using_kaminari(3), mock_request)

mock_request
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash(@options)
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash
end

def test_last_page_pagination_links_using_will_paginate
adapter = load_adapter(using_will_paginate(3))
adapter = load_adapter(using_will_paginate(3), mock_request)

mock_request
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash(@options)
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash
end

def test_not_showing_pagination_links
adapter = load_adapter(@array)
adapter = load_adapter(@array, mock_request)

assert_equal expected_response_without_pagination_links, adapter.serializable_hash
end
Expand Down

0 comments on commit ec824c9

Please sign in to comment.