Class: ActionController::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/search_autocomplete/searchable.rb

Overview

Searchable

Instance Method Summary collapse

Instance Method Details

#search(model, approximate_fields = [], exact_fields = [], include_list = nil) ⇒ Object

Performs a search on the model based on permitted fields

Parameters:

  • model (Class)

    Model to perform the search

  • approximate_fields (Array{Symbol}) (defaults to: [])

    List of fields to allow as approximate filters

  • exact_fields (Array{Symbol}) (defaults to: [])

    List of fields to allow as exact filters

  • include_list (Array{Symbol}) (defaults to: nil)

    List of related resources to include



14
15
16
17
18
19
20
21
22
23
# File 'lib/search_autocomplete/searchable.rb', line 14

def search(model, approximate_fields = [], exact_fields = [], include_list = nil)
  arel_table = model.arel_table

  search_conditions = prepare_search_fields arel_table, exact_fields
  search_conditions += prepare_search_fields(arel_table, approximate_fields, false)

  query = include_list.present? ? model.includes(include_list) : model
  query = query.where(*search_conditions) if search_conditions.length.positive?
  query
end