Class: PgSearchable::Model::Searcher

Inherits:
Object
  • Object
show all
Defined in:
lib/pg-searchable/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Searcher

Returns a new instance of Searcher.



19
20
21
# File 'lib/pg-searchable/model.rb', line 19

def initialize(model)
  @model = model
end

Instance Method Details

#dictionaryObject



23
24
25
# File 'lib/pg-searchable/model.rb', line 23

def dictionary
  Arel::Nodes.build_quoted(:simple)
end

#exec(column_name, query, raw: false, prefix: false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pg-searchable/model.rb', line 27

def exec(column_name, query, raw:false, prefix:false)
  column_ref = "#{@model.quoted_table_name}.#{column_name}"
  
  vector = Arel::Nodes::NamedFunction.new(
    "to_tsvector",
    [dictionary, Arel.sql(column_ref)]
  )
  
  query = Builder.tsquery(query, prefix: prefix, raw: raw)
  
  condition = Arel::Nodes::Grouping.new(
    Arel::Nodes::InfixOperation.new("@@", vector, query)
  )
  
  condition
end