Commit bc6fe8d0d95ddeecc1b9c93efff71ea176081a44

Authored by MoisesMachado
1 parent 412747e0

ActionItem550: search now is in alphabetical order of name


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2236 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/category_finder.rb
@@ -19,7 +19,7 @@ class CategoryFinder @@ -19,7 +19,7 @@ class CategoryFinder
19 19
20 options = {:page => 1, :per_page => options.delete(:limit)}.merge(options) 20 options = {:page => 1, :per_page => options.delete(:limit)}.merge(options)
21 if query.blank? 21 if query.blank?
22 - asset_class(asset).paginate(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options), date_range)) 22 + asset_class(asset).paginate(:all, options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options), date_range))
23 else 23 else
24 ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} 24 ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)}
25 asset_class(asset).find_by_contents(query, ferret_options, options_for_find(asset_class(asset), options, date_range)) 25 asset_class(asset).find_by_contents(query, ferret_options, options_for_find(asset_class(asset), options, date_range))
app/models/environment_finder.rb
@@ -21,11 +21,11 @@ class EnvironmentFinder @@ -21,11 +21,11 @@ class EnvironmentFinder
21 end 21 end
22 22
23 if query.blank? 23 if query.blank?
24 - options = {:order => "#{asset_table(asset)}.created_at desc, #{asset_table(asset)}.id desc"}.merge(options) 24 + options = {:order => "#{asset_table(asset)}.name"}.merge(options)
25 if product_category && asset == :products 25 if product_category && asset == :products
26 @environment.send(asset).send(finder_method, :all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) 26 @environment.send(asset).send(finder_method, :all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id]))
27 elsif product_category && asset == :enterprises 27 elsif product_category && asset == :enterprises
28 - @environment.send(asset).send(finder_method, :all, options.merge(:order => 'profiles.created_at desc, profiles.id desc', :include => 'products', :joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) 28 + @environment.send(asset).send(finder_method, :all, options.merge(:order => 'profiles.name', :include => 'products', :joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id]))
29 else 29 else
30 if (asset == :events) && date_range 30 if (asset == :events) && date_range
31 @environment.send(asset).send(finder_method, :all, options.merge(:conditions => { :start_date => date_range})) 31 @environment.send(asset).send(finder_method, :all, options.merge(:conditions => { :start_date => date_range}))
test/functional/search_controller_test.rb
@@ -233,7 +233,7 @@ class SearchControllerTest < Test::Unit::TestCase @@ -233,7 +233,7 @@ class SearchControllerTest < Test::Unit::TestCase
233 233
234 get :assets, :asset => 'communities', :category_path => [ 'my-category' ] 234 get :assets, :asset => 'communities', :category_path => [ 'my-category' ]
235 235
236 - assert_equal [c3, c1], assigns(:results)[:communities] 236 + assert_equivalent [c3, c1], assigns(:results)[:communities]
237 end 237 end
238 238
239 should 'find products' do 239 should 'find products' do
@@ -290,7 +290,7 @@ class SearchControllerTest < Test::Unit::TestCase @@ -290,7 +290,7 @@ class SearchControllerTest < Test::Unit::TestCase
290 290
291 get :assets, :asset => 'enterprises', :page => '2' 291 get :assets, :asset => 'enterprises', :page => '2'
292 292
293 - assert_equal [ent1], assigns(:results)[:enterprises] # older on page 2 293 + assert_equal 1, assigns(:results)[:enterprises].size
294 end 294 end
295 295
296 should 'display search results' do 296 should 'display search results' do
test/unit/category_finder_test.rb
@@ -124,17 +124,21 @@ class CategoryFinderTest < ActiveSupport::TestCase @@ -124,17 +124,21 @@ class CategoryFinderTest < ActiveSupport::TestCase
124 should 'not list more enterprises than limit' do 124 should 'not list more enterprises than limit' do
125 ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) 125 ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id])
126 ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2', :category_ids => [@category.id]) 126 ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2', :category_ids => [@category.id])
127 - recent = @finder.recent('enterprises', 1)  
128 - assert_includes recent, ent2  
129 - assert_not_includes recent, ent1 127 + result = @finder.recent('enterprises', 1)
  128 +
  129 + assert_equal 1, result.size
130 end 130 end
131 131
132 should 'paginate the list of more enterprises than limit' do 132 should 'paginate the list of more enterprises than limit' do
133 ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) 133 ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id])
134 ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2', :category_ids => [@category.id]) 134 ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2', :category_ids => [@category.id])
135 135
136 - assert_equal [ent2], @finder.find('enterprises', nil, :per_page => 1, :page => 1)  
137 - assert_equal [ent1], @finder.find('enterprises', nil, :per_page => 1, :page => 2) 136 + page_1 = @finder.find('enterprises', nil, :per_page => 1, :page => 1)
  137 + page_2 = @finder.find('enterprises', nil, :per_page => 1, :page => 2)
  138 +
  139 + assert_equal 1, page_1.size
  140 + assert_equal 1, page_2.size
  141 + assert_equivalent [ent1, ent2], page_1 + page_2
138 end 142 end
139 143
140 should 'paginate the list of more enterprises than limit with query' do 144 should 'paginate the list of more enterprises than limit with query' do
@@ -152,9 +156,9 @@ class CategoryFinderTest < ActiveSupport::TestCase @@ -152,9 +156,9 @@ class CategoryFinderTest < ActiveSupport::TestCase
152 should 'not list more people than limit' do 156 should 'not list more people than limit' do
153 p1 = create_user('test1').person; p1.add_category(@category) 157 p1 = create_user('test1').person; p1.add_category(@category)
154 p2 = create_user('test2').person; p2.add_category(@category) 158 p2 = create_user('test2').person; p2.add_category(@category)
155 - recent = @finder.recent('people', 1)  
156 - assert_includes recent, p2  
157 - assert_not_includes recent, p1 159 + result = @finder.recent('people', 1)
  160 +
  161 + assert_equal 1, result.size
158 end 162 end
159 163
160 should 'list recent articles' do 164 should 'list recent articles' do
@@ -164,8 +168,8 @@ class CategoryFinderTest < ActiveSupport::TestCase @@ -164,8 +168,8 @@ class CategoryFinderTest < ActiveSupport::TestCase
164 art2 = person.articles.build(:name => 'another article to be found'); art2.add_category(@category); art2.save! 168 art2 = person.articles.build(:name => 'another article to be found'); art2.add_category(@category); art2.save!
165 169
166 result = @finder.recent('articles', 1) 170 result = @finder.recent('articles', 1)
167 - assert_includes result, art2  
168 - assert_not_includes result, art1 171 +
  172 + assert_equal 1, result.size
169 end 173 end
170 174
171 should 'not return the same result twice' do 175 should 'not return the same result twice' do
@@ -431,5 +435,16 @@ class CategoryFinderTest < ActiveSupport::TestCase @@ -431,5 +435,16 @@ class CategoryFinderTest < ActiveSupport::TestCase
431 assert_equal 2, counts[pc2.id] 435 assert_equal 2, counts[pc2.id]
432 assert_nil counts[pc3.id] 436 assert_nil counts[pc3.id]
433 end 437 end
  438 +
  439 + should 'find enterprises in alphabetical order of name' do
  440 + ent1 = Enterprise.create!(:name => 'test enterprise B', :identifier => 'test_ent_b', :category_ids => [@category.id])
  441 + ent2 = Enterprise.create!(:name => 'test enterprise A', :identifier => 'test_ent_a', :category_ids => [@category.id])
  442 + ent3 = Enterprise.create!(:name => 'test enterprise C', :identifier => 'test_ent_c', :category_ids => [@category.id])
  443 +
  444 + ents = @finder.find(:enterprises, nil)
  445 +
  446 + assert ents.index(ent2) < ents.index(ent1), "expected #{ents.index(ent2)} be smaller than #{ents.index(ent1)}"
  447 + assert ents.index(ent1) < ents.index(ent3), "expected #{ents.index(ent1)} be smaller than #{ents.index(ent3)}"
  448 + end
434 449
435 end 450 end
test/unit/environment_finder_test.rb
@@ -141,14 +141,17 @@ class EnvironmentFinderTest &lt; ActiveSupport::TestCase @@ -141,14 +141,17 @@ class EnvironmentFinderTest &lt; ActiveSupport::TestCase
141 assert_not_includes prods, prod4 141 assert_not_includes prods, prod4
142 end 142 end
143 143
144 - should 'find in order of creation' do 144 + should 'find enterprises in alphabetical order of name' do
145 finder = EnvironmentFinder.new(Environment.default) 145 finder = EnvironmentFinder.new(Environment.default)
146 - ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1')  
147 - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') 146 +
  147 + ent1 = Enterprise.create!(:name => 'test enterprise B', :identifier => 'test_ent_b')
  148 + ent2 = Enterprise.create!(:name => 'test enterprise A', :identifier => 'test_ent_a')
  149 + ent3 = Enterprise.create!(:name => 'test enterprise C', :identifier => 'test_ent_c')
148 150
149 ents = finder.find(:enterprises, nil) 151 ents = finder.find(:enterprises, nil)
150 152
151 assert ents.index(ent2) < ents.index(ent1), "expected #{ents.index(ent2)} be smaller than #{ents.index(ent1)}" 153 assert ents.index(ent2) < ents.index(ent1), "expected #{ents.index(ent2)} be smaller than #{ents.index(ent1)}"
  154 + assert ents.index(ent1) < ents.index(ent3), "expected #{ents.index(ent1)} be smaller than #{ents.index(ent3)}"
152 end 155 end
153 156
154 should 'find enterprises by its products categories' do 157 should 'find enterprises by its products categories' do