From 4a0fde66399b1ebde07daf8bda2e044f33aa45c3 Mon Sep 17 00:00:00 2001 From: Nicolas Alexandre Date: Tue, 6 May 2025 11:00:12 +0200 Subject: [PATCH 1/3] feat: preload polymorphic associations --- app/services/forest_liana/resources_getter.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/services/forest_liana/resources_getter.rb b/app/services/forest_liana/resources_getter.rb index 8ad42547..64e90048 100644 --- a/app/services/forest_liana/resources_getter.rb +++ b/app/services/forest_liana/resources_getter.rb @@ -55,11 +55,23 @@ def query_for_batch end def records - records = @records.offset(offset).limit(limit).to_a - + records = @records.offset(offset).limit(limit).to_a polymorphic_association, preload_loads = analyze_associations(@resource) + if polymorphic_association && Rails::VERSION::MAJOR >= 7 - # TODO + preloader = ActiveRecord::Associations::Preloader.new(records: @records, associations: polymorphic_association) + preloader.loaders + preloader.branches.each do |branch| + branch.loaders.each do |loader| + records_by_owner = loader.records_by_owner + records_by_owner.each do |record, association| + record_index = @records.find_index { |r| r.id == record.id } + @records[record_index].define_singleton_method(branch.association) do + association.first + end + end + end + end end preload_cross_database_associations(records, preload_loads) From c22a1f8bc96e58beb29b7c2b67d44d6134b25272 Mon Sep 17 00:00:00 2001 From: Nicolas Alexandre Date: Tue, 6 May 2025 16:26:26 +0200 Subject: [PATCH 2/3] fix: apply offset and limit before preload polymorphic associations --- app/services/forest_liana/resources_getter.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/forest_liana/resources_getter.rb b/app/services/forest_liana/resources_getter.rb index 64e90048..977ce22b 100644 --- a/app/services/forest_liana/resources_getter.rb +++ b/app/services/forest_liana/resources_getter.rb @@ -59,14 +59,14 @@ def records polymorphic_association, preload_loads = analyze_associations(@resource) if polymorphic_association && Rails::VERSION::MAJOR >= 7 - preloader = ActiveRecord::Associations::Preloader.new(records: @records, associations: polymorphic_association) + preloader = ActiveRecord::Associations::Preloader.new(records: records, associations: polymorphic_association) preloader.loaders preloader.branches.each do |branch| branch.loaders.each do |loader| records_by_owner = loader.records_by_owner records_by_owner.each do |record, association| - record_index = @records.find_index { |r| r.id == record.id } - @records[record_index].define_singleton_method(branch.association) do + record_index = records.find_index { |r| r.id == record.id } + records[record_index].define_singleton_method(branch.association) do association.first end end From 8f8d6216a3486a416a25b3d948078601ec6084cd Mon Sep 17 00:00:00 2001 From: Nicolas Alexandre Date: Wed, 28 May 2025 11:15:07 +0200 Subject: [PATCH 3/3] fix: serializer factory --- app/serializers/forest_liana/serializer_factory.rb | 4 ++-- app/services/forest_liana/resources_getter.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/serializers/forest_liana/serializer_factory.rb b/app/serializers/forest_liana/serializer_factory.rb index d795da96..b3f9105e 100644 --- a/app/serializers/forest_liana/serializer_factory.rb +++ b/app/serializers/forest_liana/serializer_factory.rb @@ -139,10 +139,10 @@ def has_one_relationships next if !should_include_attr?(attribute_name, attr_data) - unless relation.polymorphic? + unless relation.nil? || (relation.respond_to?(:polymorphic?) && relation.polymorphic?) relation_class_name = ForestLiana.name_for(relation.klass).demodulize - if object.send(relation.foreign_key.to_sym) && + if object.respond_to?(relation.foreign_key.to_sym) && @options[:fields][relation_class_name]&.size == 1 && @options[:fields][relation_class_name]&.include?(relation.klass.primary_key.to_sym) diff --git a/app/services/forest_liana/resources_getter.rb b/app/services/forest_liana/resources_getter.rb index 977ce22b..63e0348f 100644 --- a/app/services/forest_liana/resources_getter.rb +++ b/app/services/forest_liana/resources_getter.rb @@ -55,7 +55,7 @@ def query_for_batch end def records - records = @records.offset(offset).limit(limit).to_a + records = @records.offset(offset).limit(limit).to_a polymorphic_association, preload_loads = analyze_associations(@resource) if polymorphic_association && Rails::VERSION::MAJOR >= 7