Class: ForestLiana::ResourcesGetter

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

Instance Method Summary collapse

Constructor Details

#initialize(resource, params) ⇒ ResourcesGetter

Returns a new instance of ResourcesGetter.



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

def initialize(resource, params)
  @resource = resource
  @params = params
  @count_needs_includes = false
  @field_names_requested = field_names_requested

  get_segment()
end

Instance Method Details

#countObject



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

def count
  @records_to_count.count
end

#includesObject



42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/forest_liana/resources_getter.rb', line 42

def includes
  includes = SchemaUtils.one_associations(@resource)
    .select { |association| SchemaUtils.model_included?(association.klass) }
    .map(&:name)

  if @field_names_requested
    includes & @field_names_requested
  else
    includes
  end
end

#performObject



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

def perform
  @records = @resource.unscoped

  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

#recordsObject



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

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