-
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
Refactor JSONAPI adapter #1103
Refactor JSONAPI adapter #1103
Changes from 2 commits
8482abf
f95f736
343f8b9
d9c6805
c4faafd
0401205
f7612f2
91c5cbe
bae4951
c593adb
a8a0566
f8c553a
3793f3f
070a2e6
f27f13c
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 |
---|---|---|
|
@@ -44,19 +44,15 @@ def fragment_cache(cached_hash, non_cached_hash) | |
|
||
private | ||
|
||
def add_relationships(resource, name, serializers) | ||
resource[:relationships] ||= {} | ||
resource[:relationships][name] ||= { data: [] } | ||
resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.json_api_type, id: serializer.id.to_s } } | ||
end | ||
|
||
def add_relationship(resource, name, serializer, val=nil) | ||
resource[:relationships] ||= {} | ||
resource[:relationships][name] = { data: val } | ||
|
||
if serializer && serializer.object | ||
resource[:relationships][name][:data] = { type: serializer.json_api_type, id: serializer.id.to_s } | ||
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.object.id.to_s | ||
|
||
{ id: id, type: type } | ||
end | ||
|
||
def add_included(resource_name, serializers, parent = nil) | ||
|
@@ -100,15 +96,12 @@ def attributes_for_serializer(serializer, options) | |
|
||
def resource_object_for(serializer, options) | ||
options[:fields] = @fieldset && @fieldset.fields_for(serializer) | ||
options[:required_fields] = [:id, :json_api_type] | ||
|
||
cache_check(serializer) do | ||
attributes = serializer.attributes(options) | ||
attributes.delete(:id) | ||
|
||
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. So, we don't need 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. Exactly. |
||
result = { | ||
id: attributes.delete(:id).to_s, | ||
type: attributes.delete(:json_api_type) | ||
} | ||
result = resource_identifier(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. I missed why you're throwing out 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. Because we do not want it to be part of the 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. No, I get that.. but you're not using it, but it's there... that's what I mean by throwing it out... I'm sure there's a detail I'm missing in the refactor, but figured it'd be good to ask from ignorance :) 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 mean, is the serializer.attributes(options)[:id] the same as serializer.object.id 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. No, good catch. This change actually breaks 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. It is fixed now. |
||
|
||
result[:attributes] = attributes if attributes.any? | ||
result | ||
|
@@ -136,22 +129,24 @@ def check_assoc(assoc) | |
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 | ||
opts = association.options | ||
|
||
attrs[:relationships] ||= {} | ||
|
||
if serializer.respond_to?(:each) | ||
add_relationships(attrs, key, serializer) | ||
else | ||
if opts[:virtual_value] | ||
add_relationship(attrs, key, nil, opts[:virtual_value]) | ||
else | ||
add_relationship(attrs, key, serializer) | ||
end | ||
end | ||
value = if serializer.respond_to?(:each) | ||
serializer.map { |s| resource_identifier(s) } | ||
else | ||
if opts[:virtual_value] | ||
opts[:virtual_value] | ||
elsif serializer && serializer.object | ||
resource_identifier(serializer) | ||
else | ||
nil | ||
end | ||
end | ||
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. yeah, definitely a latent object in here for serializer and serializer collections... it would also make naming blocks easier e.g. 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. (That's OT, sorry... just thinking out loud) 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 agree for naming blocks but here there are already two local variables named |
||
|
||
attrs[:relationships][association.key] = { data: value } | ||
|
||
if options[:add_included] | ||
Array(serializer).each do |s| | ||
|
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.
This might be easier to read as a case statement
maybe
resource_type
andresource_id
as the local variable names?This method seems like there's a latent object in here that transforms a single resource into a serializable hash
Nice on the naming http://jsonapi.org/format/#document-resource-identifier-objects :)
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.
Agreed for
resource_type
andresource_id
.Could you elaborate about this "latent object"?