From ee0f0a5ee36af36198c6a565cbd8a0d4e61122d8 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Tue, 6 Oct 2009 20:51:00 -0300 Subject: [PATCH] ActionItem1272: fixing contact --- app/controllers/public/contact_controller.rb | 6 ++---- app/models/person.rb | 4 ++++ test/functional/contact_controller_test.rb | 18 +++++++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/controllers/public/contact_controller.rb b/app/controllers/public/contact_controller.rb index b7067c7..55a36b9 100644 --- a/app/controllers/public/contact_controller.rb +++ b/app/controllers/public/contact_controller.rb @@ -8,9 +8,7 @@ class ContactController < PublicController def new @contact if request.post? && params[self.icaptcha_field].blank? - @contact = Contact.new(params[:contact]) - @contact.dest = profile - @contact.sender = user + @contact = user.build_contact(profile, params[:contact]) @contact.city = (!params[:city].blank? && City.exists?(params[:city])) ? City.find(params[:city]).name : nil @contact.state = (!params[:state].blank? && State.exists?(params[:state])) ? State.find(params[:state]).name : nil if @contact.deliver @@ -20,7 +18,7 @@ class ContactController < PublicController flash[:notice] = _('Contact not sent') end else - @contact = Contact.new(:name => user.name, :email => user.email) + @contact = user.build_contact(profile) end end diff --git a/app/models/person.rb b/app/models/person.rb index f232083..02fc976 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -233,6 +233,10 @@ class Person < Profile organization.tasks.pending.select{|task| self.has_permission?(task.permission, organization)} end + def build_contact(profile, params = {}) + Contact.new(params.merge(:name => name, :email => email, :sender => self, :dest => profile)) + end + def is_a_friend?(person) self.friends.include?(person) end diff --git a/test/functional/contact_controller_test.rb b/test/functional/contact_controller_test.rb index 3629ab3..c30b2b7 100644 --- a/test/functional/contact_controller_test.rb +++ b/test/functional/contact_controller_test.rb @@ -41,7 +41,7 @@ class ContactControllerTest < Test::Unit::TestCase end should 'redirect back to contact page after send contact' do - post :new, :profile => enterprise.identifier, :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all'} + post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all'} assert_response :redirect assert_redirected_to :action => 'new' end @@ -61,7 +61,7 @@ class ContactControllerTest < Test::Unit::TestCase City.stubs(:find).returns(City.new(:name => 'Camaçari')) State.stubs(:exists?).returns(true) 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 => {:subject => 'Hi', :message => 'Hi, all'}, :state => '1', :city => '1' assert_equal 'Camaçari', assigns(:contact).city assert_equal 'Bahia', assigns(:contact).state end @@ -71,17 +71,17 @@ class ContactControllerTest < Test::Unit::TestCase assert_tag :tag => 'input', :attributes => {:name => 'contact[receive_a_copy]'} end - should 'not deliver contact if mandatory field is blank' do + should 'deliver contact if subject and message are filled' do post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all'} - assert_response :success - assert_template 'new' + assert_response :redirect + assert_redirected_to :action => '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 => ''} + post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all', :state => '', :city => ''} end end @@ -96,14 +96,14 @@ class ContactControllerTest < Test::Unit::TestCase end should 'be able to post contact while inverse captcha field filled' do - post :new, :profile => enterprise.identifier, :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :state => '', :city => ''} + post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all', :state => '', :city => ''} assert_response :redirect assert_redirected_to :action => 'new' end should 'not be able to post contact while inverse captcha field filled' do - post :new, :profile => enterprise.identifier, @controller.icaptcha_field => 'filled', :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :state => '', :city => ''} + post :new, :profile => enterprise.identifier, @controller.icaptcha_field => 'filled', :contact => {:subject => 'Hi', :message => 'Hi, all', :state => '', :city => ''} assert_response :success assert_template 'new' @@ -117,7 +117,7 @@ class ContactControllerTest < Test::Unit::TestCase end should 'identify sender' do - post :new, :profile => enterprise.identifier, :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :state => '', :city => ''} + post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all', :state => '', :city => ''} assert_equal Person['contact_test_user'], assigns(:contact).sender end -- libgit2 0.21.2