-
Notifications
You must be signed in to change notification settings - Fork 146
Use serializer to determine relationship IDs when possible #155
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -98,6 +98,18 @@ def static_record_type | |
| @static_record_type | ||
| end | ||
|
|
||
| def has_many? | ||
| relationship_type == :has_many | ||
| end | ||
|
|
||
| def belongs_to? | ||
| relationship_type == :belongs_to | ||
| end | ||
|
|
||
| def has_one? | ||
| relationship_type == :has_one | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def ids_hash_from_record_and_relationship(record, params = {}) | ||
|
|
@@ -107,18 +119,28 @@ def ids_hash_from_record_and_relationship(record, params = {}) | |
|
|
||
| return unless associated_object = fetch_associated_object(record, params) | ||
|
|
||
| if associated_object.respond_to? :map | ||
| return associated_object.map do |object| | ||
| id_hash_from_record object, params | ||
| end | ||
| if has_many? | ||
| associated_object.map { |object| id_hash_from_record(object, params) } | ||
| else | ||
| id_hash_from_record associated_object, params | ||
| end | ||
|
|
||
| id_hash_from_record associated_object, params | ||
| end | ||
|
|
||
| def id_hash_from_record(record, params) | ||
| associated_record_type = record_type_for(record, params) | ||
| id_hash(record.public_send(id_method_name), associated_record_type) | ||
|
|
||
| if id_method_name && record.respond_to?(id_method_name) | ||
| # | ||
| # TODO: The serializer for a relationship should always be the single source of truth for determining the ID of | ||
| # a related record. Currently it is possible to use the `id_method_name` of a relationship for this | ||
| # purpose, so to maintain backwards compatibility this code path is maintained. | ||
| # Eventually this should be removed. | ||
| # | ||
| return id_hash(record.public_send(id_method_name), associated_record_type) | ||
| end | ||
|
|
||
| serializer = serializer_for(record, params) | ||
| id_hash(serializer.id_from_record(record, params), associated_record_type) | ||
| end | ||
|
|
||
| def ids_hash(ids, record_type) | ||
|
|
@@ -136,13 +158,39 @@ def id_hash(id, record_type, default_return = false) | |
| end | ||
|
|
||
| def fetch_id(record, params) | ||
| if object_block.present? | ||
| object = FastJsonapi.call_proc(object_block, record, params) | ||
| return object.map { |item| item.public_send(id_method_name) } if object.respond_to? :map | ||
| if object_block.nil? | ||
| # | ||
| # TODO: This line assumes the `id_method_name` is a foreign key defined on the parent record. | ||
| # The `id_method_name` option should be replaced with `foreign_key` to make the intent | ||
| # explicit. | ||
| # | ||
| foreign_key = id_method_name | ||
| foreign_key ||= (has_many? ? "#{@name.to_s.singularize}_ids" : "#{@name}_id") | ||
|
|
||
| return record.public_send(foreign_key) if record.respond_to?(foreign_key) | ||
| end | ||
|
|
||
| return unless object = fetch_associated_object(record, params) | ||
|
|
||
| # | ||
| # TODO: The serializer for a relationship should always be the single source of truth for determining the ID of | ||
| # a related record. This code path assumes that the `id_method_name` should be called on the child record. | ||
| # This code path should be removed when `id_method_name` is replaced with `foreign_key` | ||
| # | ||
| if id_method_name | ||
| if has_many? && object.first.respond_to?(id_method_name) | ||
|
Contributor
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. Thanks for taking a stab at this, I think it's really close! I'm a bit concerned about calling Also, what happens if the developer intends for I think we can solve for both this way: if has_many?
if object.length > 0 && object.first.respond_to?(id_method_name)
return object.map { |item| item.public_send(id_method_name) }
else
return []
end
...
endAlso, what should happen if |
||
| return object.map { |item| item.public_send(id_method_name) } | ||
| elsif object.respond_to?(id_method_name) | ||
| return object.public_send(id_method_name) | ||
| end | ||
| end | ||
|
|
||
| return object.try(id_method_name) | ||
| serializer = serializer_for(object, params) | ||
| if has_many? | ||
| object.map { |item| serializer.id_from_record(item, params) } | ||
| elsif object | ||
| serializer.id_from_record(object, params) | ||
| end | ||
| record.public_send(id_method_name) | ||
| end | ||
|
|
||
| def add_links_hash(record, params, output_hash) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
For performance, we should memoize this computation, as we may be iterating over hundreds of records or more, and it is constant once the relationship is defined. Either on init or as a memoized method.