-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for relationship-level links and meta.
- Loading branch information
Showing
7 changed files
with
177 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
lib/active_model/serializer/adapter/json_api/association.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
class Association | ||
def initialize(parent_serializer, serializer, options, links, meta) | ||
@object = parent_serializer.object | ||
@scope = parent_serializer.scope | ||
|
||
@options = options | ||
@data = data_for(serializer, options) | ||
@links = links | ||
.map { |key, value| { key => Link.new(parent_serializer, value).as_json } } | ||
.reduce({}, :merge) | ||
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta | ||
end | ||
|
||
def as_json | ||
hash = {} | ||
hash[:data] = @data if @options[:include_data] | ||
hash[:links] = @links if @links.any? | ||
hash[:meta] = @meta if @meta | ||
|
||
hash | ||
end | ||
|
||
protected | ||
|
||
attr_reader :object, :scope | ||
|
||
private | ||
|
||
def data_for(serializer, options) | ||
if serializer.respond_to?(:each) | ||
serializer.map { |s| ResourceIdentifier.new(s).as_json } | ||
else | ||
if options[:virtual_value] | ||
options[:virtual_value] | ||
elsif serializer && serializer.object | ||
ResourceIdentifier.new(serializer).as_json | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
41 changes: 41 additions & 0 deletions
41
lib/active_model/serializer/adapter/json_api/resource_identifier.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
class ResourceIdentifier | ||
def initialize(serializer) | ||
@id = id_for(serializer) | ||
@type = type_for(serializer) | ||
end | ||
|
||
def as_json | ||
{ id: @id.to_s, type: @type } | ||
end | ||
|
||
protected | ||
|
||
attr_reader :object, :scope | ||
|
||
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) | ||
if serializer.respond_to?(:id) | ||
serializer.id | ||
else | ||
serializer.object.id | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters