Class: ForestLiana::HasManyGetter

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

Instance Method Summary collapse

Methods inherited from BaseGetter

#get_current_collection, #get_resource

Constructor Details

#initialize(resource, association, params) ⇒ HasManyGetter



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

def initialize(resource, association, params)
  @resource = resource
  @association = association
  @params = params
  @field_names_requested = field_names_requested
  @current_collection = get_current_collection(association_table_name)
end

Instance Method Details

#countObject



51
52
53
# File 'app/services/forest_liana/has_many_getter.rb', line 51

def count
  @records.to_a.length
end

#includesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/forest_liana/has_many_getter.rb', line 26

def includes
  @association.klass
    .reflect_on_all_associations
    .select do |association|
      inclusion = !association.options[:polymorphic] &&
        SchemaUtils.model_included?(association.klass) &&
        [:belongs_to, :has_and_belongs_to_many].include?(association.macro)

      if @field_names_requested
        inclusion && @field_names_requested.include?(association.name)
      else
        inclusion
      end
    end
    .map { |association| association.name.to_s }
end

#performObject



11
12
13
14
15
16
17
18
# File 'app/services/forest_liana/has_many_getter.rb', line 11

def perform
  @records = get_resource()
    .find(@params[:id])
    .send(@params[:association_name])
    .eager_load(includes)
  @records = search_query
  @records = sort_query
end

#query_for_batchObject



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

def query_for_batch
  @records
end

#recordsObject



47
48
49
# File 'app/services/forest_liana/has_many_getter.rb', line 47

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

#search_queryObject



20
21
22
23
24
# File 'app/services/forest_liana/has_many_getter.rb', line 20

def search_query
  includesSymbols = includes.map { |association| association.to_sym }
  SearchQueryBuilder.new(@records, @params,
    includesSymbols, @current_collection).perform
end