Class: Insights::API::Common::PaginatedResponseV2

Inherits:
PaginatedResponse show all
Defined in:
lib/insights/api/common/paginated_response_v2.rb

Constant Summary collapse

ASSOCIATION_COUNT_ATTR =

GraphQL name regex: /[_A-Za-z]*/

"__count".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PaginatedResponse

#initialize, #response

Constructor Details

This class inherits a constructor from Insights::API::Common::PaginatedResponse

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



8
9
10
# File 'lib/insights/api/common/paginated_response_v2.rb', line 8

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



8
9
10
# File 'lib/insights/api/common/paginated_response_v2.rb', line 8

def offset
  @offset
end

#sort_byObject (readonly)

Returns the value of attribute sort_by.



8
9
10
# File 'lib/insights/api/common/paginated_response_v2.rb', line 8

def sort_by
  @sort_by
end

Instance Method Details

#compact_parameter(param) ⇒ Object

Condenses parameter values for handling multi-level associations and returns an array of key, value pairs.

Examples:

Input: { “association” => { “attribute” => “value” }, “direct_attribute” => “value2” } Output: [[“association.attribute”, “value”], [“direct_attribute”, “value2”]]

Input: { “association” => { “attribute” => “value” }, “association2” => { “attribute2” => “value2” } } Output: [[“association.attribute”, “value”], [“association2.attribute2”, “value2”]]

Input: { “association” => { “attribute1” => “value1”, “attribute2” => “value2” } } Output: [[“association.attribute1”, “value1”], [“association.attribute2”, “value2”]]



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/insights/api/common/paginated_response_v2.rb', line 39

def compact_parameter(param)
  result = []
  return result if param.blank?

  param.each do |k, v|
    result += if v.kind_of?(Hash) || v.kind_of?(ActionController::Parameters)
                Hash(v).map { |ak, av| ["#{k}.#{ak}", av] }
              else
                [[k, v]]
              end
  end
  result
end

#recordsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/insights/api/common/paginated_response_v2.rb', line 10

def records
  @records ||= begin
    res = @base_query.order(:id).limit(limit).offset(offset)

    select_for_associations, group_by_associations = sort_by_associations_query_parameters
    res = res.select(*select_for_associations)          if select_for_associations.present?
    res = res.left_outer_joins(*sort_by_associations)   if sort_by_associations.present?
    res = res.group(group_by_associations)              if group_by_associations.present?

    order_options = sort_by_options(res.klass)
    res = res.reorder(order_options) if order_options.present?
    res
  end
end