Class: ActiveRecord::Searchable::QueryBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, config, adapter) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



8
9
10
11
12
# File 'lib/activerecord/searchable/query_builder.rb', line 8

def initialize(model_class, config, adapter)
  @model_class = model_class
  @config = config
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



6
7
8
# File 'lib/activerecord/searchable/query_builder.rb', line 6

def adapter
  @adapter
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/activerecord/searchable/query_builder.rb', line 6

def config
  @config
end

#model_classObject (readonly)

Returns the value of attribute model_class.



6
7
8
# File 'lib/activerecord/searchable/query_builder.rb', line 6

def model_class
  @model_class
end

Instance Method Details

#build_search_scope(relation, terms, matches: false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/activerecord/searchable/query_builder.rb', line 14

def build_search_scope(relation, terms, matches: false)
  sanitized_terms = sanitize_query(terms)

  return relation.none if sanitized_terms.nil?

  scope = relation
    .joins(adapter.join_clause(config))
    .where(adapter.where_clause(config), sanitized_terms)

  # Only set select clause when we need extra columns for highlighting/snippets
  # This allows ActiveRecord's .count to work correctly (it generates COUNT(*))
  scope = scope.select(select_clause(matches)) if select_clause(matches)

  scope.order(adapter.ranking_order_clause(config))
end

#sanitize_query(terms) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/activerecord/searchable/query_builder.rb', line 30

def sanitize_query(terms)
  terms = terms.to_s
  terms = remove_invalid_characters(terms)
  terms = remove_unbalanced_quotes(terms)
  terms = terms.strip.gsub(/\s+/, " ")  # Normalize whitespace
  terms.presence
end