Skip to content

Commit

Permalink
Move ApiObjects to JsonApi namespace/folder
Browse files Browse the repository at this point in the history
Follow up to
https://github.com/rails-api/active_model_serializers/pull/1504/files#r52796473.
Moved the Relationship and ResourceIdentifier to the JsonApi
namespace/folder and slightly refactored the Relationship code.
  • Loading branch information
Yohan Robert committed Feb 28, 2016
1 parent 8a04005 commit e076111
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 331 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Fixes:
- [#1488](https://github.com/rails-api/active_model_serializers/pull/1488) Require ActiveSupport's string inflections (@nate00)

Misc:
- [#1529](https://github.com/rails-api/active_model_serializers/pull/1529) Move ApiObjects to the JsonApi namespace
as a follow up to #1504. (@groyoh)
- [#1497](https://github.com/rails-api/active_model_serializers/pull/1497) Add JRuby-9000 to appveyor.yml(@corainchicago)

### v0.10.0.rc4 (2016/01/27 11:00 +00:00)
Expand Down
13 changes: 0 additions & 13 deletions lib/active_model/serializer/adapter/json_api/api_objects.rb

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 5 additions & 4 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class JsonApi < Base
autoload :Link
require 'active_model/serializer/adapter/json_api/meta'
autoload :Deserialization
require 'active_model/serializer/adapter/json_api/api_objects'
autoload :ResourceIdentifier
autoload :Relationship

# TODO: if we like this abstraction and other API objects to it,
# then extract to its own file and require it.
Expand Down Expand Up @@ -98,7 +99,7 @@ def resource_objects_for(serializers)
end

def process_resource(serializer, primary)
resource_identifier = ActiveModel::Serializer::Adapter::JsonApi::ApiObjects::ResourceIdentifier.new(serializer).as_json
resource_identifier = ResourceIdentifier.new(serializer).as_json
return false unless @resource_identifiers.add?(resource_identifier)

resource_object = resource_object_for(serializer)
Expand Down Expand Up @@ -134,7 +135,7 @@ def attributes_for(serializer, fields)

def resource_object_for(serializer)
resource_object = cache_check(serializer) do
resource_object = ActiveModel::Serializer::Adapter::JsonApi::ApiObjects::ResourceIdentifier.new(serializer).as_json
resource_object = ResourceIdentifier.new(serializer).as_json

requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
attributes = attributes_for(serializer, requested_fields)
Expand All @@ -158,7 +159,7 @@ def resource_object_for(serializer)
def relationships_for(serializer, requested_associations)
include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(requested_associations)
serializer.associations(include_tree).each_with_object({}) do |association, hash|
hash[association.key] = ActiveModel::Serializer::Adapter::JsonApi::ApiObjects::Relationship.new(
hash[association.key] = Relationship.new(
serializer,
association.serializer,
association.options,
Expand Down
51 changes: 51 additions & 0 deletions lib/active_model_serializers/adapter/json_api/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module ActiveModelSerializers
module Adapter
class JsonApi
class Relationship
def initialize(parent_serializer, serializer, options = {}, links = {}, meta = nil)
@options = options
@data = data_for(serializer, options)
@links = links_for(parent_serializer, links)
@meta = meta_for(parent_serializer, meta)
end

def as_json
hash = {}
hash[:data] = data if options[:include_data]
links = self.links
hash[:links] = links if links.any?
meta = self.meta
hash[:meta] = meta if meta

hash
end

protected

attr_reader :data, :options, :links, :meta

private

def links_for(parent_serializer, links)
links.each_with_object({}) do |(key, value), hash|
hash[key] = Link.new(parent_serializer, value).as_json
end
end

def meta_for(parent_serializer, meta)
meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
end

def data_for(serializer, options)
if serializer.respond_to?(:each)
serializer.map { |s| ResourceIdentifier.new(s).as_json }
elsif options[:virtual_value]
options[:virtual_value]
elsif serializer && serializer.object
ResourceIdentifier.new(serializer).as_json
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module ActiveModelSerializers
module Adapter
class JsonApi
class ResourceIdentifier
def initialize(serializer)
@id = id_for(serializer)
@type = type_for(serializer)
end

def as_json
{ id: id, type: type }
end

protected

attr_reader :id, :type

private

def type_for(serializer)
return serializer._type if serializer._type
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
serializer.object.class.model_name.singular
else
serializer.object.class.model_name.plural
end
end

def id_for(serializer)
serializer.read_attribute_for_serialization(:id).to_s
end
end
end
end
end
Loading

0 comments on commit e076111

Please sign in to comment.