summaryrefslogtreecommitdiff
path: root/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb')
-rw-r--r--vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb60
1 files changed, 0 insertions, 60 deletions
diff --git a/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb b/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb
deleted file mode 100644
index 1ad9d1a..0000000
--- a/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb
+++ /dev/null
@@ -1,60 +0,0 @@
1module ThinkingSphinx
2 class FacetCollection < Hash
3 attr_accessor :arguments
4
5 def initialize(arguments)
6 @arguments = arguments.clone
7 @attribute_values = {}
8 @facets = []
9 end
10
11 def add_from_results(facet, results)
12 facet = facet_from_object(results.first, facet) if facet.is_a?(String)
13
14 self[facet.name] ||= {}
15 @attribute_values[facet.name] ||= {}
16 @facets << facet
17
18 results.each_with_groupby_and_count { |result, group, count|
19 facet_value = facet.value(result, group)
20
21 self[facet.name][facet_value] ||= 0
22 self[facet.name][facet_value] += count
23 @attribute_values[facet.name][facet_value] = group
24 }
25 end
26
27 def for(hash = {})
28 arguments = @arguments.clone
29 options = arguments.extract_options!
30 options[:with] ||= {}
31
32 hash.each do |key, value|
33 attrib = facet_for_key(key).attribute_name
34 options[:with][attrib] = underlying_value key, value
35 end
36
37 arguments << options
38 ThinkingSphinx::Search.search *arguments
39 end
40
41 private
42
43 def underlying_value(key, value)
44 case value
45 when Array
46 value.collect { |item| underlying_value(key, item) }
47 else
48 @attribute_values[key][value]
49 end
50 end
51
52 def facet_for_key(key)
53 @facets.detect { |facet| facet.name == key }
54 end
55
56 def facet_from_object(object, name)
57 object.sphinx_facets.detect { |facet| facet.attribute_name == name }
58 end
59 end
60end \ No newline at end of file