Skip to content

Commit dc96641

Browse files
authored
fix: serializer on relationships (#745)
1 parent be26b9c commit dc96641

File tree

2 files changed

+0
-48
lines changed

2 files changed

+0
-48
lines changed

app/serializers/forest_liana/serializer_factory.rb

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -131,33 +131,6 @@ def relationship_related_link(attribute_name)
131131
ret
132132
end
133133

134-
def has_one_relationships
135-
return {} if self.class.to_one_associations.nil?
136-
data = {}
137-
self.class.to_one_associations.each do |attribute_name, attr_data|
138-
relation = object.class.reflect_on_all_associations.find { |a| a.name == attribute_name }
139-
140-
next if !should_include_attr?(attribute_name, attr_data)
141-
142-
unless relation.nil? || (relation.respond_to?(:polymorphic?) && relation.polymorphic?)
143-
relation_class_name = ForestLiana.name_for(relation.klass).demodulize
144-
145-
if object.respond_to?(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 {
150-
foreign_key_value = object.send(relation.foreign_key.to_sym)
151-
foreign_key_value.nil? ? nil : relation.klass.new(relation.klass.primary_key => foreign_key_value)
152-
}
153-
end
154-
end
155-
156-
data[attribute_name] = attr_data
157-
end
158-
data
159-
end
160-
161134
private
162135

163136
def intercom_integration?

spec/services/forest_liana/serializer_factory_spec.rb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,6 @@ module ForestLiana
55
let(:island) { Island.create!(name: 'TestIsland') }
66
let(:tree) { Tree.create!(name: 'TestTree', island: island, owner: user) }
77

8-
it 'simulates has_one relation with only primary key' do
9-
factory = described_class.new
10-
serializer_class = factory.serializer_for(Tree)
11-
12-
serializer_class.send(:has_one, :island) { }
13-
14-
instance = serializer_class.new(tree, fields: {
15-
'Island' => [:id],
16-
'Tree' => [:island]
17-
})
18-
19-
relationships = instance.send(:has_one_relationships)
20-
expect(relationships).to have_key(:island)
21-
relation_data = relationships[:island]
22-
expect(relation_data[:attr_or_block]).to be_a(Proc)
23-
model = relation_data[:attr_or_block].call
24-
25-
expect(model).to be_a(Island)
26-
expect(model.id).to eq(island.id)
27-
end
28-
298
it 'returns nil if foreign key is nil' do
309
tree_without_island = Tree.create!(name: 'NoIslandTree', island_id: nil, owner: user)
3110

0 commit comments

Comments
 (0)