Class: Sunspot::Search::FieldFacet

Inherits:
QueryFacet show all
Defined in:
lib/sunspot/search/field_facet.rb

Instance Attribute Summary

Attributes inherited from QueryFacet

#name

Instance Method Summary collapse

Methods inherited from QueryFacet

#add_row

Constructor Details

#initialize(field, search, options) ⇒ FieldFacet

:nodoc:



6
7
8
9
# File 'lib/sunspot/search/field_facet.rb', line 6

def initialize(field, search, options) #:nodoc:
  super(field.name, search, options)
  @field = field
end

Instance Method Details

#populate_instancesObject

:nodoc:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sunspot/search/field_facet.rb', line 28

def populate_instances #:nodoc:
  if reference = @field.reference
    values_hash = rows.inject({}) do |hash, row|
      hash[row.value] = row
      hash
    end
    instances = Adapters::DataAccessor.create(Sunspot::Util.full_const_get(reference)).load_all(
      values_hash.keys
    )
    instances.each do |instance|
      values_hash[Adapters::InstanceAdapter.adapt(instance).id].instance = instance
    end
  end
end

#rowsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sunspot/search/field_facet.rb', line 11

def rows
  @rows ||=
    begin
      rows = super
      has_query_facets = !rows.empty?
      if @search.facet_response['facet_fields']
        if data = @search.facet_response['facet_fields'][@field.indexed_name]
          data.each_slice(2) do |value, count|
            rows << FacetRow.new(@field.cast(value), count, self)
          end
        end
      end
      sort_rows!(rows) if has_query_facets
      rows
    end
end