Commit 245398e94e1b58a2b0f0789ae4fc5dc174f0686a
1 parent
ea9448c0
Exists in
master
and in
29 other branches
Add acts_as_faceted lib for declaring facets
Showing
2 changed files
with
72 additions
and
0 deletions
Show diff stats
config/initializers/dependencies.rb
... | ... | @@ -0,0 +1,71 @@ |
1 | +module ActsAsFaceted | |
2 | + | |
3 | + module ClassMethods | |
4 | + end | |
5 | + | |
6 | + module ActsMethods | |
7 | + # === fields: | |
8 | + # A hash of id fields (may be an attribute or a method). | |
9 | + # === order: | |
10 | + # An array | |
11 | + # Example: | |
12 | + # | |
13 | + # acts_as_faceted :fields => {:f_category_id => {:class => ProductCategory, :display_field => :name, :label => _('Related products')}, | |
14 | + # :f_region_id => {:class => Region, :display_field => :name, :label => _('Region')}, | |
15 | + # :f_qualifier_id => {:class => Qualifier, :display_field => :name, :label => _('Qualifiers')}, | |
16 | + # :f_certifier_id => {:class => Certifier, :display_field => :name, :label => _('Certifiers')}}, | |
17 | + # :order => [:f_category_id, :f_region_id, :f_qualifier_id, :f_certifier_id] | |
18 | + # end | |
19 | + def acts_as_faceted(options) | |
20 | + extend ClassMethods | |
21 | + | |
22 | + cattr_accessor :facets | |
23 | + cattr_accessor :facets_order | |
24 | + cattr_accessor :solr_facet_fields | |
25 | + cattr_accessor :to_solr_facet_fields | |
26 | + | |
27 | + self.facets = options[:fields] | |
28 | + self.facets_order = options[:order] || facets | |
29 | + | |
30 | + # A hash to retrieve the field key for the solr facet string returned | |
31 | + # "field_name_facet" => :field_name | |
32 | + self.solr_facet_fields = Hash[facets.keys.map{|f| f.to_s+'_facet'}.zip(facets.keys)] | |
33 | + # :field_name => "field_name_facet" | |
34 | + self.to_solr_facet_fields = Hash[facets.keys.zip(facets.keys.map{|f| f.to_s+'_facet'})] | |
35 | + | |
36 | + def each_facet | |
37 | + if facets_order | |
38 | + facets_order.each_with_index { |f, i| yield [f, i] } | |
39 | + else | |
40 | + facets.each_with_index { |f, i| yield [f. i] } | |
41 | + end | |
42 | + end | |
43 | + | |
44 | + def each_facet_obj(solr_facet, id_hash, options = {}) | |
45 | + facet = facets[solr_facet_fields[solr_facet]] | |
46 | + klass = facet[:class] | |
47 | + | |
48 | + if options[:sort] == :alphabetically | |
49 | + display_field = facet[:display_field] | |
50 | + result = [] | |
51 | + id_hash.each do |id, count| | |
52 | + obj = klass.find_by_id(id) | |
53 | + result << [obj, count] if obj | |
54 | + end | |
55 | + result = result.sort { |a,b| a[0].send(display_field) <=> b[0].send(display_field) } | |
56 | + result.each { |obj, count| yield [obj, count] } | |
57 | + else | |
58 | + result = options[:sort] == :count ? id_hash.sort{ |a,b| -1*(a[1] <=> b[1]) } : id_hash | |
59 | + result.each do |id, count| | |
60 | + obj = klass.find_by_id(id) | |
61 | + yield [obj, count] if obj | |
62 | + end | |
63 | + end | |
64 | + end | |
65 | + end | |
66 | + end | |
67 | + | |
68 | +end | |
69 | + | |
70 | +ActiveRecord::Base.extend ActsAsFaceted::ActsMethods | |
71 | + | ... | ... |