-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add support for relationship-level links and meta attributes. #1454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this an APIObject? If so, it should be Relationship. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bf4 should this be namespaced under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we follow that pattern, yes. It was always about how we feel about it making sense #1004 (comment) and #1482 |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
class ResourceIdentifier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ApiObject? |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be on the same page here, that would change the current behaviour, right? (see https://github.com/beauby/active_model_serializers/blob/master/lib/active_model/serializer/adapter/json_api.rb#L125) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, I did indeedy mean to call it on the serializer, but @groyoh thanks for pointing out my error that this was just a direct port of the method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh you're right, I didn't realized that |
||
if serializer.respond_to?(:id) | ||
serializer.id | ||
else | ||
serializer.object.id | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ class Serializer | |
# @example | ||
# Association.new(:comments, CommentSummarySerializer) | ||
# | ||
Association = Struct.new(:name, :serializer, :options) do | ||
Association = Struct.new(:name, :serializer, :options, :links, :meta) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSON API concerns creeping into the serializer.. hmm |
||
# @return [Symbol] | ||
# | ||
def key | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for the instance variables here?