Class: ForestLiana::ResourcesGetter

Inherits:
BaseGetter show all
Defined in:
app/services/forest_liana/resources_getter.rb

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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/services/forest_liana/resources_getter.rb', line 7

def initialize(resource, params, forest_user)
  @resource = resource
  @params = params
  @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
  get_segment
  compute_includes
  @user = forest_user
  @search_query_builder = SearchQueryBuilder.new(@params, @includes, @collection, forest_user)

  prepare_query
end

Instance Attribute Details

#includesObject (readonly)

Returns the value of attribute includes.



4
5
6
# File 'app/services/forest_liana/resources_getter.rb', line 4

def includes
  @includes
end

#records_countObject (readonly)

Returns the value of attribute records_count.



5
6
7
# File 'app/services/forest_liana/resources_getter.rb', line 5

def records_count
  @records_count
end

#search_query_builderObject (readonly)

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



23
24
25
26
27
28
29
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
# File 'app/services/forest_liana/resources_getter.rb', line 23

def self.get_ids_from_request(params, user)
  attributes = params.dig('data', 'attributes')
  has_body_attributes = attributes != nil
  is_select_all_records_query = has_body_attributes && attributes[:all_records] == true

  # NOTICE: If it is not a "select all records" query and it receives a list of ID, return list of ID.
  return attributes[:ids] if (!is_select_all_records_query && attributes[:ids])

  # NOTICE: If it is a "select all records" we have to perform query to build ID list.
  ids = Array.new

  # NOTICE: Merging all_records_subset_query into attributes preserves filters in HasManyGetter and ResourcesGetter.
  attributes = attributes.merge(attributes[:all_records_subset_query].dup.to_unsafe_h)

  # NOTICE: Initialize actual resources getter (could either a HasManyGetter or a ResourcesGetter).
  is_related_data = attributes[:parent_collection_id] &&
    attributes[:parent_collection_name] &&
    attributes[:parent_association_name]
  if is_related_data
    parent_collection_name = attributes[:parent_collection_name]
    parent_model = ForestLiana::SchemaUtils.find_model_from_collection_name(parent_collection_name)
    model = parent_model.reflect_on_association(attributes[:parent_association_name].try(:to_sym))
    resources_getter = ForestLiana::HasManyGetter.new(parent_model, model, attributes.merge({
      collection: parent_collection_name,
      id: attributes[:parent_collection_id],
      association_name: attributes[:parent_association_name],
    }), user)
  else
    collection_name = attributes[:collection_name]
    model = ForestLiana::SchemaUtils.find_model_from_collection_name(collection_name)
    resources_getter = ForestLiana::ResourcesGetter.new(model, attributes, user)
  end

  # NOTICE: build IDs list.
  resources_getter.query_for_batch.find_in_batches() do |records|
    ids += records.map { |record| record.id }
  end

  # NOTICE: remove excluded IDs.
  ids_excluded = (attributes[:all_records_ids_excluded]).map { |id_excluded| id_excluded.to_s }
  return ids.select { |id| !ids_excluded.include? id.to_s } if (ids_excluded && ids_excluded.any?)

  return ids
end

Instance Method Details

#compute_includesObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/services/forest_liana/resources_getter.rb', line 86

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

#countObject



72
73
74
75
76
# File 'app/services/forest_liana/resources_getter.rb', line 72

def count
  # NOTICE: For performance reasons, do not eager load the data if there is  no search or
  #         filters on associations.
  @records_count = @count_needs_includes ? @records.eager_load(@includes).count : @records.count
end

#includes_for_serializationObject



111
112
113
# File 'app/services/forest_liana/resources_getter.rb', line 111

def includes_for_serialization
  super & @fields_to_serialize.map(&:to_s)
end

#performObject



68
69
70
# File 'app/services/forest_liana/resources_getter.rb', line 68

def perform
  @records = @records.eager_load(@includes)
end

#query_for_batchObject



78
79
80
# File 'app/services/forest_liana/resources_getter.rb', line 78

def query_for_batch
  @records
end

#recordsObject



82
83
84
# File 'app/services/forest_liana/resources_getter.rb', line 82

def records
  @records.offset(offset).limit(limit).to_a
end