Class: PackAPI::Querying::CollectionQuery
- Inherits:
-
Object
- Object
- PackAPI::Querying::CollectionQuery
- 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
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#collection_key ⇒ Object
Returns the value of attribute collection_key.
-
#default_sort ⇒ Object
Returns the value of attribute default_sort.
-
#filter_factory ⇒ Object
Returns the value of attribute filter_factory.
-
#paginator ⇒ Object
Returns the value of attribute paginator.
-
#results ⇒ Object
Returns the value of attribute results.
-
#sort ⇒ Object
Returns the value of attribute sort.
Instance Method Summary collapse
-
#call(cursor: nil, per_page: nil, sort: nil, search: nil, filters: {}) ⇒ Object
Perform the query.
- #current_page_snapshot_cursor ⇒ Object
-
#initialize(collection:, collection_key: nil, default_sort: nil) ⇒ CollectionQuery
constructor
Create the query object.
- #reset ⇒ Object
- #to_sql ⇒ Object
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
#collection ⇒ Object
Returns the value of attribute collection.
12 13 14 |
# File 'lib/pack_api/querying/collection_query.rb', line 12 def collection @collection end |
#collection_key ⇒ Object
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_sort ⇒ Object
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_factory ⇒ Object
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 |
#paginator ⇒ Object
Returns the value of attribute paginator.
12 13 14 |
# File 'lib/pack_api/querying/collection_query.rb', line 12 def paginator @paginator end |
#results ⇒ Object
Returns the value of attribute results.
12 13 14 |
# File 'lib/pack_api/querying/collection_query.rb', line 12 def results @results end |
#sort ⇒ Object
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.
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_cursor ⇒ Object
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 |
#reset ⇒ Object
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_sql ⇒ Object
44 45 46 |
# File 'lib/pack_api/querying/collection_query.rb', line 44 def to_sql @query.to_sql end |