send whole request context to model serializer

This commit is contained in:
Bruno Bacarini
2015-08-18 19:05:20 -03:00
parent 5031eb9f96
commit 01eab3bdb4
5 changed files with 48 additions and 44 deletions

View File

@@ -25,11 +25,6 @@ module ActionController
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
options[:adapter] = false
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|
if serializable_resource.serializer?
serializable_resource.serialization_scope ||= serialization_scope
@@ -52,6 +47,7 @@ module ActionController
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
define_method renderer_method do |resource, options|
options.fetch(:context) { options[:context] = request }
serializable_resource = get_serializer(resource, options)
super(serializable_resource, options)
end
@@ -62,13 +58,5 @@ module ActionController
self._serialization_scope = scope
end
end
def original_url
request.original_url[/\A[^?]+/]
end
def query_parameters
request.query_parameters
end
end
end

View File

@@ -21,9 +21,9 @@ module ActiveModel
end
def as_json(options = nil)
serializable_hash(options).tap do |hash|
include_meta(hash)
end
hash = serializable_hash(options)
include_meta(hash)
hash
end
def self.create(resource, options = {})

View File

@@ -164,11 +164,18 @@ module ActiveModel
def add_links(options)
links = @hash.fetch(:links) { {} }
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
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

View File

@@ -5,16 +5,15 @@ module ActiveModel
class PaginationLinks
FIRST_PAGE = 1
attr_reader :collection
attr_reader :collection, :context
def initialize(collection)
raise_unless_any_gem_installed
def initialize(collection, context)
@collection = collection
@context = context
end
def serializable_hash(options = {})
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
hash[key] = "#{url(options)}?#{params}"
@@ -41,17 +40,17 @@ module ActiveModel
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)
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