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.



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

#includesObject (readonly)

Returns the value of attribute includes.



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

def includes
  @includes
end

#records_countObject (readonly)

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_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



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

#compute_includesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/forest_liana/resources_getter.rb', line 48

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



36
37
38
# File 'app/services/forest_liana/resources_getter.rb', line 36

def count
  @records_count = @count_needs_includes ? optimized_count : @records.count
end

#includes_for_serializationObject



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

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

#performObject



32
33
34
# File 'app/services/forest_liana/resources_getter.rb', line 32

def perform
  @records = optimize_record_loading(@resource, @records)
end

#query_for_batchObject



40
41
42
# File 'app/services/forest_liana/resources_getter.rb', line 40

def query_for_batch
  @records
end

#recordsObject



44
45
46
# File 'app/services/forest_liana/resources_getter.rb', line 44

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