Class: ApiMaker::Preloader

Inherits:
Object
  • Object
show all
Defined in:
lib/api_maker/preloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ability: nil, api_maker_args: nil, collection:, data:, key_path: [], locals:, preload_param:, model_class: nil, records:, select:, select_columns:) ⇒ Preloader

Returns a new instance of Preloader.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/api_maker/preloader.rb', line 4

def initialize(
  ability: nil,
  api_maker_args: nil,
  collection:,
  data:,
  key_path: [],
  locals:,
  preload_param:,
  model_class: nil,
  records:,
  select:,
  select_columns:
)
  @ability = ability
  @api_maker_args = api_maker_args
  @collection = collection
  @data = data
  @key_path = key_path
  @locals = locals
  @preload_param = preload_param
  @model_class = model_class || @collection.model
  @records = records
  @select = select
  @select_columns = select_columns
end

Instance Attribute Details

#api_maker_argsObject (readonly)

Returns the value of attribute api_maker_args.



2
3
4
# File 'lib/api_maker/preloader.rb', line 2

def api_maker_args
  @api_maker_args
end

#key_pathObject (readonly)

Returns the value of attribute key_path.



2
3
4
# File 'lib/api_maker/preloader.rb', line 2

def key_path
  @key_path
end

#localsObject (readonly)

Returns the value of attribute locals.



2
3
4
# File 'lib/api_maker/preloader.rb', line 2

def locals
  @locals
end

#model_classObject (readonly)

Returns the value of attribute model_class.



2
3
4
# File 'lib/api_maker/preloader.rb', line 2

def model_class
  @model_class
end

#preload_paramObject (readonly)

Returns the value of attribute preload_param.



2
3
4
# File 'lib/api_maker/preloader.rb', line 2

def preload_param
  @preload_param
end

Instance Method Details

#fill_dataObject

rubocop:disable Metrics/AbcSize



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/api_maker/preloader.rb', line 30

def fill_data # rubocop:disable Metrics/AbcSize
  parsed = ApiMaker::RelationshipPreloader.parse(preload_param)
  return unless parsed

  parsed.each do |key, value|
    next unless key

    key_path << key

    reflection = model_class.reflections[key]
    raise "Unknown reflection: #{@collection.model.name}##{key}" unless reflection

    fill_empty_relationships_for_key(reflection, key)
    preload_class = preload_class_for_key(reflection)

    Rails.logger.debug { "API maker: Preloading #{model_class}: #{key_path.join(".")}" }

    preload_result = ApiMaker::Configuration.profile(-> { "Preloading #{reflection.klass.name} with #{preload_class.name}" }) do
      preload_class
        .new(
          ability: @ability,
          api_maker_args: api_maker_args,
          collection: @collection,
          data: @data,
          locals: locals,
          records: @records,
          reflection: reflection,
          select: @select,
          select_columns: @select_columns
        )
        .preload
    end

    if value.blank? || preload_result.empty?
      key_path.pop
      next
    end

    ApiMaker::Preloader
      .new(
        ability: @ability,
        api_maker_args: api_maker_args,
        data: @data,
        collection: preload_result,
        key_path: key_path.dup,
        locals: locals,
        preload_param: value,
        model_class: reflection.klass,
        records: @data.fetch(:preloaded),
        select: @select,
        select_columns: @select_columns
      )
      .fill_data

    key_path.pop
  end
end