Class: ForestLiana::ResourcesGetter
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from BaseGetter
#get_collection, #get_resource
Constructor Details
#initialize(resource, params, forest_user) ⇒ ResourcesGetter
Returns a new instance of ResourcesGetter.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'app/services/forest_liana/resources_getter.rb', line 5
def initialize(resource, params, forest_user)
@resource = resource
@params = params
@user = forest_user
@count_needs_includes = false
@collection_name = ForestLiana.name_for(@resource)
@collection = get_collection(@collection_name)
@fields_to_serialize = get_fields_to_serialize
@field_names_requested = field_names_requested
@segment = get_segment
compute_includes
@search_query_builder = SearchQueryBuilder.new(@params, @includes, @collection, @user)
prepare_query
end
|
Instance Attribute Details
#includes ⇒ Object
Returns the value of attribute includes.
3
4
5
|
# File 'app/services/forest_liana/resources_getter.rb', line 3
def includes
@includes
end
|
#records_count ⇒ Object
Returns the value of attribute records_count.
3
4
5
|
# File 'app/services/forest_liana/resources_getter.rb', line 3
def records_count
@records_count
end
|
#search_query_builder ⇒ Object
Returns the value of attribute search_query_builder.
3
4
5
|
# File 'app/services/forest_liana/resources_getter.rb', line 3
def search_query_builder
@search_query_builder
end
|
Class Method Details
.get_ids_from_request(params, user) ⇒ Object
21
22
23
24
25
26
27
28
29
30
|
# File 'app/services/forest_liana/resources_getter.rb', line 21
def self.get_ids_from_request(params, user)
attributes = params.dig('data', 'attributes')
return attributes[:ids] if attributes&.fetch(:all_records, false) == false && attributes[:ids]
attributes = merge_subset_query(attributes)
resources_getter = initialize_resources_getter(attributes, user)
ids = fetch_ids(resources_getter)
filter_excluded_ids(ids, attributes[:all_records_ids_excluded])
end
|
Instance Method Details
#columns_for_cross_database_association(association_name) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'app/services/forest_liana/resources_getter.rb', line 115
def columns_for_cross_database_association(association_name)
return [:id] unless @params[:fields].present?
fields = @params[:fields][association_name.to_s]
return [:id] unless fields
base_fields = fields.split(',').map(&:strip).map(&:to_sym) | [:id]
association = @resource.reflect_on_association(association_name)
= association.foreign_key
base_fields << if association.macro == :has_one
base_fields.uniq
end
|
#compute_includes ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'app/services/forest_liana/resources_getter.rb', line 133
def compute_includes
associations_has_one = ForestLiana::QueryHelper.get_one_associations(@resource)
includes = associations_has_one.map(&:name)
includes_for_smart_search = []
if @collection && @collection.search_fields
includes_for_smart_search = @collection.search_fields
.select { |field| field.include? '.' }
.map { |field| field.split('.').first.to_sym }
includes_has_many = SchemaUtils.many_associations(@resource)
.select { |association| SchemaUtils.model_included?(association.klass) }
.map(&:name)
includes_for_smart_search = includes_for_smart_search & includes_has_many
end
if @field_names_requested
@includes = (includes & @field_names_requested).concat(includes_for_smart_search)
else
@includes = includes
end
end
|
#count ⇒ Object
49
50
51
|
# File 'app/services/forest_liana/resources_getter.rb', line 49
def count
@records_count = @count_needs_includes ? optimized_count : @records.count
end
|
#includes_for_serialization ⇒ Object
158
159
160
|
# File 'app/services/forest_liana/resources_getter.rb', line 158
def includes_for_serialization
super & @fields_to_serialize.map(&:to_s)
end
|
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'app/services/forest_liana/resources_getter.rb', line 32
def perform
polymorphic_association, preload_loads = analyze_associations(@resource)
includes = @includes.uniq - polymorphic_association - preload_loads
has_smart_fields = @params[:fields][@collection_name].split(',').any? do |field|
ForestLiana::SchemaHelper.is_smart_field?(@resource, field)
end
if includes.empty? || has_smart_fields
@records = optimize_record_loading(@resource, @records, false)
else
select = compute_select_fields
@records = optimize_record_loading(@resource, @records, false).references(includes).select(*select)
end
@records
end
|
#preload_cross_database_associations(records, preload_loads) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'app/services/forest_liana/resources_getter.rb', line 70
def preload_cross_database_associations(records, preload_loads)
preload_loads.each do |association_name|
association = @resource.reflect_on_association(association_name)
next unless separate_database?(@resource, association)
columns = columns_for_cross_database_association(association_name)
if association.macro == :belongs_to
foreign_key = association.foreign_key
primary_key = association.klass.primary_key
ids = records.map { |r| r.public_send(foreign_key) }.compact.uniq
next if ids.empty?
associated = association.klass.where(primary_key => ids)
.select(columns)
.index_by { |record| record.public_send(primary_key) }
records.each do |record|
record.define_singleton_method(association_name) do
associated[record.send(foreign_key.to_sym)] || nil
end
end
end
if association.macro == :has_one
foreign_key = association.foreign_key
primary_key = association.active_record_primary_key
ids = records.map { |r| r.public_send(primary_key) }.compact.uniq
next if ids.empty?
associated = association.klass.where(foreign_key => ids)
.select(columns)
.index_by { |record| record.public_send(foreign_key.to_sym) }
records.each do |record|
record.define_singleton_method(association_name) do
associated[record.send(primary_key.to_sym)] || nil
end
end
end
end
end
|
#query_for_batch ⇒ Object
53
54
55
|
# File 'app/services/forest_liana/resources_getter.rb', line 53
def query_for_batch
@records
end
|
#records ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'app/services/forest_liana/resources_getter.rb', line 57
def records
records = @records.offset(offset).limit(limit).to_a
polymorphic_association, preload_loads = analyze_associations(@resource)
if polymorphic_association && Rails::VERSION::MAJOR >= 7
end
preload_cross_database_associations(records, preload_loads)
records
end
|