Class: ForestLiana::ResourcesGetter

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

Instance Method Summary collapse

Methods inherited from BaseGetter

#get_collection, #get_resource

Constructor Details

#initialize(resource, params) ⇒ ResourcesGetter

Returns a new instance of ResourcesGetter.



3
4
5
6
7
8
9
10
11
# File 'app/services/forest_liana/resources_getter.rb', line 3

def initialize(resource, params)
  @resource = resource
  @params = params
  @count_needs_includes = false
  @collection_name = ForestLiana.name_for(@resource)
  @collection = get_collection(@collection_name)
  @field_names_requested = field_names_requested
  get_segment()
end

Instance Method Details

#countObject



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

def count
  @records_to_count.count
end

#includesObject



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

def includes
  includes = SchemaUtils.one_associations(@resource)
    .select { |association| SchemaUtils.model_included?(association.klass) }
    .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 & @field_names_requested).concat(includes_for_smart_search)
  else
    includes
  end
end

#performObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/forest_liana/resources_getter.rb', line 13

def perform
  @records = get_resource

  if @segment && @segment.scope
    @records = @records.send(@segment.scope)
  elsif @segment && @segment.where
    @records = @records.where(@segment.where.call())
  end

  @records = search_query
  @records_to_count = @records

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

  @records = @records.eager_load(includes)
  @records_sorted = sort_query
end

#query_for_batchObject



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

def query_for_batch
  @records
end

#recordsObject



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

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