Class: Insights::API::Common::PaginatedResponseV2
- Inherits:
-
PaginatedResponse
- Object
- PaginatedResponse
- Insights::API::Common::PaginatedResponseV2
- 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
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#sort_by ⇒ Object
readonly
Returns the value of attribute sort_by.
Instance Method Summary collapse
-
#compact_parameter(param) ⇒ Object
Condenses parameter values for handling multi-level associations and returns an array of key, value pairs.
- #records ⇒ Object
Methods inherited from PaginatedResponse
Constructor Details
This class inherits a constructor from Insights::API::Common::PaginatedResponse
Instance Attribute Details
#limit ⇒ Object (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 |
#offset ⇒ Object (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_by ⇒ Object (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 |
#records ⇒ Object
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? = (res.klass) res = res.reorder() if .present? res end end |