Class: JsonApiFilter::Dispatch

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_filter/dispatch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, params, allowed_filters:, allowed_searches:) ⇒ Dispatch

Returns a new instance of Dispatch.

Parameters:

  • scope (ActiveRecord::Base)
  • params (Hash, ActionController::Parameters)
  • allowed_filters (Array<Symbol>)


9
10
11
12
13
14
# File 'lib/json_api_filter/dispatch.rb', line 9

def initialize(scope, params, allowed_filters:, allowed_searches:)
  @params = params
  @scope = scope
  @allowed_filters = allowed_filters
  @allowed_searches = allowed_searches
end

Instance Attribute Details

#allowed_filtersObject (readonly)

Returns the value of attribute allowed_filters.



4
5
6
# File 'lib/json_api_filter/dispatch.rb', line 4

def allowed_filters
  @allowed_filters
end

#allowed_searchesObject (readonly)

Returns the value of attribute allowed_searches.



4
5
6
# File 'lib/json_api_filter/dispatch.rb', line 4

def allowed_searches
  @allowed_searches
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/json_api_filter/dispatch.rb', line 4

def params
  @params
end

#scopeObject (readonly)

Returns the value of attribute scope.



4
5
6
# File 'lib/json_api_filter/dispatch.rb', line 4

def scope
  @scope
end

Instance Method Details

#processActiveRecord_Relation

Returns:

  • (ActiveRecord_Relation)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/json_api_filter/dispatch.rb', line 17

def process
  @scope = [
    scope.all,
    sort_predicate,
    filters_predicate,
    search_predicate,
    join_predicate,
  ].compact.reduce(&:merge)
  return scope if params[:pagination].nil?
  scope.merge(pagination_predicate)
end