class Brainstem::QueryStrategies::FilterOrSearch

Public Instance Methods

execute(scope) click to toggle source
# 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

Private Instance Methods

handle_only(scope, only) click to toggle source
# File lib/brainstem/query_strategies/filter_or_search.rb, line 75
def handle_only(scope, only)
  ids = (only || "").split(",").select {|id| id =~ /\A\d+\z/}.uniq
  [scope.where(:id => ids), scope.where(:id => ids)]
end
paginate(scope) click to toggle source
# File lib/brainstem/query_strategies/filter_or_search.rb, line 70
def paginate(scope)
  limit, offset = calculate_limit_and_offset
  [scope.limit(limit).offset(offset).distinct, scope.select("distinct #{scope.connection.quote_table_name @options[:table_name]}.id")]
end
searching?() click to toggle source
# File lib/brainstem/query_strategies/filter_or_search.rb, line 40
def searching?
  @options[:params][:search] && @options[:primary_presenter].configuration[:search].present?
end