-
Notifications
You must be signed in to change notification settings - Fork 1
feat(datasource-active-record): expose has_one :through as a to-one relation #327
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -100,21 +100,32 @@ def fetch_associations | |
| source_polymorphic = association.source_reflection&.polymorphic? && | ||
| association.options[:source_type].present? | ||
|
|
||
| add_field( | ||
| association.name.to_s, | ||
| ForestAdminDatasourceToolkit::Schema::Relations::ManyToManySchema.new( | ||
| foreign_collection: format_model_name(association.klass.name), | ||
| origin_key: through_reflection.foreign_key, | ||
| origin_key_target: through_reflection.join_foreign_key, | ||
| foreign_key: association.join_foreign_key, | ||
| foreign_key_target: association.association_primary_key, | ||
| through_collection: format_model_name(through_reflection.klass.name), | ||
| origin_type_field: is_polymorphic ? through_reflection.type : nil, | ||
| origin_type_value: is_polymorphic ? @model.name : nil, | ||
| foreign_type_field: source_polymorphic ? association.source_reflection.foreign_type : nil, | ||
| foreign_type_value: source_polymorphic ? association.options[:source_type] : nil | ||
| if is_polymorphic || source_polymorphic | ||
| add_field( | ||
| association.name.to_s, | ||
| ForestAdminDatasourceToolkit::Schema::Relations::ManyToManySchema.new( | ||
| foreign_collection: format_model_name(association.klass.name), | ||
| origin_key: through_reflection.foreign_key, | ||
| origin_key_target: through_reflection.join_foreign_key, | ||
| foreign_key: association.join_foreign_key, | ||
| foreign_key_target: association.association_primary_key, | ||
| through_collection: format_model_name(through_reflection.klass.name), | ||
| origin_type_field: is_polymorphic ? through_reflection.type : nil, | ||
| origin_type_value: is_polymorphic ? @model.name : nil, | ||
| foreign_type_field: source_polymorphic ? association.source_reflection.foreign_type : nil, | ||
| foreign_type_value: source_polymorphic ? association.options[:source_type] : nil | ||
| ) | ||
| ) | ||
| ) | ||
| else | ||
| add_field( | ||
| association.name.to_s, | ||
| ForestAdminDatasourceToolkit::Schema::Relations::OneToOneSchema.new( | ||
| foreign_collection: format_model_name(association.klass.name), | ||
| origin_key: association.klass.primary_key, | ||
| origin_key_target: @model.primary_key | ||
| ) | ||
| ) | ||
|
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. |
||
| end | ||
| elsif association.inverse_of&.polymorphic? | ||
| add_field( | ||
| association.name.to_s, | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -196,6 +196,10 @@ def build_select(collection, projection) | |
| if collection.model.table_name == @collection.model.table_name | ||
| if one_to_one_relations.include?(relation_schema.type) | ||
| @select << "#{collection.model.table_name}.#{relation_schema.origin_key_target}" | ||
| # foreign_key is an array for a composite-key belongs_to through hop | ||
| Array(root_through_foreign_key(collection, relation_name)).each do |through_fk| | ||
| @select << "#{collection.model.table_name}.#{through_fk}" | ||
| end | ||
| elsif many_to_one_relations.include?(relation_schema.type) | ||
| @select << "#{collection.model.table_name}.#{relation_schema.foreign_key}" | ||
| end | ||
|
|
@@ -275,7 +279,7 @@ def joinable_tables(collection, relation_name, sub_projection, used_tables) | |
| target = joinable_target(collection, relation_name, used_tables) | ||
| return nil if target.nil? | ||
|
|
||
| tables = Set[target.model.table_name] | ||
| tables = Set[target.model.table_name] | through_tables(collection, relation_name) | ||
| sub_projection.relations.each do |nested_name, nested_projection| | ||
| nested = joinable_tables(target, nested_name, nested_projection, used_tables | tables) | ||
| return nil if nested.nil? | ||
|
|
@@ -288,23 +292,50 @@ def joinable_tables(collection, relation_name, sub_projection, used_tables) | |
| # The target collection when this hop is safe to collapse into a JOIN, else nil (-> preload). | ||
| def joinable_target(collection, relation_name, used_tables) | ||
| relation_schema = collection.schema[:fields][relation_name] | ||
| # belongs_to only: it joins on the target's primary key, so it can't duplicate the parent | ||
| # (a has_one child may not be unique) | ||
| return unless relation_schema.type == 'ManyToOne' && relation_schema.respond_to?(:foreign_collection) | ||
| return unless relation_schema.respond_to?(:foreign_collection) | ||
|
|
||
| # a scoped association applies its scope to the JOIN and may inject raw/unqualified SQL or | ||
| # extra joins (e.g. `belongs_to :x, -> { where('id > ?', 1) }`) | ||
| reflection = collection.model.reflect_on_association(relation_name.to_sym) | ||
| return if reflection.nil? || reflection.scope | ||
|
|
||
| case relation_schema.type | ||
| when 'ManyToOne' then nil | ||
| when 'OneToOne' then return unless belongs_to_chain_through?(reflection) | ||
| else return | ||
| end | ||
|
|
||
| target = local_ar_collection(collection.datasource, relation_schema.foreign_collection) | ||
| return if target.nil? || !target.model.default_scopes.empty? # same risk as a scoped association | ||
| return unless same_database?(collection.model, target.model) | ||
| return if used_tables.include?(target.model.table_name) # a table joined twice would be aliased by AR | ||
| return if through_tables(collection, relation_name).intersect?(used_tables) | ||
|
|
||
| target | ||
|
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. |
||
| end | ||
|
|
||
| def through_tables(collection, relation_name) | ||
| through = collection.model.reflect_on_association(relation_name.to_sym)&.through_reflection | ||
| return Set[] unless through | ||
|
|
||
| Set[through.table_name] | ||
| rescue StandardError | ||
| Set[] | ||
| end | ||
|
|
||
| def belongs_to_chain_through?(reflection) | ||
| return false unless reflection.through_reflection? | ||
|
|
||
| through = reflection.through_reflection | ||
| source = reflection.source_reflection | ||
|
|
||
| through && source && through.belongs_to? && source.belongs_to? && | ||
|
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. |
||
| through.scope.nil? && source.scope.nil? && through.klass.default_scopes.empty? && | ||
| same_database?(reflection.active_record, through.klass) | ||
| rescue StandardError | ||
| false | ||
| end | ||
|
|
||
| def same_database?(model_a, model_b) | ||
| # compare the pools, not connection_specification_name (only an owner class name, shared across shards) | ||
| model_a.connection_pool == model_b.connection_pool | ||
|
|
@@ -330,6 +361,15 @@ def add_join_relation(relation_name) | |
| @query | ||
| end | ||
|
|
||
| def root_through_foreign_key(collection, relation_name) | ||
| through = collection.model.reflect_on_association(relation_name.to_sym)&.through_reflection | ||
| return unless through&.belongs_to? | ||
|
|
||
| through.foreign_key | ||
| rescue StandardError | ||
| nil | ||
| end | ||
|
|
||
| def resolve_field(original_field) | ||
| if original_field.include?(':') | ||
| relation_name, column_name = original_field.split(':') | ||
|
|
||
1 change: 1 addition & 0 deletions
1
packages/forest_admin_datasource_active_record/spec/dummy/app/models/account.rb
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| class Account < ApplicationRecord | ||
| belongs_to :supplier | ||
| belongs_to :account_history | ||
| has_one :order, through: :account_history | ||
| end |
1 change: 1 addition & 0 deletions
1
packages/forest_admin_datasource_active_record/spec/dummy/app/models/account_history.rb
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| class AccountHistory < ApplicationRecord | ||
| has_one :account | ||
| belongs_to :order, optional: true | ||
| end |
5 changes: 5 additions & 0 deletions
5
...urce_active_record/spec/dummy/db/migrate/20251126100004_add_order_to_account_histories.rb
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddOrderToAccountHistories < ActiveRecord::Migration[7.1] | ||
| def change | ||
| add_reference :account_histories, :order, null: true, foreign_key: true | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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.
Found 15 lines of similar code in 2 locations (mass = 94) [qlty:similar-code]