-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Follow up to #1454 #1504
Follow up to #1454 #1504
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,13 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
module ApiObjects | ||
extend ActiveSupport::Autoload | ||
autoload :Relationship | ||
autoload :ResourceIdentifier | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
module ApiObjects | ||
class Relationship | ||
def initialize(parent_serializer, serializer, options = {}, links = {}, meta = nil) | ||
@object = parent_serializer.object | ||
@scope = parent_serializer.scope | ||
|
||
@options = options | ||
@data = data_for(serializer, options) | ||
@links = links.each_with_object({}) do |(key, value), hash| | ||
hash[key] = Link.new(parent_serializer, value).as_json | ||
end | ||
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : 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 :object, :scope, :data, :options, :links, :meta | ||
|
||
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 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
module ApiObjects | ||
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 | ||
end | ||
end |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,25 +42,30 @@ def initialize(*) | |
|
||
def link(name, value = nil, &block) | ||
@_links[name] = block || value | ||
nil | ||
:nil | ||
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. Not sure if this should be called 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. this is to signify something like 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. Yes. For more details, there are two use cases to consider for the relationship block:
has_one :author do
meta some: :thing
object.own_author
end
has_one :author do
meta some: :thing
end For the latest, I defined the value to 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. Maybe create an issue or start a pr to discuss any improvements B mobile phone
|
||
end | ||
|
||
def meta(value = nil, &block) | ||
@_meta = block || value | ||
nil | ||
:nil | ||
end | ||
|
||
def include_data(value = true) | ||
@_include_data = value | ||
nil | ||
:nil | ||
end | ||
|
||
def value(serializer) | ||
@object = serializer.object | ||
@scope = serializer.scope | ||
|
||
if block | ||
instance_eval(&block) | ||
block_value = instance_eval(&block) | ||
if block_value == :nil | ||
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. This assumes that a relationship value (or at least a relationship defined in a block value) MUST never 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. I think that's right. ref https://github.com/rails-api/active_model_serializers/pull/1504/files#r52399639 |
||
serializer.read_attribute_for_serialization(name) | ||
else | ||
block_value | ||
end | ||
else | ||
serializer.read_attribute_for_serialization(name) | ||
end | ||
|
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.
So, in https://github.com/rails-api/active_model_serializers/pull/1004/files#r52563837 I noticed that we've already started a pattern of sticking the json_api objects in the json_api folder. e.g. above we have Meta and Link which are also json_api object.
In code I removed I move that object into
json_api/jsonapi.rb
. What do you think about just sticking also the api objects in thejson_api
folder as we've essentially already been doing?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.
It's fine by me. Should I still keep the
ApiObjects
namespace?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.
NEEDS FOLLOWUP