Class: Documentation::Searchers::Simple

Inherits:
Abstract
  • Object
show all
Defined in:
lib/documentation/searchers/simple.rb

Instance Attribute Summary

Attributes inherited from Abstract

#options

Instance Method Summary collapse

Methods inherited from Abstract

#delete, #initialize, #reset, #setup

Constructor Details

This class inherits a constructor from Documentation::Searchers::Abstract

Instance Method Details

#index(page) ⇒ Object



34
35
36
# File 'lib/documentation/searchers/simple.rb', line 34

def index(page)
  true
end

#search(query, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/documentation/searchers/simple.rb', line 5

def search(query, options = {})
  # Default options
  options[:page]      ||= 1
  options[:per_page]  ||= 15
  
  # Query string 
  query_string = "content LIKE ? OR title LIKE ?", "%#{query}%", "%#{query}"
  
  # Get the total number of pages
  total_results = pages = Documentation::Page.where(query_string).count
  
  # Get the actual pages
  pages = Documentation::Page.where(query_string)
  pages = pages.offset((options[:page].to_i - 1) * options[:per_page].to_i)
  pages = pages.limit(options[:per_page].to_i)
  
  # Create a result object
  result                = Documentation::SearchResult.new
  result.page           = options[:page].to_i
  result.per_page       = options[:per_page].to_i
  result.total_results  = total_results
  result.query          = query
  result.time           = 0
  result.results        = pages
  
  # Return the result
  result
end