From 946bd81026a5103e75454301a0118840015a5d4c Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Sat, 29 Nov 2008 23:03:39 -0300 Subject: [PATCH] ActionItem791: 'contact us' feature for enterprises --- app/controllers/public/contact_controller.rb | 17 +++++++++++++++++ app/helpers/contact_helper.rb | 2 ++ app/models/contact.rb | 13 +++++++++++++ app/models/enterprise.rb | 3 ++- app/views/blocks/profile_info_actions/enterprise.rhtml | 3 +++ app/views/contact/new.rhtml | 17 +++++++++++++++++ app/views/profile_editor/_organization.rhtml | 5 +++-- app/views/tasks/_contact.rhtml | 18 ++++++++++++++++++ config/routes.rb | 3 +++ public/designs/icons/default/style.css | 1 + test/functional/contact_controller_test.rb | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/functional/profile_controller_test.rb | 19 ++++++++++++++++++- test/functional/profile_editor_controller_test.rb | 6 ++++++ test/functional/tasks_controller_test.rb | 12 ++++++++++++ test/integration/routing_test.rb | 4 ++++ test/unit/contact_test.rb | 25 +++++++++++++++++++++++++ test/unit/enterprise_test.rb | 4 ++++ 17 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 app/controllers/public/contact_controller.rb create mode 100644 app/helpers/contact_helper.rb create mode 100644 app/models/contact.rb create mode 100644 app/views/contact/new.rhtml create mode 100644 app/views/tasks/_contact.rhtml create mode 100644 test/functional/contact_controller_test.rb create mode 100644 test/unit/contact_test.rb diff --git a/app/controllers/public/contact_controller.rb b/app/controllers/public/contact_controller.rb new file mode 100644 index 0000000..c9a2491 --- /dev/null +++ b/app/controllers/public/contact_controller.rb @@ -0,0 +1,17 @@ +class ContactController < PublicController + + needs_profile + + def new + @contact = Contact.new(params[:contact]) + if request.post? + if @contact.save + flash[:notice] = _('Contact successfully sent') + redirect_to :controller => 'profile', :profile => profile.identifier + else + flash[:notice] = _('Contact not sent') + end + end + end + +end diff --git a/app/helpers/contact_helper.rb b/app/helpers/contact_helper.rb new file mode 100644 index 0000000..83d146d --- /dev/null +++ b/app/helpers/contact_helper.rb @@ -0,0 +1,2 @@ +module ContactHelper +end diff --git a/app/models/contact.rb b/app/models/contact.rb new file mode 100644 index 0000000..2527bc3 --- /dev/null +++ b/app/models/contact.rb @@ -0,0 +1,13 @@ +class Contact < Task + + validates_presence_of :target_id, :subject, :email, :message + validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT + + acts_as_having_settings :field => :data + settings_items :subject, :message, :city_and_state, :email, :phone + + def description + _('%s sent a new message') % (requestor ? requestor.name : _('Someone')) + end + +end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 3cc387f..283845d 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -57,7 +57,6 @@ class Enterprise < Organization end end - def default_set_of_blocks blocks = [ [MainBlock], @@ -74,6 +73,8 @@ class Enterprise < Organization environment.enterprise_template end + settings_items :enable_contact_us, :type => :boolean, :default => true + protected def default_homepage(attrs) diff --git a/app/views/blocks/profile_info_actions/enterprise.rhtml b/app/views/blocks/profile_info_actions/enterprise.rhtml index 5e736f6..98fe0e1 100644 --- a/app/views/blocks/profile_info_actions/enterprise.rhtml +++ b/app/views/blocks/profile_info_actions/enterprise.rhtml @@ -9,4 +9,7 @@
  • <%= link_to content_tag('span', _('Join')), { :profile => user.identifier, :controller => 'memberships', :action => 'join', :id => profile.id }, :class => 'button with-text icon-add', :title => __('Join this enterprise') %>
  • <% end %> <% end %> + <% if profile.enable_contact_us %> +
  • <%= link_to content_tag('span', _('Contact us')), {:profile => profile.identifier, :controller => 'contact', :action => 'new'}, :class => 'button with-text icon-menu-mail' %>
  • + <% end %> diff --git a/app/views/contact/new.rhtml b/app/views/contact/new.rhtml new file mode 100644 index 0000000..a91108a --- /dev/null +++ b/app/views/contact/new.rhtml @@ -0,0 +1,17 @@ +

    <%= _('Contact %s') % profile.name %>

    + +<%= error_messages_for 'contact' %> + +<% labelled_form_for :contact, @contact do |f| %> + + <%= hidden_field_tag 'contact[target_id]', profile.id %> + <%= hidden_field_tag 'contact[requestor_id]', (logged_in? ? current_user.person.id : nil) %> + <%= f.text_field :subject %> + <%= f.text_field :email %> + <%= f.text_field :city_and_state %> + <%= f.text_field :phone %> + <%= f.text_area :message, :rows => 5 %> + + <%= submit_button(:send, _('Send')) %> + +<% end %> diff --git a/app/views/profile_editor/_organization.rhtml b/app/views/profile_editor/_organization.rhtml index 4d65282..51342e5 100644 --- a/app/views/profile_editor/_organization.rhtml +++ b/app/views/profile_editor/_organization.rhtml @@ -9,12 +9,13 @@ <%= f.text_field(:acronym) %> - <%= f.text_field(:address, 'size' => 50) if @profile.kind_of?(Enterprise) %> + <%= f.text_field(:address, 'size' => 50) if @profile.enterprise? %> <%= f.text_field(:foundation_year) %> <%= f.text_field(:contact_person) %> <%= f.text_field(:contact_email) %> <%= f.text_field(:economic_activity) %> - <%= f.text_area(:description, :rows => 5) if @profile.kind_of?(Community) %> + <%= f.text_area(:description, :rows => 5) if @profile.community? %> + <%= f.check_box(:enable_contact_us) if @profile.enterprise? %>

    <%= _('Moderation options') %>

    <%= _('New members must be approved:')%> diff --git a/app/views/tasks/_contact.rhtml b/app/views/tasks/_contact.rhtml new file mode 100644 index 0000000..0db37cb --- /dev/null +++ b/app/views/tasks/_contact.rhtml @@ -0,0 +1,18 @@ +

    <%= task.description %>

    + +<% form_for('task', task, :url => { :action => 'close', :id => task.id}) do |f| %> + + <%= hidden_field_tag(:decision, :finish) %> + + <% %w[ subject email phone city_and_state message ].each do |field| %> + <% if task.respond_to?(field) and !task.send(field).nil? and !task.send(field).empty? %> + + <% end %> + <% end %> +
    <%= _(field.humanize) %> <%= task.send(field) %>
    + + <% button_bar do %> + <%= submit_button(:ok, _('Ok!')) %> + <% end %> + +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 4d7fbd4..defdc3d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,6 +53,9 @@ ActionController::Routing::Routes.draw do |map| map.catalog 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format}/ map.product 'catalog/:profile/:id', :controller => 'catalog', :action => 'show', :profile => /#{Noosfero.identifier_format}/ + # contact + map.contact 'contact/:profile/:action/:id', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format}/ + ###################################################### ## Controllers that are profile-specific (for profile admins ) ###################################################### diff --git a/public/designs/icons/default/style.css b/public/designs/icons/default/style.css index 153a347..e1413df 100644 --- a/public/designs/icons/default/style.css +++ b/public/designs/icons/default/style.css @@ -5,6 +5,7 @@ .icon-open { background-image: url(folder-open.gif) } .icon-cms { background-image: url(abiword_48.png) } .icon-save { background-image: url(save-HC.gif) } +.icon-send { background-image: url(mail-HC.gif) } .icon-up { background-image: url(go-up-HC.gif) } .icon-cancel { background-image: url(cancel-HC.gif) } .icon-person { background-image: url(user_icon.png) } diff --git a/test/functional/contact_controller_test.rb b/test/functional/contact_controller_test.rb new file mode 100644 index 0000000..cfb8864 --- /dev/null +++ b/test/functional/contact_controller_test.rb @@ -0,0 +1,75 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'contact_controller' + +# Re-raise errors caught by the controller. +class ContactController; def rescue_action(e) raise e end; end + +class ContactControllerTest < Test::Unit::TestCase + + all_fixtures + + def setup + @controller = ContactController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @profile = create_user('contact_test_user').person + @enterprise = Enterprise.create!(:identifier => 'contact_test_enterprise', :name => 'Test contact enteprise') + end + attr_reader :profile, :enterprise + + should 'respond to new' do + get :new, :profile => enterprise.identifier + assert_response :success + end + + should 'display destinatary name in title' do + get :new, :profile => enterprise.identifier + assert_tag :tag => 'h1', :content => "Contact #{enterprise.name}" + end + + should 'add form to create contact via post' do + get :new, :profile => enterprise.identifier + assert_tag :tag => 'form', :attributes => { :action => "/contact/#{enterprise.identifier}/new", :method => 'post' } + end + + should 'display input for destinatary email' do + get :new, :profile => enterprise.identifier + assert_tag :tag => 'input', :attributes => { :name => 'contact[email]', :type => 'text' } + end + + should 'display input for message' do + get :new, :profile => enterprise.identifier + assert_tag :tag => 'textarea', :attributes => { :name => 'contact[message]' } + end + + should 'add hidden field with target_id' do + get :new, :profile => enterprise.identifier + assert_tag :tag => 'input', :attributes => { :name => 'contact[target_id]', :value => enterprise.id, :type => 'hidden' } + end + + should 'add requestor id if logged in' do + login_as(profile.identifier) + @controller.stubs(:current_user).returns(profile.user) + get :new, :profile => enterprise.identifier + assert_tag :tag => 'input', :attributes => { :name => 'contact[requestor_id]', :value => profile.id } + end + + should 'nil requestor id if not logged in' do + get :new, :profile => enterprise.identifier + assert_tag :tag => 'input', :attributes => { :name => 'contact[requestor_id]', :value => nil } + end + + should 'redirect to profile page after contact' do + post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :target_id => enterprise.id} + assert_response :redirect + assert_redirected_to :controller => 'profile', :profile => enterprise.identifier + end + + should 'be able to send contact' do + assert_difference Contact, :count do + post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :target_id => enterprise.id} + end + end + +end diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 6bc112b..9f77075 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -249,7 +249,7 @@ class ProfileControllerTest < Test::Unit::TestCase get :index, :profile => 'my-test-enterprise' assert_tag :tag => 'a', :attributes => { :href => '/catalog/my-test-enterprise'}, :content => /Products\/Services/ end - + should 'not display "Products" link for enterprise if environment do not let' do env = Environment.default env.enable('disable_products_for_enterprises') @@ -304,4 +304,21 @@ class ProfileControllerTest < Test::Unit::TestCase assert_no_tag :content => /t2@t2.com/ end + should 'display contact us for enterprises' do + ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise') + get :index, :profile => 'my-test-enterprise' + assert_tag :tag => 'a', :attributes => { :href => "/contact/my-test-enterprise/new" }, :content => 'Contact us' + end + + should 'not display contact us for non-enterprises' do + get :index, :profile => @profile.identifier + assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{@profile.identifier}/new" }, :content => 'Contact us' + end + + should 'display contact us only if enabled' do + ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise', :enable_contact_us => false) + get :index, :profile => 'my-test-enterprise' + assert_no_tag :tag => 'a', :attributes => { :href => "/contact/my-test-enterprise/new" }, :content => 'Contact us' + end + end diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index 043f645..c241514 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -517,4 +517,10 @@ class ProfileEditorControllerTest < Test::Unit::TestCase :attributes => { :name=>'profile_data[email]', :value=>'teste_user@teste.com' } end + should 'display enable contact us for enterprise' do + org = Enterprise.create!(:name => 'test org', :identifier => 'testorg') + get :edit, :profile => 'testorg' + assert_tag :tag => 'input', :attributes => {:name => 'profile_data[enable_contact_us]', :type => 'checkbox'} + end + end diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 24e6b32..0d5de8a 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -151,4 +151,16 @@ class TasksControllerTest < Test::Unit::TestCase assert_equal f, assigns(:ticket).target end + + should 'list enterprise contacts' do + ent = Enterprise.create!(:identifier => 'contact_test_enterprise', :name => 'Test contact enteprise') + task = Contact.create!(:subject => 'test', :target_id => profile.id, :email => 'visitor@invalid.com', :message => 'Hi, all') + + login_as(profile.identifier) + get :index, :profile => ent.identifier + + assert_includes assigns(:tasks), task + assert_tag :tag => 'li', :attributes => { :class => 'task-Contact' }, :content => 'Someone sent a new message' + end + end diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index bfadba0..cf02f10 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -186,4 +186,8 @@ class RoutingTest < ActionController::IntegrationTest assert_routing('/myprofile/profile.withdot', :controller => 'profile_editor', :action => 'index', :profile => 'profile.withdot') end + def test_contact_routing + assert_routing('/contact/wintermute/new', :controller => 'contact', :action => 'new', :profile => 'wintermute') + end + end diff --git a/test/unit/contact_test.rb b/test/unit/contact_test.rb new file mode 100644 index 0000000..fdb760b --- /dev/null +++ b/test/unit/contact_test.rb @@ -0,0 +1,25 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ContactTest < ActiveSupport::TestCase + + should 'have serialized data' do + t = Contact.new + t.data[:test] = 'test' + + assert_equal({:test => 'test'}, t.data) + end + + should 'validates required fields' do + contact = Contact.new + assert !contact.valid? + contact.subject = 'Hi' + assert !contact.valid? + contact.email = 'visitor@invalid.com' + assert !contact.valid? + contact.message = 'Hi, all' + assert !contact.valid? + contact.target = create_user('contact_user_test').person + assert contact.save! + end + +end diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index dfc4ee1..4e13433 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -211,5 +211,9 @@ class EnterpriseTest < Test::Unit::TestCase assert_kind_of Enterprise, p.template end + should 'contact us enabled by default' do + e = Enterprise.create!(:name => 'test_com', :identifier => 'test_com', :environment => Environment.default) + assert e.enable_contact_us + end end -- libgit2 0.21.2