From 0669cbb2be3a45b2bb3adcc2843b2656dc24679b Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Fri, 2 May 2008 21:27:07 +0000 Subject: [PATCH] ActionItem129: added an action to the search controller to find enterprises by the products_categories of its products or for distance of a point --- app/controllers/public/search_controller.rb | 8 ++++++++ app/models/profile.rb | 2 ++ test/functional/search_controller_test.rb | 23 +++++++++++++++++++++++ test/unit/profile_test.rb | 14 ++++++++++++++ 4 files changed, 47 insertions(+), 0 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index e01b64e..6b54606 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -120,6 +120,14 @@ class SearchController < ApplicationController @tagged = @tag.taggings.map(&:taggable) end + def sellers + options = {} + @product_category = ProductCategory.find_by_path(params[:product_category].join('/')) if params[:product_category] + options.merge!({:include => :products, :conditions => ['products.product_category_id = ?', @product_category.id]}) if @product_category + options.merge!({:origin => [params[:lat].to_f, params[:long].to_f], :within => params[:radius] }) if params[:lat] && params[:long] && params[:radius] + @enterprises = Enterprise.find(:all, options) + end + ####################################################### def popup diff --git a/app/models/profile.rb b/app/models/profile.rb index ad00ee3..5ee56bf 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -35,6 +35,8 @@ class Profile < ActiveRecord::Base acts_as_having_settings :field => :data + acts_as_mappable :default_units => :kms + # Valid identifiers must match this format. IDENTIFIER_FORMAT = /^[a-z][a-z0-9~.]*([_-][a-z0-9~.]+)*$/ diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 6b8a5a9..d168eeb 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -748,6 +748,29 @@ class SearchControllerTest < Test::Unit::TestCase assert_not_includes assigns(:results)[:comments], comment4 end + should 'find enterprise by product category' do + ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1') + prod_cat = ProductCategory.create!(:name => 'pc-test', :environment => Environment.default) + prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat) + + ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2') + + get :sellers, :product_category => ['pc-test'] + + assert_includes assigns('enterprises'), ent1 + assert_not_includes assigns('enterprises'), ent2 + end + + should 'find enterprise by origin and radius' do + ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :lat => 45.0, :lng => 45.0) + ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :lat => 30.0, :lng => 30.0) + + get :sellers, :lat => 45.0, :long => 45.0, :radius => 10 + + assert_includes assigns('enterprises'), ent1 + assert_not_includes assigns('enterprises'), ent2 + end + should 'not show term "Category:" before product category' do Profile.delete_all ent = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index fe39b3b..daeaa48 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -442,6 +442,20 @@ class ProfileTest < Test::Unit::TestCase assert_not_includes list, outside end + should 'have latitude and longitude' do + e = Enterprise.create!(:name => 'test1', :identifier => 'test1') + e.lat, e.lng = 45, 45 ; e.save! + + assert_includes Enterprise.find_within(2, :origin => [45, 45]), e + end + + should 'have latitude and longitude and find' do + e = Enterprise.create!(:name => 'test1', :identifier => 'test1') + e.lat, e.lng = 45, 45 ; e.save! + + assert_includes Enterprise.find(:all, :within => 2, :origin => [45, 45]), e + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2