Class: ForestLiana::HasManyGetter

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

Constant Summary collapse

SUPPORTED_ASSOCIATION_MACROS =
[:belongs_to, :has_one, :has_and_belongs_to_many].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseGetter

#get_collection, #get_resource, #includes_for_serialization

Constructor Details

#initialize(resource, association, params, forest_user) ⇒ HasManyGetter

Returns a new instance of HasManyGetter.



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

def initialize(resource, association, params, forest_user)
  @resource = resource
  @association = association
  @params = params
  @collection_name = ForestLiana.name_for(model_association)
  @field_names_requested = field_names_requested
  @collection = get_collection(@collection_name)
  compute_includes()
  includes_symbols = @includes.map { |include| include.to_sym }
  @search_query_builder = SearchQueryBuilder.new(@params, includes_symbols, @collection, forest_user)

  prepare_query()
end

Instance Attribute Details

#includesObject (readonly)

Returns the value of attribute includes.



4
5
6
# File 'app/services/forest_liana/has_many_getter.rb', line 4

def includes
  @includes
end

#records_countObject (readonly)

Returns the value of attribute records_count.



5
6
7
# File 'app/services/forest_liana/has_many_getter.rb', line 5

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/has_many_getter.rb', line 3

def search_query_builder
  @search_query_builder
end

Instance Method Details

#countObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/forest_liana/has_many_getter.rb', line 27

def count
  association_class = model_association

  if association_class.primary_key.is_a?(Array)
    adapter_name = association_class.connection.adapter_name.downcase

    if adapter_name.include?('sqlite')
      # For SQLite: concatenate columns for DISTINCT count
      pk_concat = association_class.primary_key.map do |pk|
        "#{association_class.table_name}.#{pk}"
      end.join(" || '|' || ")

      @records_count = @records.distinct.count(Arel.sql(pk_concat))
    else
      # For PostgreSQL/MySQL: use DISTINCT with multiple columns
      pk_columns = association_class.primary_key.map do |pk|
        "#{association_class.table_name}.#{pk}"
      end.join(', ')

      @records_count = @records.distinct.count(Arel.sql(pk_columns))
    end
  else
    @records_count = @records.count
  end
end

#performObject



23
24
25
# File 'app/services/forest_liana/has_many_getter.rb', line 23

def perform
  @records
end

#query_for_batchObject



53
54
55
# File 'app/services/forest_liana/has_many_getter.rb', line 53

def query_for_batch
  @records
end

#recordsObject



57
58
59
# File 'app/services/forest_liana/has_many_getter.rb', line 57

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