@@ -19,6 +19,51 @@ def initialize(resource, params)
19
19
prepare_query ( )
20
20
end
21
21
22
+ def self . get_ids_from_request ( params )
23
+ attributes = params . dig ( 'data' , 'attributes' )
24
+ has_body_attributes = attributes != nil
25
+ is_select_all_records_query = has_body_attributes && attributes [ :all_records ] == true
26
+
27
+ # NOTICE: If it is not a "select all records" query and it receives a list of ID, return list of ID.
28
+ return attributes [ :ids ] if ( !is_select_all_records_query && attributes [ :ids ] )
29
+
30
+ # NOTICE: If it is a "select all records" we have to perform query to build ID list.
31
+ ids = Array . new
32
+
33
+ # NOTICE: Merging all_records_subset_query into attributes preserves filters in HasManyGetter and ResourcesGetter.
34
+ attributes = attributes . merge ( attributes [ :all_records_subset_query ] . dup . to_unsafe_h )
35
+
36
+ # NOTICE: Initialize actual resources getter (could either a HasManyGetter or a ResourcesGetter).
37
+ is_related_data = attributes [ :parent_collection_id ] &&
38
+ attributes [ :parent_collection_name ] &&
39
+ attributes [ :parent_association_name ]
40
+ if is_related_data
41
+ parent_collection_name = attributes [ :parent_collection_name ]
42
+ parent_model = ForestLiana ::SchemaUtils . find_model_from_collection_name ( parent_collection_name )
43
+ model = parent_model . reflect_on_association ( attributes [ :parent_association_name ] . try ( :to_sym ) )
44
+ resources_getter = ForestLiana ::HasManyGetter . new ( parent_model , model , attributes . merge ( {
45
+ collection : parent_collection_name ,
46
+ id : attributes [ :parent_collection_id ] ,
47
+ association_name : attributes [ :parent_association_name ] ,
48
+ } ) )
49
+ else
50
+ collection_name = attributes [ :collection_name ]
51
+ model = ForestLiana ::SchemaUtils . find_model_from_collection_name ( collection_name )
52
+ resources_getter = ForestLiana ::ResourcesGetter . new ( model , attributes )
53
+ end
54
+
55
+ # NOTICE: build IDs list.
56
+ resources_getter . query_for_batch . find_in_batches ( ) do |records |
57
+ ids += records . map { |record | record . id }
58
+ end
59
+
60
+ # NOTICE: remove excluded IDs.
61
+ ids_excluded = ( attributes [ :all_records_ids_excluded ] ) . map { |id_excluded | id_excluded . to_s }
62
+ return ids . select { |id | !ids_excluded . include? id . to_s } if ( ids_excluded && ids_excluded . any? )
63
+
64
+ return ids
65
+ end
66
+
22
67
def perform
23
68
@records = @records . eager_load ( @includes )
24
69
end
0 commit comments