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
# File 'app/services/forest_liana/resources_getter.rb', line 3

def initialize(resource, params)
  @resource = resource
  @params = params
  @field_names_requested = field_names_requested
end

Instance Method Details

#countObject



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

def count
  @records.count
end

#field_names_requestedObject



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

def field_names_requested
  return nil unless @params[:fields] && @params[:fields][@resource.table_name]

  associations_for_query = []

  # NOTICE: Populate the necessary associations for filters
  if @params[:filter]
    @params[:filter].each do |field, values|
      if field.include? ':'
        associations_for_query << field.split(':').first.to_sym
      end
    end
  end

  if @params[:sort] && @params[:sort].include?('.')
    associations_for_query << @params[:sort].split('.').first.to_sym
  end

  field_names = @params[:fields][@resource.table_name].split(',')
                                          .map { |name| name.to_sym }
  field_names | associations_for_query
end

#includesObject



46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/forest_liana/resources_getter.rb', line 46

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



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

def perform
  @records = @resource.unscoped.eager_load(includes)
  @records = search_query
  @sorted_records = sort_query
end

#recordsObject



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

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