Skip to content

Commit

Permalink
Merge pull request #1121 from beauby/fix-jsonapi-links
Browse files Browse the repository at this point in the history
Refactor `add_links` in JSONAPI adapter.
  • Loading branch information
joaomdmoura committed Sep 15, 2015
2 parents d7534a6 + 285cdf8 commit 479146c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
70 changes: 35 additions & 35 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt

def initialize(serializer, options = {})
super
@hash = { data: [] }

@included = ActiveModel::Serializer::Utils.include_args_to_hash(@options[:include])
fields = options.delete(:fields)
if fields
Expand All @@ -19,26 +17,10 @@ def initialize(serializer, options = {})
def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
serializer.each do |s|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
@hash[:data] << result[:data]

if result[:included]
@hash[:included] ||= []
@hash[:included] |= result[:included]
end
end

add_links(options)
serializable_hash_for_collection(serializer, options)
else
primary_data = primary_data_for(serializer, options)
relationships = relationships_for(serializer)
included = included_for(serializer)
@hash[:data] = primary_data
@hash[:data][:relationships] = relationships if relationships.any?
@hash[:included] = included if included.any?
serializable_hash_for_single_resource(serializer, options)
end
@hash
end

def fragment_cache(cached_hash, non_cached_hash)
Expand All @@ -48,6 +30,37 @@ def fragment_cache(cached_hash, non_cached_hash)

private

def serializable_hash_for_collection(serializer, options)
hash = { data: [] }
serializer.each do |s|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
hash[:data] << result[:data]

if result[:included]
hash[:included] ||= []
hash[:included] |= result[:included]
end
end

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

hash
end

def serializable_hash_for_single_resource(serializer, options)
primary_data = primary_data_for(serializer, options)
relationships = relationships_for(serializer)
included = included_for(serializer)
hash = { data: primary_data }
hash[:data][:relationships] = relationships if relationships.any?
hash[:included] = included if included.any?

hash
end

def resource_identifier_type_for(serializer)
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
serializer.object.class.model_name.singular
Expand Down Expand Up @@ -139,20 +152,7 @@ def _included_for(serializer, includes)
end
end

def add_links(options)
links = @hash.fetch(:links) { {} }
collection = serializer.object
@hash[:links] = add_pagination_links(links, collection, options) if paginated?(collection)
end

def add_pagination_links(links, resources, options)
pagination_links = ActiveModel::Serializer::Adapter::JsonApi::PaginationLinks.new(resources, options[:context]).serializable_hash(options)
links.update(pagination_links)
end

def paginated?(collection)
collection.respond_to?(:current_page) &&
collection.respond_to?(:total_pages) &&
collection.respond_to?(:size)
def links_for(serializer, options)
JsonApi::PaginationLinks.new(serializer.object, options[:context]).serializable_hash(options)
end
end
6 changes: 6 additions & 0 deletions lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def json_key
key = root || @serializers.first.try(:json_key) || object.try(:name).try(:underscore)
key.try(:pluralize)
end

def paginated?
object.respond_to?(:current_page) &&
object.respond_to?(:total_pages) &&
object.respond_to?(:size)
end
end
end
end

0 comments on commit 479146c

Please sign in to comment.