Class: Brainstem::QueryStrategies::FilterOrSearch

Inherits:
BaseStrategy
  • Object
show all
Defined in:
lib/brainstem/query_strategies/filter_or_search.rb

Instance Method Summary collapse

Methods inherited from BaseStrategy

#calculate_per_page, #evaluate_count, #evaluate_scope, #initialize

Constructor Details

This class inherits a constructor from Brainstem::QueryStrategies::BaseStrategy

Instance Method Details

#execute(scope) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/brainstem/query_strategies/filter_or_search.rb', line 4

def execute(scope)
  if searching?
    # Search
    sort_name, direction = @options[:primary_presenter].calculate_sort_name_and_direction @options[:params]
    scope, count, ordered_search_ids = run_search(scope, filter_includes.map(&:name), sort_name, direction)

    # Load models!
    primary_models = scope.to_a

    primary_models = order_for_search(primary_models, ordered_search_ids)
  else
    # Filter

    scope = @options[:primary_presenter].apply_filters_to_scope(scope, @options[:params], @options)

    if @options[:params][:only].present?
      # Handle Only
      scope, count_scope = handle_only(scope, @options[:params][:only])
    else
      # Paginate
      scope, count_scope = paginate scope
    end

    # Ordering
    scope = @options[:primary_presenter].apply_ordering_to_scope(scope, @options[:params])

    primary_models = evaluate_scope(scope)
    count = evaluate_count(count_scope)
    count = count.keys.length if count.is_a?(Hash)
  end

  [primary_models, count]
end