Commit 6b85a7bceeb88aba6d1cc93fea8f592dbea63674
1 parent
000b5f53
Exists in
master
and in
29 other branches
Change acts_as_faceted to lookup for names
Showing
1 changed file
with
10 additions
and
25 deletions
Show diff stats
lib/acts_as_faceted.rb
| ... | ... | @@ -4,18 +4,13 @@ module ActsAsFaceted |
| 4 | 4 | end |
| 5 | 5 | |
| 6 | 6 | module ActsMethods |
| 7 | - # === fields: | |
| 8 | - # A hash of id fields (may be an attribute or a method). | |
| 9 | - # === order: | |
| 10 | - # An array | |
| 11 | 7 | # Example: |
| 12 | 8 | # |
| 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 | |
| 9 | + # acts_as_faceted :fields => { | |
| 10 | + # :f_category => {:label => _('Related products')}, | |
| 11 | + # :f_region => {:label => _('Region')}, | |
| 12 | + # :f_qualifier => {:label => _('Qualifiers')}}, | |
| 13 | + # :order => [:f_category, :f_region, :f_qualifier] | |
| 19 | 14 | def acts_as_faceted(options) |
| 20 | 15 | extend ClassMethods |
| 21 | 16 | |
| ... | ... | @@ -41,25 +36,15 @@ module ActsAsFaceted |
| 41 | 36 | end |
| 42 | 37 | end |
| 43 | 38 | |
| 44 | - def each_facet_obj(solr_facet, id_hash, options = {}) | |
| 39 | + def each_facet_name(solr_facet, data, options = {}) | |
| 45 | 40 | facet = facets[solr_facet_fields[solr_facet]] |
| 46 | - klass = facet[:class] | |
| 47 | 41 | |
| 48 | 42 | 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] } | |
| 43 | + result = data.sort{ |a,b| -1*(a[0] <=> b[0]) } | |
| 44 | + result.each { |name, count| yield [name, count] } | |
| 57 | 45 | 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 | |
| 46 | + result = options[:sort] == :count ? data.sort{ |a,b| -1*(a[1] <=> b[1]) } : data | |
| 47 | + result.each { |name, count| yield [name, count] } | |
| 63 | 48 | end |
| 64 | 49 | end |
| 65 | 50 | end | ... | ... |