Skip to content

Commit

Permalink
Refactor resource_identifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
beauby committed Sep 1, 2015
1 parent d9c6805 commit c4faafd
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,25 @@ def fragment_cache(cached_hash, non_cached_hash)

private

def resource_identifier_type(serializer)
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
serializer.object.class.model_name.singular
else
serializer.object.class.model_name.plural
end
end

def resource_identifier_id(serializer)
if serializer.respond_to?('id')
serializer.id.to_s
else
serializer.object.id.to_s
end
end

def resource_identifier(serializer)
type = if ActiveModel::Serializer.config.jsonapi_resource_type == :plural
serializer.object.class.model_name.plural
else
serializer.object.class.model_name.singular
end
id = serializer.respond_to?('id') ? serializer.id.to_s : serializer.object.id.to_s
type = resource_identifier_type(serializer)
id = resource_identifier_id(serializer)

{ id: id, type: type }
end
Expand Down Expand Up @@ -125,8 +137,8 @@ def resource_relationship_value(serializer, options = {})
else
if options[:virtual_value]
options[:virtual_value]
elsif serializer.object
resurce_identifier(serializer)
elsif serializer && serializer.object
resource_identifier(serializer)
else
nil
end
Expand All @@ -135,19 +147,13 @@ def resource_relationship_value(serializer, options = {})

def add_resource_relationships(attrs, serializer, options = {})
options[:add_included] = options.fetch(:add_included, true)

attrs[:relationships] = {} if serializer.associations.any?
serializer.associations.each do |association|
key = association.key
serializer = association.serializer
options = association.options
value = resource_relationship_value(serializer, options)

value = resource_relationship_value(association.serializer, association.options)
attrs[:relationships][association.key] = { data: value }

if options[:add_included]
Array(serializer).each do |s|
add_included(key, s)
Array(association.serializer).each do |s|
add_included(association.key, s)
end
end
end
Expand Down

0 comments on commit c4faafd

Please sign in to comment.