-
Notifications
You must be signed in to change notification settings - Fork 532
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
Add STI relationship option #1359
Open
lgebhardt
wants to merge
2
commits into
release-0-10
Choose a base branch
from
sti_v0-10
base: release-0-10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−38
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -92,6 +92,9 @@ def find_to_populate_by_keys(keys, options = {}) | |||||
def find_fragments(filters, options = {}) | ||||||
include_directives = options[:include_directives] ? options[:include_directives].include_directives : {} | ||||||
resource_klass = self | ||||||
|
||||||
fragments = {} | ||||||
|
||||||
linkage_relationships = to_one_relationships_for_linkage(include_directives[:include_related]) | ||||||
|
||||||
sort_criteria = options.fetch(:sort_criteria) { [] } | ||||||
|
@@ -129,19 +132,33 @@ def find_fragments(filters, options = {}) | |||||
if linkage_relationship.polymorphic? && linkage_relationship.belongs_to? | ||||||
linkage_relationship.resource_types.each do |resource_type| | ||||||
klass = resource_klass_for(resource_type) | ||||||
linkage_fields << {relationship_name: name, resource_klass: klass} | ||||||
|
||||||
linkage_table_alias = join_manager.join_details_by_polymorphic_relationship(linkage_relationship, resource_type)[:alias] | ||||||
primary_key = klass._primary_key | ||||||
|
||||||
linkage_fields << {relationship_name: name, | ||||||
linkage_relationship: linkage_relationship, | ||||||
resource_klass: klass, | ||||||
field: "#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}", | ||||||
alias: "#{linkage_table_alias}_#{primary_key}"} | ||||||
|
||||||
pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}") | ||||||
end | ||||||
else | ||||||
klass = linkage_relationship.resource_klass | ||||||
linkage_fields << {relationship_name: name, resource_klass: klass} | ||||||
|
||||||
linkage_table_alias = join_manager.join_details_by_relationship(linkage_relationship)[:alias] | ||||||
primary_key = klass._primary_key | ||||||
pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}") | ||||||
|
||||||
linkage_fields << {relationship_name: name, | ||||||
linkage_relationship: linkage_relationship, | ||||||
resource_klass: klass, | ||||||
field: "#{concat_table_field(linkage_table_alias, primary_key)} AS \"#{linkage_table_alias}_#{primary_key}\"", | ||||||
alias: "#{linkage_table_alias}_#{primary_key}"} | ||||||
|
||||||
pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS \"#{linkage_table_alias}_#{primary_key}\"") | ||||||
|
||||||
if linkage_relationship.sti? | ||||||
pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, 'type')} AS \"#{linkage_table_alias}_type\"") | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
|
@@ -158,7 +175,6 @@ def find_fragments(filters, options = {}) | |||||
pluck_fields << Arel.sql(field) | ||||||
end | ||||||
|
||||||
fragments = {} | ||||||
rows = records.pluck(*pluck_fields) | ||||||
rows.each do |row| | ||||||
rid = JSONAPI::ResourceIdentity.new(resource_klass, pluck_fields.length == 1 ? row : row[0]) | ||||||
|
@@ -175,7 +191,14 @@ def find_fragments(filters, options = {}) | |||||
fragments[rid].initialize_related(linkage_field_details[:relationship_name]) | ||||||
related_id = row[attributes_offset] | ||||||
if related_id | ||||||
related_rid = JSONAPI::ResourceIdentity.new(linkage_field_details[:resource_klass], related_id) | ||||||
if linkage_field_details[:linkage_relationship].sti? | ||||||
type = row[2] | ||||||
related_rid = JSONAPI::ResourceIdentity.new(resource_klass_for(type), related_id) | ||||||
attributes_offset+= 1 | ||||||
else | ||||||
related_rid = JSONAPI::ResourceIdentity.new(linkage_field_details[:resource_klass], related_id) | ||||||
end | ||||||
|
||||||
fragments[rid].add_related_identity(linkage_field_details[:relationship_name], related_rid) | ||||||
end | ||||||
attributes_offset+= 1 | ||||||
|
@@ -413,6 +436,10 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne | |||||
Arel.sql("#{concat_table_field(resource_table_alias, resource_klass._primary_key)} AS #{resource_table_alias}_#{resource_klass._primary_key}") | ||||||
] | ||||||
|
||||||
if relationship.sti? | ||||||
pluck_fields << Arel.sql("#{concat_table_field(resource_table_alias, 'type')} AS #{resource_table_alias}_type") | ||||||
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.
Suggested change
right? |
||||||
end | ||||||
|
||||||
cache_field = resource_klass.attribute_to_model_field(:_cache_field) if options[:cache] | ||||||
if cache_field | ||||||
pluck_fields << Arel.sql("#{concat_table_field(resource_table_alias, cache_field[:name])} AS #{resource_table_alias}_#{cache_field[:name]}") | ||||||
|
@@ -458,12 +485,17 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne | |||||
fragments = {} | ||||||
rows = records.distinct.pluck(*pluck_fields) | ||||||
rows.each do |row| | ||||||
rid = JSONAPI::ResourceIdentity.new(resource_klass, row[1]) | ||||||
if relationship.sti? | ||||||
type = row[2] | ||||||
rid = JSONAPI::ResourceIdentity.new(resource_klass_for(type), row[1]) | ||||||
attributes_offset = 3 | ||||||
else | ||||||
rid = JSONAPI::ResourceIdentity.new(resource_klass, row[1]) | ||||||
attributes_offset = 2 | ||||||
end | ||||||
|
||||||
fragments[rid] ||= JSONAPI::ResourceFragment.new(rid) | ||||||
|
||||||
attributes_offset = 2 | ||||||
|
||||||
if cache_field | ||||||
fragments[rid].cache = cast_to_attribute_type(row[attributes_offset], cache_field[:type]) | ||||||
attributes_offset+= 1 | ||||||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
right?
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.
I think at this point klass is the resource, not the ActiveRecord::Base class, so you'd also need to define an
inheritance_column
method onJSONAPI::BasicResource
.Although in practice you'd probably want to use
_inheritance_column
instead, to be consistent with_primary_key
.