From db00f92d4377ca761993f5635faded2125255c7d Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Mon, 15 Dec 2008 16:47:12 -0300 Subject: [PATCH] ActionItem791: fix bug when state or city is blank --- app/controllers/public/contact_controller.rb | 4 ++-- test/functional/contact_controller_test.rb | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/controllers/public/contact_controller.rb b/app/controllers/public/contact_controller.rb index 3586d14..8beba51 100644 --- a/app/controllers/public/contact_controller.rb +++ b/app/controllers/public/contact_controller.rb @@ -7,8 +7,8 @@ class ContactController < PublicController if request.post? @contact = Contact.new(params[:contact]) @contact.dest = profile - @contact.city = City.exists?(params[:city]) ? City.find(params[:city]).name : _('Missing') - @contact.state = State.exists?(params[:state]) ? State.find(params[:state]).name : _('Missing') + @contact.city = (!params[:city].blank? && City.exists?(params[:city])) ? City.find(params[:city]).name : _('Missing') + @contact.state = (!params[:state].blank? && State.exists?(params[:state])) ? State.find(params[:state]).name : _('Missing') if @contact.deliver flash[:notice] = _('Contact successfully sent') redirect_to :action => 'new' diff --git a/test/functional/contact_controller_test.rb b/test/functional/contact_controller_test.rb index 494d0a8..e8c9159 100644 --- a/test/functional/contact_controller_test.rb +++ b/test/functional/contact_controller_test.rb @@ -64,7 +64,7 @@ class ContactControllerTest < Test::Unit::TestCase should 'define city and state' do City.stubs(:find).returns(City.new(:name => 'Camaçari')) State.stubs(:find).returns(State.new(:name => 'Bahia')) - post :new, :profile => enterprise.identifier, :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :state => 1, :city => 1} + post :new, :profile => enterprise.identifier, :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all'}, :state => '1', :city => '1' assert_equal 'Camaçari', assigns(:contact).city assert_equal 'Bahia', assigns(:contact).state end @@ -80,4 +80,12 @@ class ContactControllerTest < Test::Unit::TestCase assert_template 'new' end + should 'not throws exception when city and state is blank' do + State.expects(:exists?).with('').never + City.expects(:exists?).with('').never + assert_nothing_raised do + post :new, :profile => enterprise.identifier, :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :state => '', :city => ''} + end + end + end -- libgit2 0.21.2