remove resource and options attr_reader from array_serialize

This commit is contained in:
Bruno Bacarini 2015-08-11 14:10:50 -03:00
parent 7be25fef14
commit e0d050d2af
6 changed files with 35 additions and 54 deletions

View File

@ -2,7 +2,7 @@ require "set"
module ActiveModel module ActiveModel
class SerializableResource class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter]) ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :pagination])
def initialize(resource, options = {}) def initialize(resource, options = {})
@resource = resource @resource = resource

View File

@ -20,7 +20,7 @@ module ActiveModel
options ||= {} options ||= {}
if serializer.respond_to?(:each) if serializer.respond_to?(:each)
serializer.each do |s| serializer.each do |s|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options) result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash({})
@hash[:data] << result[:data] @hash[:data] << result[:data]
if result[:included] if result[:included]
@ -29,9 +29,9 @@ module ActiveModel
end end
end end
include_pagination_links if serializer.options[:pagination] add_links(options)
else else
@hash[:data] = attributes_for_serializer(serializer, options) @hash[:data] = attributes_for_serializer(serializer, {})
add_resource_relationships(@hash[:data], serializer) add_resource_relationships(@hash[:data], serializer)
end end
@hash @hash
@ -161,18 +161,14 @@ module ActiveModel
end end
end end
def include_pagination_links def add_links(options)
return if page_links.empty? links = @hash.fetch(:links) { {} }
resources = serializer.instance_variable_get(:@resource)
links? ? @hash[:links].merge!(page_links) : @hash[:links] = page_links @hash[:links] = add_pagination_links(links, resources, options) if @options[:pagination]
end end
def page_links def add_pagination_links(links, resources, options)
@links ||= JsonApi::PaginationLinks.new(serializer.resource, serializer.options).page_links links.update(JsonApi::PaginationLinks.new(resources).serializable_hash(options))
end
def links?
!@hash[:links].nil?
end end
end end
end end

View File

@ -5,18 +5,19 @@ module ActiveModel
class PaginationLinks class PaginationLinks
FIRST_PAGE = 1 FIRST_PAGE = 1
attr_reader :collection, :options attr_reader :collection
def initialize(collection, options={}) def initialize(collection)
raise_unless_any_gem_installed raise_unless_any_gem_installed
@collection = collection @collection = collection
@options = options
end end
def page_links 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.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}?#{params}"
hash[key] = "#{url(options)}?#{params}"
end end
end end
@ -44,17 +45,9 @@ module ActiveModel
"Please install either dependency by adding one of those to your Gemfile" "Please install either dependency by adding one of those to your Gemfile"
end end
def url def url(options)
return default_url unless options && options[:links] && options[:links][:self] self_link = options.fetch(:links) {{}}
options[:links][:self] self_link.fetch(:self) {} ? options[:links][:self] : options[:original_url]
end
def default_url
options[:original_url]
end
def query_parameters
options[:query_parameters] ? options[:query_parameters] : {}
end end
end end
end end

View File

@ -5,7 +5,7 @@ module ActiveModel
include Enumerable include Enumerable
delegate :each, to: :@objects delegate :each, to: :@objects
attr_reader :root, :meta, :meta_key, :options, :resource attr_reader :root, :meta, :meta_key
def initialize(objects, options = {}) def initialize(objects, options = {})
@root = options[:root] @root = options[:root]
@ -24,7 +24,6 @@ module ActiveModel
end end
@meta = options[:meta] @meta = options[:meta]
@meta_key = options[:meta_key] @meta_key = options[:meta_key]
@options = options
end end
def json_key def json_key

View File

@ -64,31 +64,33 @@ module ActiveModel
end end
def test_pagination_links_using_kaminari def test_pagination_links_using_kaminari
serializer = ArraySerializer.new(using_kaminari, pagination: true, original_url: "http://example.com") serializer = ArraySerializer.new(using_kaminari)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, pagination: true)
assert_equal expected_response_with_pagination_links, adapter.serializable_hash assert_equal expected_response_with_pagination_links,
adapter.serializable_hash(original_url: "http://example.com")
end end
def test_pagination_links_using_will_paginate def test_pagination_links_using_will_paginate
serializer = ArraySerializer.new(using_will_paginate, pagination: true, original_url: "http://example.com") serializer = ArraySerializer.new(using_will_paginate)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, pagination: true)
assert_equal expected_response_with_pagination_links, adapter.serializable_hash assert_equal expected_response_with_pagination_links,
adapter.serializable_hash(original_url: "http://example.com")
end end
def test_pagination_links_with_additional_params def test_pagination_links_with_additional_params
serializer = ArraySerializer.new(using_will_paginate, pagination: true, serializer = ArraySerializer.new(using_will_paginate)
original_url: "http://example.com", adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, pagination: true)
query_parameters: { teste: "teste"}) assert_equal expected_response_with_pagination_links_and_additional_params,
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter.serializable_hash(original_url: "http://example.com",
query_parameters: { teste: "teste"})
assert_equal expected_response_with_pagination_links_and_additional_params, adapter.serializable_hash
end end
def test_not_showing_pagination_links def test_not_showing_pagination_links
serializer = ArraySerializer.new(using_will_paginate, pagination: false) serializer = ArraySerializer.new(using_will_paginate)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, pagination: false)
assert_equal expected_response_without_pagination_links, adapter.serializable_hash assert_equal expected_response_without_pagination_links, adapter.serializable_hash
end end

View File

@ -92,15 +92,6 @@ module ActiveModel
serializer = ArraySerializer.new(build_named_collection, root: 'custom_root') serializer = ArraySerializer.new(build_named_collection, root: 'custom_root')
assert_equal serializer.json_key, 'custom_roots' assert_equal serializer.json_key, 'custom_roots'
end end
def test_pagination_attr_readers
serializer = ArraySerializer.new(@resource, pagination: true)
assert_equal serializer.options[:pagination], true
end
def test_resource
assert_equal @serializer.resource, @resource
end
end end
end end
end end