forest_liana 9.14.2 → 9.14.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/serializers/forest_liana/serializer_factory.rb +4 -1
- data/app/services/forest_liana/has_many_getter.rb +2 -3
- data/app/services/forest_liana/resources_getter.rb +7 -8
- data/lib/forest_liana/version.rb +1 -1
- data/spec/dummy/app/models/tree.rb +1 -1
- data/spec/services/forest_liana/resources_getter_spec.rb +20 -0
- data/spec/services/forest_liana/serializer_factory_spec.rb +53 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc578d6e8079646a883e1fa6685a272b47ea49c956fb90cc2bcb3e946d04de9a
|
4
|
+
data.tar.gz: 938d02f609606ef9ed4e5efed2038ed1f93715339cb874b90c3effbbb308dc37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0410aade693b10507faf1977d767aeafe0d9cac0cf9ba08ff948815d219a3885bc7c4a4b5da7442f4347466862f2c9750d0c97b2e48bae751dca90bedd5093ce
|
7
|
+
data.tar.gz: 8d693fe4ef1ace2bb391eaf2d4c062d27c0d59d74725328f2cf28eee2a17759610c6c01a091ed8e6abf52a187700ebdfae64a1fd13713f35167c8ea2877354da
|
@@ -146,7 +146,10 @@ module ForestLiana
|
|
146
146
|
@options[:fields][relation_class_name]&.size == 1 &&
|
147
147
|
@options[:fields][relation_class_name]&.include?(relation.klass.primary_key.to_sym)
|
148
148
|
|
149
|
-
attr_data[:attr_or_block] = proc {
|
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
|
+
}
|
150
153
|
end
|
151
154
|
end
|
152
155
|
|
@@ -62,9 +62,8 @@ module ForestLiana
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def field_names_requested
|
65
|
-
|
66
|
-
|
67
|
-
.map { |name| name.to_sym }
|
65
|
+
fields = @params.dig(:fields, @collection_name)
|
66
|
+
Array(fields&.split(',')).map(&:to_sym)
|
68
67
|
end
|
69
68
|
|
70
69
|
def model_association
|
@@ -32,7 +32,7 @@ module ForestLiana
|
|
32
32
|
def perform
|
33
33
|
polymorphic_association, preload_loads = analyze_associations(@resource)
|
34
34
|
includes = @includes.uniq - polymorphic_association - preload_loads - @optional_includes
|
35
|
-
has_smart_fields =
|
35
|
+
has_smart_fields = Array(@params.dig(:fields, @collection_name)&.split(',')).any? do |field|
|
36
36
|
ForestLiana::SchemaHelper.is_smart_field?(@resource, field)
|
37
37
|
end
|
38
38
|
|
@@ -143,10 +143,8 @@ module ForestLiana
|
|
143
143
|
includes = associations_has_one.map do |association|
|
144
144
|
association_name = association.name.to_s
|
145
145
|
|
146
|
-
|
147
|
-
|
148
|
-
@params[:fields][association_name].split(',').include?(association.klass.primary_key)
|
149
|
-
|
146
|
+
fields = @params[:fields]&.[](association_name)&.split(',')
|
147
|
+
if fields&.size == 1 && fields.include?(association.klass.primary_key)
|
150
148
|
@field_names_requested << association.foreign_key
|
151
149
|
@optional_includes << association.name
|
152
150
|
end
|
@@ -191,7 +189,7 @@ module ForestLiana
|
|
191
189
|
end
|
192
190
|
|
193
191
|
def field_names_requested
|
194
|
-
return
|
192
|
+
return [] unless @params.dig(:fields, @collection_name)
|
195
193
|
|
196
194
|
associations_for_query = extract_associations_from_filter
|
197
195
|
associations_for_query << @params[:sort].split('.').first.to_sym if @params[:sort]&.include?('.')
|
@@ -332,11 +330,12 @@ module ForestLiana
|
|
332
330
|
select << "#{@resource.table_name}.#{association.foreign_key}"
|
333
331
|
end
|
334
332
|
|
335
|
-
|
333
|
+
fields = @params[:fields]&.[](path)&.split(',')
|
334
|
+
if fields
|
336
335
|
association = get_one_association(path)
|
337
336
|
table_name = association.table_name
|
338
337
|
|
339
|
-
|
338
|
+
fields.each do |association_path|
|
340
339
|
if ForestLiana::SchemaHelper.is_smart_field?(association.klass, association_path)
|
341
340
|
association.klass.attribute_names.each { |attribute| select << "#{table_name}.#{attribute}" }
|
342
341
|
else
|
data/lib/forest_liana/version.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Tree < ActiveRecord::Base
|
2
2
|
belongs_to :owner, class_name: 'User', inverse_of: :trees_owned
|
3
3
|
belongs_to :cutter, class_name: 'User', inverse_of: :trees_cut
|
4
|
-
belongs_to :island
|
4
|
+
belongs_to :island, optional: true
|
5
5
|
belongs_to :eponymous_island,
|
6
6
|
->(record) { where(name: record.name) },
|
7
7
|
class_name: 'Island',
|
@@ -572,6 +572,26 @@ module ForestLiana
|
|
572
572
|
expect(records.second.name).to eq 'Treasure'
|
573
573
|
end
|
574
574
|
end
|
575
|
+
|
576
|
+
describe '#perform' do
|
577
|
+
let(:resource) { User }
|
578
|
+
let(:fields) { nil }
|
579
|
+
let(:sort) { nil }
|
580
|
+
let(:filters) { nil }
|
581
|
+
|
582
|
+
it 'does not raise an error when params[:fields] is nil' do
|
583
|
+
expect {
|
584
|
+
getter.perform
|
585
|
+
}.not_to raise_error
|
586
|
+
|
587
|
+
records = getter.records
|
588
|
+
count = getter.count
|
589
|
+
|
590
|
+
expect(records).to all(be_a(User))
|
591
|
+
expect(count).to eq User.count
|
592
|
+
end
|
593
|
+
end
|
594
|
+
|
575
595
|
end
|
576
596
|
end
|
577
597
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module ForestLiana
|
2
|
+
describe SerializerFactory do
|
3
|
+
describe '#serializer_for has_one_relationships patch' do
|
4
|
+
let(:user) { User.create!(name: 'PatchTest') }
|
5
|
+
let(:island) { Island.create!(name: 'TestIsland') }
|
6
|
+
let(:tree) { Tree.create!(name: 'TestTree', island: island, owner: user) }
|
7
|
+
|
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
|
+
|
29
|
+
it 'returns nil if foreign key is nil' do
|
30
|
+
tree_without_island = Tree.create!(name: 'NoIslandTree', island_id: nil, owner: user)
|
31
|
+
|
32
|
+
factory = described_class.new
|
33
|
+
serializer_class = factory.serializer_for(Tree)
|
34
|
+
|
35
|
+
serializer_class.send(:has_one, :island) { }
|
36
|
+
|
37
|
+
instance = serializer_class.new(tree_without_island, fields: {
|
38
|
+
'Island' => [:id],
|
39
|
+
'Tree' => [:island]
|
40
|
+
})
|
41
|
+
|
42
|
+
relationships = instance.send(:has_one_relationships)
|
43
|
+
expect(relationships).to have_key(:island)
|
44
|
+
relation_data = relationships[:island]
|
45
|
+
expect(relation_data[:attr_or_block]).to be_a(Proc)
|
46
|
+
model = relation_data[:attr_or_block].call
|
47
|
+
|
48
|
+
expect(model).to be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: forest_liana
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 9.14.
|
4
|
+
version: 9.14.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sandro Munda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-06-
|
11
|
+
date: 2025-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -431,6 +431,7 @@ files:
|
|
431
431
|
- spec/services/forest_liana/resources_getter_spec.rb
|
432
432
|
- spec/services/forest_liana/schema_adapter_spec.rb
|
433
433
|
- spec/services/forest_liana/scope_manager_spec.rb
|
434
|
+
- spec/services/forest_liana/serializer_factory_spec.rb
|
434
435
|
- spec/services/forest_liana/smart_action_field_validator_spec.rb
|
435
436
|
- spec/services/forest_liana/smart_action_form_parser_spec.rb
|
436
437
|
- spec/services/forest_liana/utils/context_variables_injector_spec.rb
|
@@ -736,6 +737,7 @@ test_files:
|
|
736
737
|
- spec/services/forest_liana/resources_getter_spec.rb
|
737
738
|
- spec/services/forest_liana/schema_adapter_spec.rb
|
738
739
|
- spec/services/forest_liana/scope_manager_spec.rb
|
740
|
+
- spec/services/forest_liana/serializer_factory_spec.rb
|
739
741
|
- spec/services/forest_liana/smart_action_field_validator_spec.rb
|
740
742
|
- spec/services/forest_liana/smart_action_form_parser_spec.rb
|
741
743
|
- spec/services/forest_liana/utils/context_variables_injector_spec.rb
|