Class: FulltextRow

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/fulltext_row.rb

Overview

FulltextRow

2008-03-07

Patched by Artūras Šlajus <[email protected]> for will_paginate support

2008-06-19

Fixed a bug, see acts_as_fulltextable.rb

Constant Summary collapse

@@use_advanced_search =
false
@@use_and_search =
false
@@use_phrase_search =
false

Class Method Summary collapse

Class Method Details

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

Performs full-text search. It takes four options:

  • limit: maximum number of rows to return (use 0 for all). Defaults to 10.

  • offset: offset to apply to query. Defaults to 0.

  • page: only available with will_paginate.

  • active_record: wether a ActiveRecord objects should be returned or an Array of [class_name, id]

  • only: limit search to these classes. Defaults to all classes. (should be a symbol or an Array of symbols)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fulltext_row.rb', line 32

def self.search(query, options = {})
  default_options = {:active_record => true, :parent_id => nil}
  options = default_options.merge(options)
  unless options[:page]
    options = {:limit => 10, :offset => 0}.merge(options)
    options[:offset] = 0 if options[:offset] < 0
    unless options[:limit].nil?
      options[:limit] = 10 if options[:limit] < 0
      options[:limit] = nil if options[:limit] == 0
    end
  end
  options[:only] = [options[:only]] unless options[:only].nil? || options[:only].is_a?(Array)
  options[:only] = options[:only].map {|o| o.to_s.camelize}.uniq.compact unless options[:only].nil?

  rows = raw_search(query, options[:only], options[:limit], 
    options[:offset], options[:parent_id], options[:page], 
    options[:search_class])
  if options[:active_record]
    types = {}
    rows.each {|r| types.include?(r.fulltextable_type) ? (types[r.fulltextable_type] << r.fulltextable_id) : (types[r.fulltextable_type] = [r.fulltextable_id])}
    objects = {}
    types.each {|k, v| objects[k] = Object.const_get(k).find_all_by_id(v)}
    objects.each {|k, v| v.sort! {|x, y| types[k].index(x.id) <=> types[k].index(y.id)}}

    if defined?(WillPaginate) && options[:page]
      result = WillPaginate::Collection.new(
        rows.current_page,
        rows.per_page,
        rows.total_entries
      )
    else
      result = []
    end

    rows.each {|r| result << objects[r.fulltextable_type].shift}
    return result
  else
    return rows.map {|r| [r.fulltextable_type, r.fulltextable_id]}
  end
end

.use_advanced_search!Object

Use advanced search mechanism, instead of pure fulltext search.



75
76
77
# File 'lib/fulltext_row.rb', line 75

def self.use_advanced_search!
  @@use_advanced_search = true
end

.use_and_search!Object

Force usage of AND search instead of OR. Works only when advanced search is enabled.



82
83
84
# File 'lib/fulltext_row.rb', line 82

def self.use_and_search!
  @@use_and_search = true
end

.use_phrase_search!Object

Force usage of phrase search instead of OR search. Doesn’t work when advanced search is enabled.



89
90
91
# File 'lib/fulltext_row.rb', line 89

def self.use_phrase_search!
  @@use_phrase_search = true
end