Class: StaticSearch::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/static_search/search.rb

Class Method Summary collapse

Class Method Details

.find_content(q, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/static_search/search.rb', line 3

def self.find_content(q, options = {})
  return [] unless q.present?
  result = StaticContent.where("LOWER(content) like ? OR LOWER(title) like ?", "% #{q} %", "%#{q}%");
  if options[:truncation]
    return result.map do |page|
      page = page.as_json
      page["content"] = truncate_body(page["content"], q, options[:truncation])
      page
    end
  else
    return result.map(&:as_json)
  end
end

.truncate_body(content, q, truncation_size) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/static_search/search.rb', line 17

def self.truncate_body content, q, truncation_size
  content = \
    if content.include? q
      index = content.downcase.index(q.downcase) || 0
      content[index..(index + truncation_size)]
    else
      content[0..truncation_size]
    end
  if content && content.size > truncation_size
    content << "..."
  end
  return content
end