Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/services/forest_liana/base_getter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def analyze_associations(resource)
end

def separate_database?(resource, association)
return false if SchemaUtils.polymorphic?(association)

target_model_connection = association.klass.connection
target_model_database = target_model_connection.current_database if target_model_connection.respond_to? :current_database
resource_connection = resource.connection
Expand Down
29 changes: 13 additions & 16 deletions app/services/forest_liana/resources_getter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def records
records = @records.offset(offset).limit(limit).to_a
polymorphic_association, preload_loads = analyze_associations(@resource)

if polymorphic_association && Rails::VERSION::MAJOR >= 7
if polymorphic_association.any? && Rails::VERSION::MAJOR >= 7
preloader = ActiveRecord::Associations::Preloader.new(records: records, associations: polymorphic_association)
preloader.loaders
preloader.branches.each do |branch|
Expand All @@ -85,7 +85,6 @@ def preload_cross_database_associations(records, preload_loads)
next unless separate_database?(@resource, association)

columns = columns_for_cross_database_association(association_name)

if association.macro == :belongs_to
foreign_key = association.foreign_key
primary_key = association.klass.primary_key
Expand Down Expand Up @@ -125,27 +124,22 @@ def preload_cross_database_associations(records, preload_loads)
end

def columns_for_cross_database_association(association_name)
return [:id] unless @params[:fields].present?

fields = @params[:fields][association_name.to_s]
return [:id] unless fields

base_fields = fields.split(',').map(&:strip).map(&:to_sym) | [:id]

association = @resource.reflect_on_association(association_name)
extra_key = association.foreign_key

# Add the foreign key used for the association to ensure it's available in the preloaded records
# This is necessary for has_one associations, without it calling record.public_send(foreign_key) would raise a "missing attribute" error
base_fields << extra_key if association.macro == :has_one
# Always include all columns of the associated model to avoid missing attribute errors
columns = association.klass.column_names.map(&:to_sym)

# Ensure the foreign key is present for manual binding (especially for has_one)
columns << association.foreign_key.to_sym if association.macro == :has_one

base_fields.uniq
columns.uniq
end

def compute_includes
associations_has_one = ForestLiana::QueryHelper.get_one_associations(@resource)

@optional_includes = []
if @field_names_requested
if @field_names_requested && @params['searchExtended'].to_i != 1
includes = associations_has_one.map do |association|
association_name = association.name.to_s

Expand Down Expand Up @@ -175,7 +169,10 @@ def compute_includes

@includes = (includes & @field_names_requested).concat(includes_for_smart_search)
else
@includes = associations_has_one.map(&:name)
@includes = associations_has_one
# Avoid eager loading has_one associations pointing to a different database as ORM can't join cross databases
.reject { |association| separate_database?(@resource, association) }
.map(&:name)
end
end

Expand Down
18 changes: 16 additions & 2 deletions app/services/forest_liana/schema_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@ def add_associations
field[:field] = association.name
field[:inverse_of] = inverse_of(association)
field[:relationship] = get_relationship_type(association)
# NOTICE: Create the fields of hasOne, HasMany, … relationships.

ForestLiana::SchemaUtils.disable_filter_and_sort_if_cross_db!(
field,
association.name.to_s,
ForestLiana.name_for(@model)
)
# NOTICE: Create the fields of hasOne, HasMany, … relationships.
else
collection.fields << get_schema_for_association(association)
end
Expand Down Expand Up @@ -346,7 +352,7 @@ def get_schema_for_column(column)
end

def get_schema_for_association(association)
{
opts ={
field: association.name.to_s,
type: get_type_for_association(association),
relationship: get_relationship_type(association),
Expand All @@ -363,6 +369,14 @@ def get_schema_for_association(association)
widget: nil,
validations: []
}

ForestLiana::SchemaUtils.disable_filter_and_sort_if_cross_db!(
opts,
association.name.to_s,
ForestLiana.name_for(@model)
)

opts
end

def get_relationship_type(association)
Expand Down
22 changes: 22 additions & 0 deletions app/services/forest_liana/schema_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,27 @@ def self.habtm?(model)
def self.is_active_type? model
Object.const_defined?('ActiveType::Object') && model < ActiveType::Object
end

def self.disable_filter_and_sort_if_cross_db!(opts, name, collection_name)
return unless opts[:reference]

assoc_name = opts[:reference].split('.').first&.underscore&.to_sym || name
model = find_model_from_collection_name(collection_name)
return unless model

association = model.reflect_on_association(assoc_name)
return unless association
return if polymorphic?(association)

model_db = model.connection_db_config.database
assoc_db = association.klass.connection_db_config.database

if model_db != assoc_db
opts[:is_filterable] = false
opts[:is_sortable] = false
end
rescue => e
FOREST_LOGGER.warn("Could not evaluate cross-db association for #{name}: #{e.message}")
end
end
end
Loading