Commit 9d41da9620ad45eaa1c89c5c54ffaae28e922d56
1 parent
c57ced6a
Exists in
master
and in
29 other branches
ActsAsFaceted fixes and tests
Showing
2 changed files
with
122 additions
and
35 deletions
Show diff stats
lib/acts_as_faceted.rb
@@ -33,7 +33,7 @@ module ActsAsFaceted | @@ -33,7 +33,7 @@ module ActsAsFaceted | ||
33 | cattr_accessor :facet_category_query | 33 | cattr_accessor :facet_category_query |
34 | 34 | ||
35 | self.facets = options[:fields] | 35 | self.facets = options[:fields] |
36 | - self.facets_order = options[:order] || facets | 36 | + self.facets_order = options[:order] || self.facets.keys |
37 | self.facets_results_containers = {:fields => 'facet_fields', :queries => 'facet_queries', :ranges => 'facet_ranges'} | 37 | self.facets_results_containers = {:fields => 'facet_fields', :queries => 'facet_queries', :ranges => 'facet_ranges'} |
38 | self.facets_option_for_solr = Hash[facets.select{ |id,data| ! data.has_key?(:queries) }].keys | 38 | self.facets_option_for_solr = Hash[facets.select{ |id,data| ! data.has_key?(:queries) }].keys |
39 | self.facets_fields_for_solr = facets.map{ |id,data| {id => data[:type] || :facet} } | 39 | self.facets_fields_for_solr = facets.map{ |id,data| {id => data[:type] || :facet} } |
@@ -49,8 +49,7 @@ module ActsAsFaceted | @@ -49,8 +49,7 @@ module ActsAsFaceted | ||
49 | end | 49 | end |
50 | 50 | ||
51 | def map_facets_for(environment) | 51 | def map_facets_for(environment) |
52 | - list = facets_order ? facets_order : facets.keys | ||
53 | - list.map do |id| | 52 | + facets_order.map do |id| |
54 | facet = facet_by_id(id) | 53 | facet = facet_by_id(id) |
55 | next if facet[:type_if] and !facet[:type_if].call(self.new) | 54 | next if facet[:type_if] and !facet[:type_if].call(self.new) |
56 | 55 | ||
@@ -65,6 +64,7 @@ module ActsAsFaceted | @@ -65,6 +64,7 @@ module ActsAsFaceted | ||
65 | end | 64 | end |
66 | 65 | ||
67 | def map_facet_results(facet, facet_params, facets_data, unfiltered_facets_data = {}, options = {}) | 66 | def map_facet_results(facet, facet_params, facets_data, unfiltered_facets_data = {}, options = {}) |
67 | + raise 'Use map_facets_for before this method' if facet[:solr_field].nil? | ||
68 | facets_data ||= {} | 68 | facets_data ||= {} |
69 | solr_facet = to_solr_fields_names[facet[:solr_field]] | 69 | solr_facet = to_solr_fields_names[facet[:solr_field]] |
70 | 70 |
test/unit/acts_as_faceted_test.rb
@@ -15,60 +15,147 @@ class TestModel < ActiveRecord::Base | @@ -15,60 +15,147 @@ class TestModel < ActiveRecord::Base | ||
15 | } | 15 | } |
16 | h[klass] | 16 | h[klass] |
17 | end | 17 | end |
18 | - def self.f_profile_type_proc(klass) | ||
19 | - h = { | ||
20 | - 'Enterprise' => "Enterprise", | ||
21 | - 'Community' => "Community", | ||
22 | - 'Person' => "Person", | ||
23 | - 'BscPlugin::Bsc' => "BSC" | ||
24 | - } | ||
25 | - h[klass] | ||
26 | - end | ||
27 | acts_as_faceted :fields => { | 18 | acts_as_faceted :fields => { |
28 | - :f_type => {:label => 'Type', :proc => proc{|klass| f_type_proc(klass)}}, | ||
29 | - :f_published_at => {:type => :date, :label => 'Published date', :queries => {'[* TO NOW-1YEARS/DAY]' => "Older than one year", '[NOW-1YEARS TO NOW/DAY]' => "Last year"}}, | ||
30 | - :f_profile_type => {:label => 'Author', :proc => proc{|klass| f_profile_type_proc(klass)}}, | ||
31 | - :f_category => {:label => 'Categories'}}, | ||
32 | - :order => [:f_type, :f_published_at, :f_profile_type, :f_category] | 19 | + :f_type => {:label => 'Type', :proc => proc{|klass| f_type_proc(klass)}}, |
20 | + :f_published_at => {:type => :date, :label => 'Published date', :queries => | ||
21 | + {'[* TO NOW-1YEARS/DAY]' => "Older than one year", '[NOW-1YEARS TO NOW/DAY]' => "Last year"}}, | ||
22 | + }, :order => [:f_type, :f_published_at] | ||
33 | end | 23 | end |
34 | 24 | ||
35 | class ActsAsFacetedTest < ActiveSupport::TestCase | 25 | class ActsAsFacetedTest < ActiveSupport::TestCase |
36 | def setup | 26 | def setup |
37 | @facets = { | 27 | @facets = { |
38 | "facet_fields"=> { | 28 | "facet_fields"=> { |
39 | - "f_profile_type_facet"=>{"Person"=>29}, | ||
40 | "f_type_facet"=>{"TextArticle"=>15, "Blog"=>3, "Folder"=>3, "Forum"=>1, "UploadedFile"=>6, "Gallery"=>1}, | 29 | "f_type_facet"=>{"TextArticle"=>15, "Blog"=>3, "Folder"=>3, "Forum"=>1, "UploadedFile"=>6, "Gallery"=>1}, |
41 | - "f_category_facet"=>{}}, | ||
42 | - "facet_ranges"=>{}, "facet_dates"=>{}, | 30 | + }, "facet_ranges"=>{}, "facet_dates"=>{}, |
43 | "facet_queries"=>{"f_published_at_d:[* TO NOW-1YEARS/DAY]"=>10, "f_published_at_d:[NOW-1YEARS TO NOW/DAY]"=>19} | 31 | "facet_queries"=>{"f_published_at_d:[* TO NOW-1YEARS/DAY]"=>10, "f_published_at_d:[NOW-1YEARS TO NOW/DAY]"=>19} |
44 | } | 32 | } |
33 | + #any facet selected | ||
34 | + @facet_params = {} | ||
35 | + @all_facets = @facets | ||
36 | + end | ||
37 | + | ||
38 | + should 'get defined facets' do | ||
39 | + assert TestModel.facets.has_key? :f_type | ||
40 | + assert TestModel.facets.has_key? :f_published_at | ||
41 | + end | ||
42 | + | ||
43 | + should 'get facets by id' do | ||
44 | + facet = TestModel.facet_by_id :f_type | ||
45 | + assert_equal :f_type, facet[:id] | ||
46 | + assert_equal TestModel.facets[:f_type][:label], facet[:label] | ||
47 | + assert_equal TestModel.facets[:f_type][:proc], facet[:proc] | ||
48 | + end | ||
49 | + | ||
50 | + should 'convert facets to solr field names' do | ||
51 | + solr_names = TestModel.solr_fields_names | ||
52 | + assert solr_names.include?("f_type_facet") | ||
53 | + assert solr_names.include?("f_published_at_d") | ||
54 | + | ||
55 | + solr_names = TestModel.to_solr_fields_names | ||
56 | + | ||
57 | + assert_equal solr_names[:f_type], 'f_type_facet' | ||
58 | + assert_equal solr_names[:f_published_at], 'f_published_at_d' | ||
59 | + end | ||
60 | + | ||
61 | + should 'return facets containers' do | ||
62 | + containers = TestModel.facets_results_containers | ||
63 | + | ||
64 | + assert_equal containers.count, 3 | ||
65 | + assert_equal containers[:fields], 'facet_fields' | ||
66 | + assert_equal containers[:queries], 'facet_queries' | ||
67 | + assert_equal containers[:ranges], 'facet_ranges' | ||
68 | + end | ||
69 | + | ||
70 | + should 'show facets option for solr' do | ||
71 | + assert TestModel.facets_option_for_solr.include?(:f_type) | ||
72 | + assert !TestModel.facets_option_for_solr.include?(:f_published_at) | ||
73 | + end | ||
74 | + | ||
75 | + should 'show facets fields for solr' do | ||
76 | + TestModel.facets_fields_for_solr.each do |facet| | ||
77 | + assert_equal facet[:f_type], :facet if facet[:f_type] | ||
78 | + assert_equal facet[:f_published_at], :date if facet[:f_published_at] | ||
79 | + end | ||
45 | end | 80 | end |
46 | 81 | ||
47 | should 'iterate over each result' do | 82 | should 'iterate over each result' do |
83 | + facets = TestModel.map_facets_for(Environment.default) | ||
84 | + assert facets.count, 2 | ||
85 | + | ||
86 | + f = facets.select{ |f| f[:id] == 'f_type' }.first | ||
87 | + r = TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | ||
88 | + assert_equivalent [["TextArticle", 'Text', 15], ["Blog", "Blog", 3], ["Folder", "Folder", 3], ["Forum", "Forum", 1], ["UploadedFile", "Uploaded File", 6], ["Gallery", "Gallery", 1]], r | ||
89 | + | ||
90 | + f = facets.select{ |f| f[:id] == 'f_published_at' }.first | ||
91 | + r = TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | ||
92 | + assert_equivalent [["[* TO NOW-1YEARS/DAY]", "Older than one year", 10], ["[NOW-1YEARS TO NOW/DAY]", "Last year", 19]], r | ||
93 | + end | ||
94 | + | ||
95 | + should 'return facet hash in map_facets_for' do | ||
96 | + r = TestModel.map_facets_for(Environment.default) | ||
97 | + assert r.count, 2 | ||
98 | + | ||
99 | + f_type = r.select{ |f| f[:id] == 'f_type' }.first | ||
100 | + assert_equal f_type[:solr_field], :f_type | ||
101 | + assert_equal f_type[:label], "Type" | ||
102 | + | ||
103 | + f_published = r.select{ |f| f[:id] == 'f_published_at' }.first | ||
104 | + assert_equal :f_published_at, f_published[:solr_field] | ||
105 | + assert_equal :date, f_published[:type] | ||
106 | + assert_equal "Published date", f_published[:label] | ||
107 | + hash = {"[NOW-1YEARS TO NOW/DAY]"=>"Last year", "[* TO NOW-1YEARS/DAY]"=>"Older than one year"} | ||
108 | + assert_equal hash, f_published[:queries] | ||
109 | + end | ||
110 | + | ||
111 | + should 'get label of a facet' do | ||
48 | f = TestModel.facet_by_id(:f_type) | 112 | f = TestModel.facet_by_id(:f_type) |
49 | - r = [] | ||
50 | - TestModel.each_facet_result(f, @facets, {}) { |i| r.push i } | ||
51 | - assert_equal r, [["TextArticle", 'Text', 15], ["Blog", "Blog", 3], ["Folder", "Folder", 3], ["Forum", "Forum", 1], ["UploadedFile", "Uploaded File", 6], ["Gallery", "Gallery", 1]] | 113 | + assert_equal f[:label], 'Type' |
114 | + end | ||
52 | 115 | ||
116 | + should "get facets' queries" do | ||
53 | f = TestModel.facet_by_id(:f_published_at) | 117 | f = TestModel.facet_by_id(:f_published_at) |
54 | - r = [] | ||
55 | - TestModel.each_facet_result(f, @facets, {}) { |i| r.push i } | ||
56 | - assert_equal r, [ ["[* TO NOW-1YEARS/DAY]", "Older than one year", 10], ["[NOW-1YEARS TO NOW/DAY]", "Last year", 19] ] | 118 | + assert_equal f[:queries]['[* TO NOW-1YEARS/DAY]'], 'Older than one year' |
119 | + end | ||
120 | + | ||
121 | + should 'not map_facet_results without map_facets_for' do | ||
122 | + assert_raise RuntimeError do | ||
123 | + f = TestModel.facet_by_id(:f_published_at) | ||
124 | + TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | ||
125 | + end | ||
126 | + end | ||
127 | + | ||
128 | + should 'show correct ordering' do | ||
129 | + assert_equal TestModel.facets_order, [:f_type, :f_published_at] | ||
57 | end | 130 | end |
58 | 131 | ||
59 | - should 'query label of a facet' do | ||
60 | - l = TestModel.facet_by_id(:f_type) | ||
61 | - assert_equal l[:label], 'Type' | ||
62 | - l = TestModel.facet_by_id(:f_published_at) | ||
63 | - assert_equal l[:queries]['[* TO NOW-1YEARS/DAY]'], 'Older than one year' | 132 | + should 'return facet options hash in acts_as_solr format' do |
133 | + options = TestModel.facets_find_options()[:facets] | ||
134 | + assert_equal [:f_type], options[:fields] | ||
135 | + assert_equal ["f_published_at:[NOW-1YEARS TO NOW/DAY]", "f_published_at:[* TO NOW-1YEARS/DAY]"], options[:query] | ||
64 | end | 136 | end |
65 | 137 | ||
66 | should 'return browse options hash in acts_as_solr format' do | 138 | should 'return browse options hash in acts_as_solr format' do |
67 | - o = TestModel.facets_find_options()[:facets] | ||
68 | - assert_equal o[:browse], [] | 139 | + options = TestModel.facets_find_options()[:facets] |
140 | + assert_equal options[:browse], [] | ||
141 | + | ||
142 | + options = TestModel.facets_find_options({'f_published_at' => '[* TO NOW-1YEARS/DAY]'})[:facets] | ||
143 | + assert_equal options[:browse], ['f_published_at:[* TO NOW-1YEARS/DAY]'] | ||
144 | + end | ||
69 | 145 | ||
70 | - o = TestModel.facets_find_options({'f_profile_type' => 'Person', 'f_published_at' => '[* TO NOW-1YEARS/DAY]'})[:facets] | ||
71 | - assert_equal o[:browse], ['f_profile_type:"Person"', 'f_published_at:[* TO NOW-1YEARS/DAY]'] | 146 | + should 'sort facet results alphabetically' do |
147 | + facets = TestModel.map_facets_for(Environment.default) | ||
148 | + facet = facets.select{ |f| f[:id] == 'f_type' }.first | ||
149 | + facet_data = TestModel.map_facet_results facet, @facet_params, @facets, @all_facets, {} | ||
150 | + sorted = TestModel.facet_result_sort(facet, facet_data, :alphabetically) | ||
151 | + assert_equal [["Blog", "Blog", 3], ["Folder", "Folder", 3], ["Forum", "Forum", 1], ["Gallery", "Gallery", 1], ["TextArticle", 'Text', 15], ["UploadedFile", "Uploaded File", 6]], sorted | ||
72 | end | 152 | end |
73 | 153 | ||
154 | + should 'sort facet results by count' do | ||
155 | + facets = TestModel.map_facets_for(Environment.default) | ||
156 | + facet = facets.select{ |f| f[:id] == 'f_type' }.first | ||
157 | + facet_data = TestModel.map_facet_results facet, @facet_params, @facets, @all_facets, {} | ||
158 | + sorted = TestModel.facet_result_sort(facet, facet_data, :count) | ||
159 | + assert_equal [["TextArticle", 'Text', 15], ["UploadedFile", "Uploaded File", 6], ["Blog", "Blog", 3], ["Folder", "Folder", 3], ["Forum", "Forum", 1], ["Gallery", "Gallery", 1]], sorted | ||
160 | + end | ||
74 | end | 161 | end |