mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
send whole request context to model serializer
This commit is contained in:
parent
5031eb9f96
commit
01eab3bdb4
@ -25,11 +25,6 @@ module ActionController
|
|||||||
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
|
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
|
||||||
options[:adapter] = false
|
options[:adapter] = false
|
||||||
end
|
end
|
||||||
if resource.respond_to?(:current_page) && resource.respond_to?(:total_pages)
|
|
||||||
options[:pagination] = {}
|
|
||||||
options[:pagination][:original_url] = original_url
|
|
||||||
options[:pagination][:query_parameters] = query_parameters
|
|
||||||
end
|
|
||||||
ActiveModel::SerializableResource.serialize(resource, options) do |serializable_resource|
|
ActiveModel::SerializableResource.serialize(resource, options) do |serializable_resource|
|
||||||
if serializable_resource.serializer?
|
if serializable_resource.serializer?
|
||||||
serializable_resource.serialization_scope ||= serialization_scope
|
serializable_resource.serialization_scope ||= serialization_scope
|
||||||
@ -52,6 +47,7 @@ module ActionController
|
|||||||
|
|
||||||
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
||||||
define_method renderer_method do |resource, options|
|
define_method renderer_method do |resource, options|
|
||||||
|
options.fetch(:context) { options[:context] = request }
|
||||||
serializable_resource = get_serializer(resource, options)
|
serializable_resource = get_serializer(resource, options)
|
||||||
super(serializable_resource, options)
|
super(serializable_resource, options)
|
||||||
end
|
end
|
||||||
@ -62,13 +58,5 @@ module ActionController
|
|||||||
self._serialization_scope = scope
|
self._serialization_scope = scope
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def original_url
|
|
||||||
request.original_url[/\A[^?]+/]
|
|
||||||
end
|
|
||||||
|
|
||||||
def query_parameters
|
|
||||||
request.query_parameters
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -21,9 +21,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def as_json(options = nil)
|
def as_json(options = nil)
|
||||||
serializable_hash(options).tap do |hash|
|
hash = serializable_hash(options)
|
||||||
include_meta(hash)
|
include_meta(hash)
|
||||||
end
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create(resource, options = {})
|
def self.create(resource, options = {})
|
||||||
|
|||||||
@ -164,11 +164,18 @@ module ActiveModel
|
|||||||
def add_links(options)
|
def add_links(options)
|
||||||
links = @hash.fetch(:links) { {} }
|
links = @hash.fetch(:links) { {} }
|
||||||
resources = serializer.instance_variable_get(:@resource)
|
resources = serializer.instance_variable_get(:@resource)
|
||||||
@hash[:links] = add_pagination_links(links, resources, options) if options[:pagination]
|
@hash[:links] = add_pagination_links(links, resources, options) if is_paginated?(resources)
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_pagination_links(links, resources, options)
|
def add_pagination_links(links, resources, options)
|
||||||
links.update(JsonApi::PaginationLinks.new(resources).serializable_hash(options))
|
pagination_links = JsonApi::PaginationLinks.new(resources, options[:context]).serializable_hash(options)
|
||||||
|
links.update(pagination_links)
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_paginated?(resource)
|
||||||
|
resource.respond_to?(:current_page) &&
|
||||||
|
resource.respond_to?(:total_pages) &&
|
||||||
|
resource.respond_to?(:size)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,16 +5,15 @@ module ActiveModel
|
|||||||
class PaginationLinks
|
class PaginationLinks
|
||||||
FIRST_PAGE = 1
|
FIRST_PAGE = 1
|
||||||
|
|
||||||
attr_reader :collection
|
attr_reader :collection, :context
|
||||||
|
|
||||||
def initialize(collection)
|
def initialize(collection, context)
|
||||||
raise_unless_any_gem_installed
|
|
||||||
@collection = collection
|
@collection = collection
|
||||||
|
@context = context
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = {})
|
||||||
pages_from.each_with_object({}) do |(key, value), hash|
|
pages_from.each_with_object({}) do |(key, value), hash|
|
||||||
query_parameters = options[:pagination].fetch(:query_parameters) { {} }
|
|
||||||
params = query_parameters.merge(page: { number: value, size: collection.size }).to_query
|
params = query_parameters.merge(page: { number: value, size: collection.size }).to_query
|
||||||
|
|
||||||
hash[key] = "#{url(options)}?#{params}"
|
hash[key] = "#{url(options)}?#{params}"
|
||||||
@ -41,17 +40,17 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def raise_unless_any_gem_installed
|
|
||||||
return if defined?(WillPaginate) || defined?(Kaminari)
|
|
||||||
raise <<-EOF
|
|
||||||
AMS relies on either Kaminari or WillPaginate for pagination.
|
|
||||||
Please install either dependency by adding one of those to your Gemfile.
|
|
||||||
EOF
|
|
||||||
end
|
|
||||||
|
|
||||||
def url(options)
|
def url(options)
|
||||||
self_link = options.fetch(:links) {{}}
|
self_link = options.fetch(:links) {{}}
|
||||||
self_link.fetch(:self) {} ? options[:links][:self] : options[:pagination][:original_url]
|
self_link.fetch(:self) {} ? options[:links][:self] : original_url
|
||||||
|
end
|
||||||
|
|
||||||
|
def original_url
|
||||||
|
@original_url ||= context.original_url[/\A[^?]+/]
|
||||||
|
end
|
||||||
|
|
||||||
|
def query_parameters
|
||||||
|
@query_parameters ||= context.query_parameters
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -9,6 +9,8 @@ module ActiveModel
|
|||||||
class Adapter
|
class Adapter
|
||||||
class JsonApi
|
class JsonApi
|
||||||
class PaginationLinksTest < Minitest::Test
|
class PaginationLinksTest < Minitest::Test
|
||||||
|
URI = 'http://example.com'
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
ActionController::Base.cache_store.clear
|
ActionController::Base.cache_store.clear
|
||||||
@array = [
|
@array = [
|
||||||
@ -18,6 +20,14 @@ module ActiveModel
|
|||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mock_request(query_parameters={}, original_url=URI)
|
||||||
|
context = Minitest::Mock.new
|
||||||
|
context.expect(:original_url, original_url )
|
||||||
|
context.expect(:query_parameters, query_parameters)
|
||||||
|
@options = {}
|
||||||
|
@options[:context] = context
|
||||||
|
end
|
||||||
|
|
||||||
def using_kaminari
|
def using_kaminari
|
||||||
Kaminari.paginate_array(@array).page(2).per(1)
|
Kaminari.paginate_array(@array).page(2).per(1)
|
||||||
end
|
end
|
||||||
@ -38,11 +48,11 @@ module ActiveModel
|
|||||||
def links
|
def links
|
||||||
{
|
{
|
||||||
links:{
|
links:{
|
||||||
self: "http://example.com?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
||||||
first: "http://example.com?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||||
prev: "http://example.com?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||||
next: "http://example.com?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||||
last: "http://example.com?page%5Bnumber%5D=3&page%5Bsize%5D=1"
|
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -59,7 +69,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def expected_response_with_pagination_links_and_additional_params
|
def expected_response_with_pagination_links_and_additional_params
|
||||||
new_links = links[:links].each_with_object({}) {|(key, value), hash| hash[key] = "#{value}&teste=teste" }
|
new_links = links[:links].each_with_object({}) {|(key, value), hash| hash[key] = "#{value}&test=test" }
|
||||||
{}.tap do |hash|
|
{}.tap do |hash|
|
||||||
hash[:data] = [data.values.flatten.second]
|
hash[:data] = [data.values.flatten.second]
|
||||||
hash.merge! links: new_links
|
hash.merge! links: new_links
|
||||||
@ -70,25 +80,25 @@ module ActiveModel
|
|||||||
serializer = ArraySerializer.new(using_kaminari)
|
serializer = ArraySerializer.new(using_kaminari)
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
|
|
||||||
assert_equal expected_response_with_pagination_links,
|
mock_request
|
||||||
adapter.serializable_hash(pagination: { original_url: "http://example.com" })
|
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pagination_links_using_will_paginate
|
def test_pagination_links_using_will_paginate
|
||||||
serializer = ArraySerializer.new(using_will_paginate)
|
serializer = ArraySerializer.new(using_will_paginate)
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
|
|
||||||
assert_equal expected_response_with_pagination_links,
|
mock_request
|
||||||
adapter.serializable_hash(pagination: { original_url: "http://example.com" })
|
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pagination_links_with_additional_params
|
def test_pagination_links_with_additional_params
|
||||||
serializer = ArraySerializer.new(using_will_paginate)
|
serializer = ArraySerializer.new(using_will_paginate)
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
assert_equal expected_response_with_pagination_links_and_additional_params,
|
|
||||||
adapter.serializable_hash(pagination: { original_url: "http://example.com",
|
|
||||||
query_parameters: { teste: "teste"}})
|
|
||||||
|
|
||||||
|
mock_request({ test: 'test' })
|
||||||
|
assert_equal expected_response_with_pagination_links_and_additional_params,
|
||||||
|
adapter.serializable_hash(@options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_not_showing_pagination_links
|
def test_not_showing_pagination_links
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user