Class: PackAPI::Querying::CollectionQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/pack_api/querying/collection_query.rb

Overview

This is a generic query that can be used to query any collection of ActiveRecord models and then expose the results as a collection of value objects.

It allows paginated queries, as well as record iteration queries (using snapshot cursors). In order to enable record iteration mode, the query must be called with both a snapshot cursor and a primary key filter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection:, collection_key: nil, default_sort: nil) ⇒ CollectionQuery

Create the query object.

@ param [ActiveRecord::Relation] collection The collection to query @ param [Object] value_object_factory The factory to use to convert model objects to value objects @ param [String|Symbol|Hash|Arel] default_sort The default sort to use if none is specified in the query @ param [String|Symbol] collection_key Unique and indexed key to use for the collection.

Used for tie-breaker sort criteria and drives the record id lookup when in record iteration mode.


22
23
24
25
26
27
28
# File 'lib/pack_api/querying/collection_query.rb', line 22

def initialize(collection:, collection_key: nil, default_sort: nil)
  @collection = collection
  @collection_key = (collection_key || collection.primary_key).to_sym
  @default_sort = default_sort
  @filter_factory = FilterFactory.new
  @filter_factory.use_default_filter = true
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



12
13
14
# File 'lib/pack_api/querying/collection_query.rb', line 12

def collection
  @collection
end

#collection_keyObject

Returns the value of attribute collection_key.



12
13
14
# File 'lib/pack_api/querying/collection_query.rb', line 12

def collection_key
  @collection_key
end

#default_sortObject

Returns the value of attribute default_sort.



12
13
14
# File 'lib/pack_api/querying/collection_query.rb', line 12

def default_sort
  @default_sort
end

#filter_factoryObject

Returns the value of attribute filter_factory.



12
13
14
# File 'lib/pack_api/querying/collection_query.rb', line 12

def filter_factory
  @filter_factory
end

#paginatorObject

Returns the value of attribute paginator.



12
13
14
# File 'lib/pack_api/querying/collection_query.rb', line 12

def paginator
  @paginator
end

#resultsObject

Returns the value of attribute results.



12
13
14
# File 'lib/pack_api/querying/collection_query.rb', line 12

def results
  @results
end

#sortObject

Returns the value of attribute sort.



12
13
14
# File 'lib/pack_api/querying/collection_query.rb', line 12

def sort
  @sort
end

Instance Method Details

#call(cursor: nil, per_page: nil, sort: nil, search: nil, filters: {}) ⇒ Object

Perform the query.

Parameters:

  • cursor (String) (defaults to: nil)

    A pagination cursor referencing a page of records in a recordset

  • per_page (Integer|Symbol) (defaults to: nil)

    A count of how many items to include on each page, or :all to skip pagination

  • sort (String|Symbol|Hash|Arel) (defaults to: nil)

    ActiveRecord ‘order` arguments.

  • search (Hash) (defaults to: nil)

    Attribute/value pairs that define the bounds of the recordset using wildcard matching (attribute LIKE %value%)

  • filters (Hash) (defaults to: {})

    the keys are names of filters, the values are arguments to the filters



38
39
40
41
42
# File 'lib/pack_api/querying/collection_query.rb', line 38

def call(cursor: nil, per_page: nil, sort: nil, search: nil, filters: {})
  record_id = filters.delete(collection_key) if cursor.present? && filters[collection_key].present?
  build_paginator(cursor, filters, per_page, search, sort)
  build_active_record_query(record_id)
end

#current_page_snapshot_cursorObject



56
57
58
59
60
# File 'lib/pack_api/querying/collection_query.rb', line 56

def current_page_snapshot_cursor
  @current_page_snapshot_cursor ||= PackAPI::Pagination::SnapshotPaginator.cursor_for_results(results,
                                                                                              table_name: collection.klass.table_name,
                                                                                              collection_key:)
end

#resetObject



48
49
50
51
52
53
54
# File 'lib/pack_api/querying/collection_query.rb', line 48

def reset
  @results = nil
  @query = nil
  @paginator = nil
  @sort = nil
  @current_page_snapshot_cursor = nil
end

#to_sqlObject



44
45
46
# File 'lib/pack_api/querying/collection_query.rb', line 44

def to_sql
  @query.to_sql
end