Module: ActsAsExplorable::Query

Defined in:
lib/acts_as_explorable/query.rb

Overview

Adds the search scope to a model

Instance Method Summary collapse

Instance Method Details

#search(query_string) ⇒ ActiveRecord::Relation

Initiates a search with the given query string and returns an ActiveRecord::Relation scope object.

The query string’s syntax is described in the Readme File

The search method can be used just like a scope.

Foo.search("Foo Bar in:name,body sort:created_at-asc").to_sql
# => "SELECT foo.* FROM foo WHERE (foo.body ILIKE '%Foo%' OR foo.body ILIKE '%Bar%') ORDER BY foo.created_at ASC"

It is also possible to put it in a scope chain like this:

Foo.published.search("Foo Bar in:name,body sort:created_at-asc")

Parameters:

  • query_string (String)

    A query string

Returns:

  • (ActiveRecord::Relation)

    Returns an ActiveRecord::Relation scope object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/acts_as_explorable/query.rb', line 24

def search(query_string)
  parts = ActsAsExplorable.filters.keys.map { |t| Element.build(t, query_string, self) }

  result = all

  parts.compact.each do |part|
    result = part.execute(result)
  end

  result
end