Module: Libertree::Model::HasSearchableText

Included in:
Comment, Post
Defined in:
lib/libertree/model/has-searchable-text.rb

Overview

Provides a “search” class method to a class that has a “text” field and a time_created field.

Instance Method Summary collapse

Instance Method Details

#search(q, limit = 42, exact = true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/libertree/model/has-searchable-text.rb', line 6

def search(q, limit = 42, exact=true)
  if exact
    dict = 'simple'
  else
    dict = 'english'
  end

  self.where(
    Sequel.lit("(to_tsvector('simple', text) || to_tsvector('english', text)) @@ plainto_tsquery('#{dict}', ?)", q)
  ).reverse_order(:time_created)
  .limit(limit.to_i)
  .all
end