Skip to content

Commit 77105a7

Browse files
fix: has_many_getter can now work with the new optimization
1 parent 3ef5f1a commit 77105a7

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

app/serializers/forest_liana/serializer_factory.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ def relationship_related_link(attribute_name)
118118

119119
if ret[:href].blank?
120120
begin
121-
# if @options[:include].try(:include?, attribute_name.to_s) &&
122-
# !SchemaHelper.is_smart_field?(object.class, attribute_name.to_s)
123-
# object.send(attribute_name)
124-
# end
125-
126121
SchemaUtils.many_associations(object.class).each do |a|
127122
if a.name == attribute_name
128123
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/relationships/#{attribute_name}"
@@ -140,14 +135,19 @@ def has_one_relationships
140135
return {} if self.class.to_one_associations.nil?
141136
data = {}
142137
self.class.to_one_associations.each do |attribute_name, attr_data|
143-
next if !should_include_attr?(attribute_name, attr_data)
144138
relation = object.class.reflect_on_all_associations.find { |a| a.name == attribute_name }
145-
relation_class_name = ForestLiana.name_for(relation.klass).demodulize
146-
if object.send(relation.foreign_key.to_sym) &&
147-
@options[:fields][relation_class_name].size == 1 &&
148-
@options[:fields][relation_class_name].include?(relation.klass.primary_key.to_sym)
149139

150-
attr_data[:attr_or_block] = proc { relation.klass.new(relation.klass.primary_key => object.send(relation.foreign_key.to_sym)) }
140+
next if !should_include_attr?(attribute_name, attr_data)
141+
142+
unless relation.polymorphic?
143+
relation_class_name = ForestLiana.name_for(relation.klass).demodulize
144+
145+
if object.send(relation.foreign_key.to_sym) &&
146+
@options[:fields][relation_class_name]&.size == 1 &&
147+
@options[:fields][relation_class_name]&.include?(relation.klass.primary_key.to_sym)
148+
149+
attr_data[:attr_or_block] = proc { relation.klass.new(relation.klass.primary_key => object.send(relation.foreign_key.to_sym)) }
150+
end
151151
end
152152

153153
data[attribute_name] = attr_data

app/services/forest_liana/has_many_getter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def records
3737
private
3838

3939
def compute_includes
40+
@optional_includes = []
41+
4042
@includes = @association.klass
4143
.reflect_on_all_associations
4244
.select do |association|

0 commit comments

Comments
 (0)