Class: ApiMaker::PreloaderHasOne
Instance Attribute Summary
#ability, #api_maker_args, #collection, #data, #locals, #records, #reflection, #reflection_name, #select, #select_columns
Instance Method Summary
collapse
#accessible_query, #collection_ids, #initial_join_query, #initialize, #join_query, #join_query_with_joined_name, #join_query_with_normal_name, #joined_name, #models_with_join, #unconditioned_read_access?, #underscore_name
Instance Method Details
#models ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/api_maker/preloader_has_one.rb', line 21
def models
@models ||= if reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
models_with_join
else
query = query_normal
query = query.instance_eval(&reflection.scope) if reflection.scope
query = query.accessible_by(ability) if ability
query = ApiMaker::SelectColumnsOnCollection.execute!(
collection: query,
model_class: reflection.klass,
select_attributes: select,
select_columns: select_columns,
table_name: query.klass.table_name
)
query = query.fix if ApiMaker::DatabaseType.postgres?
query.load
query
end
end
|
#origin_data_for_model(model) ⇒ Object
41
42
43
44
|
# File 'lib/api_maker/preloader_has_one.rb', line 41
def origin_data_for_model(model)
origin_id = model[:api_maker_origin_id]
data.dig!(:preloaded, underscore_name, origin_id)
end
|
#preload ⇒ Object
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/api_maker/preloader_has_one.rb', line 2
def preload
models.each do |model|
model_id = ApiMaker::PrimaryIdForModel.get(model)
ApiMaker::Configuration.profile(-> { "Preloading #{model.class.name}##{model_id}" }) do
origin_data = origin_data_for_model(model)
origin_data.fetch(:r)[reflection.name] = model_id
serializer = ApiMaker::Serializer.new(ability: ability, api_maker_args: api_maker_args, locals: locals, model: model, select: select&.dig(model.class))
underscore_name = serializer.resource.underscore_name
data.fetch(:preloaded)[underscore_name] ||= {}
data.fetch(:preloaded).fetch(underscore_name)[model_id] ||= serializer
end
end
models
end
|
#query_normal ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/api_maker/preloader_has_one.rb', line 46
def query_normal
query = reflection.klass.where(reflection.foreign_key => collection.map(&:id))
.select(reflection.klass.arel_table[reflection.foreign_key].as("api_maker_origin_id"))
query = query.where("#{reflection.options.fetch(:as)}_type" => reflection.active_record.name) if reflection.options[:as]
query
end
|