Module: NuecaRailsInterfaces::V2::QueryInterface

Defined in:
lib/nueca_rails_interfaces/v2/query_interface.rb

Overview

V2 Query Interface is the same as V1 Query Interface, but with changes in the pagination logic. V1 had tight coupling with WillPaginate, this version decouples the query interface from it, allowing for custom pagination adapters to be used.

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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



38
39
40
# File 'lib/nueca_rails_interfaces/v2/query_interface.rb', line 38

def collection
  @collection
end

#queryObject (readonly)

Returns the value of attribute query.



38
39
40
# File 'lib/nueca_rails_interfaces/v2/query_interface.rb', line 38

def query
  @query
end

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/nueca_rails_interfaces/v2/query_interface.rb', line 28

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

#callObject

Do not override. This is the method to call outside this object to apply the query filters, sortings and paginations.



53
54
55
56
57
58
# File 'lib/nueca_rails_interfaces/v2/query_interface.rb', line 53

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.

Parameters:

  • query (Hash)

    The query parameters.

  • collection (ActiveRecord::Relation)

    The collection to be queried.



44
45
46
47
48
49
# File 'lib/nueca_rails_interfaces/v2/query_interface.rb', line 44

def initialize(query, collection, pagination: true)
  @query = query
  @collection = collection
  @pagination_flag = pagination
  query_aliases
end