Module: NuecaRailsInterfaces::V1::QueryInterface
- Defined in:
- lib/nueca_rails_interfaces/v1/query_interface.rb
Overview
Query Mixin Interface. Include this module to a class that will be used as a query object. Query objects are eaily identified as helpers for querying with filters and sorting with complex algorithms. Thanks to ActiveRecord’s inner workings, Ruby alone can handle the avdanced filtering before firing the query in the database. In this version, all query objects will be paginated. This is to avoid really heavy queries from hitting the database, either be it intentnional or malicious. In this version, pagination is enforced but only lightly encouraged. There will be a deprecation warning when no_pagination is used. However, the implementation of no_pagination is still a 1 page result; just that it supports a large number of queries in a single page. Developers will be required to move away from V1 of Query Interface soon to enforce strict pagination.
Constant Summary collapse
- VALID_PAGINATION_HASH =
The basis for validity of pagination settings. It also contains default values.
{ max: 20, # Absolute maximum number of records per page, even if the query requests for more. min: 1, # Absolute minimum number of records per page, even if the query requests for less. per_page: 20, # Default number of records per page if not specified in the query. page: 1 # Default page number if not specified in the query. }.freeze
- NO_PAGING_THRESHOLD =
Basis for considering a non-paging result even when the query is being processed for pagination. This number states the invalidity of pagination, but it exists for legacy support.
1_000_000
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Class Method Summary collapse
Instance Method Summary collapse
-
#call ⇒ Object
Do not override.
-
#initialize(query, collection, pagination: true) ⇒ Object
Do not override! This is how we will always initialize our query objects.
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
39 40 41 |
# File 'lib/nueca_rails_interfaces/v1/query_interface.rb', line 39 def collection @collection end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
39 40 41 |
# File 'lib/nueca_rails_interfaces/v1/query_interface.rb', line 39 def query @query end |
Class Method Details
.included(base) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/nueca_rails_interfaces/v1/query_interface.rb', line 29 def included(base) # This is the method to call outside this object to apply the query filters, sortings and paginations. # @param [Hash] query The query parameters. # @param [ActiveRecord::Relation] collection The collection to be queried. base.define_singleton_method(:call) do |query, collection| new(query, collection).call end end |
Instance Method Details
#call ⇒ Object
Do not override. This is the method to call outside this object to apply the query filters, sortings and paginations.
54 55 56 57 58 59 |
# File 'lib/nueca_rails_interfaces/v1/query_interface.rb', line 54 def call apply_filters! apply_sorting! apply_pagination! collection end |
#initialize(query, collection, pagination: true) ⇒ Object
Do not override! This is how we will always initialize our query objects. No processing should be done in the initialize method.
45 46 47 48 49 50 |
# File 'lib/nueca_rails_interfaces/v1/query_interface.rb', line 45 def initialize(query, collection, pagination: true) @query = query @collection = collection @pagination_flag = pagination query_aliases end |