Class: IndexTanked::ActiveRecordDefaults::SearchResult

Inherits:
SearchResult
  • Object
show all
Defined in:
lib/index-tanked/active_record_defaults/search_result.rb

Instance Attribute Summary collapse

Attributes inherited from SearchResult

#page, #per_page

Instance Method Summary collapse

Methods inherited from SearchResult

#facets, #matches, #raw_result, #results, #search_time

Constructor Details

#initialize(query, index, model, options = {}) ⇒ SearchResult

Returns a new instance of SearchResult.



7
8
9
10
# File 'lib/index-tanked/active_record_defaults/search_result.rb', line 7

def initialize(query, index, model, options={})
  super(query, index, options)
  @model = model
end

Instance Attribute Details

#missing_idsObject (readonly)

Returns the value of attribute missing_ids.



5
6
7
# File 'lib/index-tanked/active_record_defaults/search_result.rb', line 5

def missing_ids
  @missing_ids
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/index-tanked/active_record_defaults/search_result.rb', line 5

def model
  @model
end

Instance Method Details

#idsObject



12
13
14
# File 'lib/index-tanked/active_record_defaults/search_result.rb', line 12

def ids
  results.map { |result| id = result['docid'].match(/^#{@model.name}:(\d+)$/); id && id[1].to_i }.compact.uniq
end

#inspectObject



50
51
52
# File 'lib/index-tanked/active_record_defaults/search_result.rb', line 50

def inspect
  "<IndexTanked::ActiveRecordDefaults::SearchResult:#{object_id}, Query:'#{@query}'>"
end

#paginate(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/index-tanked/active_record_defaults/search_result.rb', line 34

def paginate(options={})
  original_options = options.clone

  @options[:page]     = options.delete(:page) || 1
  @options[:per_page] = options.delete(:per_page) || 15

  begin
    WillPaginate::Collection.create(@options[:page], @options[:per_page]) do |pager|
      pager.replace(records(options))
      pager.total_entries = results.total_entries unless pager.total_entries
    end
  rescue IndexTankedError
    @model.paginate(original_options)
  end
end

#records(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/index-tanked/active_record_defaults/search_result.rb', line 16

def records(options={})
  base = @model
  if defined?(Squirrel) && base.respond_to?(:scoped_without_squirrel)
    base = base.scoped_without_squirrel(:conditions => {:id => ids})
  else
    base = base.scoped(:conditions => {:id => ids})
  end
  records_found = base.all(options)
  @missing_ids = ids - records_found.map(&:id)
  begin
    if Configuration.missing_activerecord_ids_handler
      Configuration.missing_activerecord_ids_handler.call(@model.name, @missing_ids)
    end
  ensure
    return records_found
  end
end