Commit 4fc0ac241f11148a19c2ee4ecfeb912f66cd3b0d

Authored by Braulio Bhavamitra
1 parent fe6c058f

contact: Send message for non logged users

app/controllers/public/contact_controller.rb
1 1 class ContactController < PublicController
2 2  
3   - before_filter :login_required
4   -
5 3 needs_profile
6 4  
7 5 def new
8   - @contact
  6 + @contact = build_contact
9 7 if request.post? && params[:confirm] == 'true'
10   - @contact = user.build_contact(profile, params[:contact])
11 8 @contact.city = (!params[:city].blank? && City.exists?(params[:city])) ? City.find(params[:city]).name : nil
12 9 @contact.state = (!params[:state].blank? && State.exists?(params[:state])) ? State.find(params[:state]).name : nil
13 10 if @contact.deliver
... ... @@ -16,8 +13,17 @@ class ContactController &lt; PublicController
16 13 else
17 14 session[:notice] = _('Contact not sent')
18 15 end
  16 + end
  17 + end
  18 +
  19 + protected
  20 +
  21 + def build_contact
  22 + params[:contact] ||= {}
  23 + if logged_in?
  24 + user.build_contact profile, params[:contact]
19 25 else
20   - @contact = user.build_contact(profile)
  26 + Contact.new params[:contact].merge(dest: profile)
21 27 end
22 28 end
23 29  
... ...
app/views/contact/new.html.erb
... ... @@ -13,11 +13,16 @@
13 13  
14 14 <%= required_fields_message %>
15 15  
16   - <% location_fields = select_city(true) %>
  16 + <% unless logged_in? %>
  17 + <%= required f.text_field(:name) %>
  18 + <%= required f.text_field(:email) %>
  19 + <% end %>
17 20  
  21 + <% location_fields = select_city(true) %>
18 22 <% unless environment.enabled?('disable_select_city_for_contact') || location_fields.blank? %>
19 23 <%= labelled_form_field _('City and state'), location_fields %>
20 24 <% end %>
  25 +
21 26 <%= required f.text_field(:subject) %>
22 27  
23 28 <%= render :file => 'shared/tiny_mce' %>
... ... @@ -25,5 +30,9 @@
25 30  
26 31 <%= labelled_form_field check_box(:contact, :receive_a_copy) + _('I want to receive a copy of the message in my e-mail.'), '' %>
27 32  
28   - <%= submit_button(:send, _('Send'), :onclick => "$('confirm').value = 'true'") %>
  33 + <% unless logged_in? %>
  34 + <%= recaptcha_tags :ajax => true, :display => {:theme => 'clean'} %>
  35 + <% end %>
  36 +
  37 + <%= submit_button(:send, _('Send'), :onclick => "jQuery('#confirm').val('true')") %>
29 38 <% end %>
... ...
test/functional/contact_controller_test.rb
... ... @@ -90,11 +90,12 @@ class ContactControllerTest &lt; ActionController::TestCase
90 90 assert_no_tag :tag => 'select', :attributes => {:name => 'state'}
91 91 end
92 92  
93   - should 'not allow if not logged' do
  93 + should 'show name, email and captcha if not logged' do
94 94 logout
95 95 get :new, :profile => profile.identifier
96   - assert_response :redirect
97   - assert_redirected_to :controller => 'account', :action => 'login'
  96 + assert_tag :tag => 'input', :attributes => {:name => 'contact[name]'}
  97 + assert_tag :tag => 'input', :attributes => {:name => 'contact[email]'}
  98 + assert_tag :attributes => {id: 'dynamic_recaptcha'}
98 99 end
99 100  
100 101 should 'identify sender' do
... ...