Commit 0669cbb2be3a45b2bb3adcc2843b2656dc24679b
1 parent
9a9aaa40
Exists in
master
and in
29 other branches
ActionItem129: added an action to the search controller to find enterprises by t…
…he products_categories of its products or for distance of a point git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1771 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
4 changed files
with
47 additions
and
0 deletions
Show diff stats
app/controllers/public/search_controller.rb
... | ... | @@ -120,6 +120,14 @@ class SearchController < ApplicationController |
120 | 120 | @tagged = @tag.taggings.map(&:taggable) |
121 | 121 | end |
122 | 122 | |
123 | + def sellers | |
124 | + options = {} | |
125 | + @product_category = ProductCategory.find_by_path(params[:product_category].join('/')) if params[:product_category] | |
126 | + options.merge!({:include => :products, :conditions => ['products.product_category_id = ?', @product_category.id]}) if @product_category | |
127 | + options.merge!({:origin => [params[:lat].to_f, params[:long].to_f], :within => params[:radius] }) if params[:lat] && params[:long] && params[:radius] | |
128 | + @enterprises = Enterprise.find(:all, options) | |
129 | + end | |
130 | + | |
123 | 131 | ####################################################### |
124 | 132 | |
125 | 133 | def popup | ... | ... |
app/models/profile.rb
... | ... | @@ -35,6 +35,8 @@ class Profile < ActiveRecord::Base |
35 | 35 | |
36 | 36 | acts_as_having_settings :field => :data |
37 | 37 | |
38 | + acts_as_mappable :default_units => :kms | |
39 | + | |
38 | 40 | # Valid identifiers must match this format. |
39 | 41 | IDENTIFIER_FORMAT = /^[a-z][a-z0-9~.]*([_-][a-z0-9~.]+)*$/ |
40 | 42 | ... | ... |
test/functional/search_controller_test.rb
... | ... | @@ -748,6 +748,29 @@ class SearchControllerTest < Test::Unit::TestCase |
748 | 748 | assert_not_includes assigns(:results)[:comments], comment4 |
749 | 749 | end |
750 | 750 | |
751 | + should 'find enterprise by product category' do | |
752 | + ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1') | |
753 | + prod_cat = ProductCategory.create!(:name => 'pc-test', :environment => Environment.default) | |
754 | + prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat) | |
755 | + | |
756 | + ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2') | |
757 | + | |
758 | + get :sellers, :product_category => ['pc-test'] | |
759 | + | |
760 | + assert_includes assigns('enterprises'), ent1 | |
761 | + assert_not_includes assigns('enterprises'), ent2 | |
762 | + end | |
763 | + | |
764 | + should 'find enterprise by origin and radius' do | |
765 | + ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :lat => 45.0, :lng => 45.0) | |
766 | + ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :lat => 30.0, :lng => 30.0) | |
767 | + | |
768 | + get :sellers, :lat => 45.0, :long => 45.0, :radius => 10 | |
769 | + | |
770 | + assert_includes assigns('enterprises'), ent1 | |
771 | + assert_not_includes assigns('enterprises'), ent2 | |
772 | + end | |
773 | + | |
751 | 774 | should 'not show term "Category:" before product category' do |
752 | 775 | Profile.delete_all |
753 | 776 | ent = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ... | ... |
test/unit/profile_test.rb
... | ... | @@ -442,6 +442,20 @@ class ProfileTest < Test::Unit::TestCase |
442 | 442 | assert_not_includes list, outside |
443 | 443 | end |
444 | 444 | |
445 | + should 'have latitude and longitude' do | |
446 | + e = Enterprise.create!(:name => 'test1', :identifier => 'test1') | |
447 | + e.lat, e.lng = 45, 45 ; e.save! | |
448 | + | |
449 | + assert_includes Enterprise.find_within(2, :origin => [45, 45]), e | |
450 | + end | |
451 | + | |
452 | + should 'have latitude and longitude and find' do | |
453 | + e = Enterprise.create!(:name => 'test1', :identifier => 'test1') | |
454 | + e.lat, e.lng = 45, 45 ; e.save! | |
455 | + | |
456 | + assert_includes Enterprise.find(:all, :within => 2, :origin => [45, 45]), e | |
457 | + end | |
458 | + | |
445 | 459 | private |
446 | 460 | |
447 | 461 | def assert_invalid_identifier(id) | ... | ... |