Class: SolrCollection

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

Overview

Proxy that delegates all calls to contained subject except solr related methods in MAPPED_FIELDS

Constant Summary collapse

SOLR_FIELDS =
[:facets, :spellcheck]

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ SolrCollection

Returns a new instance of SolrCollection.



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
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/solr_collection.rb', line 8

def initialize(collection, options={})
  options = options.dup

  # get fields from solr result-set or set them to nil
  @solr_data = {}
  SOLR_FIELDS.each do |field|
    @solr_data[field] = if options[field]
      options[field]
    elsif collection.respond_to?(field)
      collection.send(field)
    else
      nil
    end
  end

  # copy or generate pagination information
  options[:page] ||= (collection.respond_to?(:current_page) ? collection.current_page : nil)
  options[:per_page] ||= (collection.respond_to?(:per_page) ? collection.per_page : nil)
  options[:total_entries] ||= if collection.respond_to?(:total)
    collection.total
  elsif collection.respond_to?(:total_entries)
    collection.total_entries
  else
    collection.size
  end
  options = fill_page_and_per_page(options)

  # build paginate-able object
  @subject = WillPaginate::Collection.new(
    options[:page],
    options[:per_page],
    options[:total_entries]
  )

  # solr result-set has results in an array called results
  results = (collection.respond_to?(:results) ? collection.results : collection)
  @subject.replace(results.to_ary)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



80
81
82
83
84
85
86
87
# File 'lib/solr_collection.rb', line 80

def method_missing(method, *args, &block)
  method = method.to_sym
  if @solr_data.key? method
    @solr_data[method] or (@subject.send(method, *args, &block) rescue nil)
  else
    @subject.send(method, *args, &block)
  end
end

Instance Method Details

#facet_field(field) ⇒ Object



52
53
54
55
56
# File 'lib/solr_collection.rb', line 52

def facet_field(field)
  if has_facet_fields? and value = facets['facet_fields'][field]
    value.empty? ? nil : value
  end
end

#has_facet_fields?Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/solr_collection.rb', line 47

def has_facet_fields?
  # can be a array, when empty reults where returned
  !!(facets and facets['facet_fields'] and not facets['facet_fields'].empty?)
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/solr_collection.rb', line 58

def respond_to?(method)
  @solr_data.key?(method) or @subject.respond_to?(method) or super
end

#to_aObject



62
63
64
# File 'lib/solr_collection.rb', line 62

def to_a
  @subject.to_a
end