Skip to content

Commit

Permalink
contact form refactored and ajaxified
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasknoepfle committed Feb 18, 2015
1 parent 394af7c commit ec5f74a
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 116 deletions.
26 changes: 4 additions & 22 deletions app/controllers/toolbox_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,9 @@ def reindex

# Send a single email to a private user, should be refactored when we have a real messaging system
def contact
session[:seller_message] = {} unless session[:seller_message]
session[:seller_message][params[:contact][:article_id]] = params[:contact][:text] # store text

if params && params[:contact][:article_id]
article = Article.find params[:contact][:article_id]
return redirect_to :back, flash: { error: I18n.t('article.show.contact.acceptance_error') } unless params[:contact][:email_transfer_accepted] == "1" # manual validation: transfer of email was accepted
return redirect_to :back, flash: { error: I18n.t('article.show.contact.empty_error') } unless params[:contact][:text].length > 0 # manual validation: message is present
return redirect_to :back, flash: { error: I18n.t('article.show.contact.long_error') } unless params[:contact][:text].length < 2000 # manual validation: message is shorter than 2000 characters
ArticleMailer.delay.contact(current_user,
article,
params[:contact][:text])
session[:seller_message][params[:contact][:article_id]] = nil # delete from session
redirect_to article, notice: I18n.t('article.show.contact.success_notice')

elsif params && params[:contact][:user_id]
user = User.find params[:contact][:user_id]
return redirect_to user, flash: { error: I18n.t('users.profile.contact.acceptance_error') } unless params[:contact][:email_transfer_accepted] == "1" # manual validation: transfer of email was accepted
return redirect_to user, flash: { error: I18n.t('users.profile.contact.empty_error') } unless params[:contact][:text].length > 0 # manual validation: message is present
return redirect_to user, flash: { error: I18n.t('users.profile.contact.long_error') } unless params[:contact][:text].length < 2000 # manual validation: message is shorter than 2000 characters
UserMailer.delay.contact(sender: current_user, receiver: user, text: params[:contact][:text])
session[:seller_message][params[:contact][:user_id]] = nil # delete from session
redirect_to user, notice: I18n.t('users.profile.contact.success_notice')
@contact_form = ContactForm.new(params[:contact_form])
if @contact_form.valid?
@contact_form.mail current_user, params[:resource_id], params[:resource_type]
end
end

Expand All @@ -78,6 +59,7 @@ def newsletter_status
end

private

def get_feed_items
begin
Timeout::timeout(10) do #10 second timeout
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ def checkbox_link_helper label, target
link_to label, target, target: '_blank', onclick: on_click_open_link_in_label(target)
end

def get_or_create_contact_form
@contact_form || ContactForm.new
end

end
6 changes: 3 additions & 3 deletions app/mailers/article_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def report_article article, user, text
mail(to: $email_addresses['ArticleMailer']['report'], from: mail, subject: "Article reported with ID: #{article.id}")
end

def contact user, article, text
@user = user
def contact(sender:, resource_id:, text:)
@user = sender
@text = text
@article = article
@article = Article.find(resource_id)
@from = @user.email
@subject = I18n.t('article.show.contact.mail_subject')
mail to: @article.seller_email, from: @from, subject: @subject
Expand Down
4 changes: 2 additions & 2 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class UserMailer < ActionMailer::Base
before_filter :inline_logos
layout 'email'

def contact(sender:, receiver:, text:)
def contact(sender:, resource_id:, text:)
@sender = sender
@receiver = receiver
@receiver = User.find resource_id
@text = text
@subject = I18n.t('email.user.contact.subject')
mail to: @receiver.email, subject: @subject
Expand Down
20 changes: 20 additions & 0 deletions app/objects/form/contact_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ContactForm
include ActiveData::Model

attribute :email_transfer_accepted, type: Boolean, default: true
attribute :text, type: String


validates :email_transfer_accepted, acceptance: { accept: true }
validates :text, length: { maximum: 2000 }, presence: true

def mail(sender, resource_id, resource_type)
mailer = get_mailer_for resource_type
mailer.delay.contact(sender: sender, resource_id: resource_id, text: self.text) if mailer
end

def get_mailer_for resource_type
return ArticleMailer if resource_type == "article"
return UserMailer if resource_type == "user"
end
end
18 changes: 1 addition & 17 deletions app/views/articles/show/_seller.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,4 @@
= link_to(library.user_nickname, library.user)
/ Contact Box (refactor when we have a real messaging system)
= accordion_item 'contact_user' do
- if current_user
p
= t('article.show.contact.description')
= semantic_form_for :contact, url: toolbox_contact_path, method: :get do |f|
= f.inputs do
= f.input :article_id, as: :hidden, input_html: { value: resource.id }
= f.input :email_transfer_accepted, label: t('article.show.contact.email_transfer_accepted_label'), as: :boolean, required: true, input_html: { checked: true }
- text = (session[:seller_message]) ? session[:seller_message][resource.id.to_s] : nil
= f.input :text, as: :text, input_html: { rows: 4, value: text }

= f.actions do
= f.action :submit, label: t('article.show.contact.action'), button_html: {class: "Button"}
- else
| Um eine Rückfrage stellen zu können, musst Du dich bei Fairmondo
=<> link_to 'registrieren', new_user_registration_path
| und
=<> link_to('einloggen', new_user_session_path) + '.'
= render 'toolbox/contact', resource_type: :article, resource_id: resource.id
15 changes: 15 additions & 0 deletions app/views/toolbox/_contact.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#contact_form
- if current_user
p
= t('article.show.contact.description')
= semantic_form_for get_or_create_contact_form , url: toolbox_contact_path(resource_type: resource_type, resource_id: resource_id), remote: true, method: :post do |f|
= f.inputs do
= f.input :email_transfer_accepted, label: t('article.show.contact.email_transfer_accepted_label'), as: :boolean, required: true
= f.input :text, as: :text, input_html: { rows: 4 }
= f.actions do
= f.action :submit, label: t('article.show.contact.action'), button_html: {class: "Button"}
- else
| Um eine Rückfrage stellen zu können, musst Du dich bei Fairmondo
=<> link_to 'registrieren', new_user_registration_path
| und
=<> link_to('einloggen', new_user_session_path) + '.'
5 changes: 5 additions & 0 deletions app/views/toolbox/contact.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- if @contact_form.errors.present?
render "toolbox/contact", resource_type: params[:resource_type], resource_id: params[:resource_id]
- else
p
= I18n.t('users.profile.contact.success_notice')
6 changes: 6 additions & 0 deletions app/views/toolbox/contact.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if @contact_form.errors.present? %>
$("#contact_form").replaceWith("<%= j render "toolbox/contact", resource_type: params[:resource_type], resource_id: params[:resource_id] %>");
<% else %>
$("#contact_form").replaceWith("<p><%= I18n.t('users.profile.contact.success_notice') %></p>");
$.colorbox.resize()
<% end %>
23 changes: 4 additions & 19 deletions app/views/users/contact.html.slim
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
/ Contact Box (refactor when we have a real messaging system)
.Content.contact_seller
- if current_user
h1= t('users.profile.contact.heading')
h4= t('users.profile.contact.description')
= semantic_form_for :contact, url: toolbox_contact_path, method: :get, html: { class: 'js-visual-submit' } do |f|
= f.inputs do
= f.hidden_field :user_id, value: resource.id
- text = (session[:seller_message]) ? session[:seller_message][resource.id.to_s] : nil
= f.input :text, as: :text, label: t('users.profile.contact.labels.text'),input_html: { rows: 4, value: text, placeholder: t('users.profile.contact.labels.text') }
= f.input :email_transfer_accepted, label: t('users.profile.contact.email_transfer_accepted_label'), as: :boolean, required: true, input_html: { checked: true }

= f.actions do
= f.action :submit, label: t('users.profile.contact.action'), button_html: {class: "Button"}
- else
| Um eine Rückfrage stellen zu können, musst Du dich bei Fairmondo
=<> link_to 'registrieren', new_user_registration_path
| und
=<> link_to('einloggen', new_user_session_path) + '.'

/ Contact Box (refactor when we have a real messaging system)
.Content
h1= t('users.profile.contact.heading')
= render 'toolbox/contact', resource_type: :user, resource_id: @user.id
3 changes: 0 additions & 3 deletions app/views/users/contact.js.erb

This file was deleted.

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
get 'confirm', constraints: {format: 'js'}
get 'rss'
get 'reload', as: 'reload'
get 'contact', as: 'contact'
post 'contact/:resource_type/:resource_id', as: 'contact', action: 'contact'
patch 'reindex/:article_id', action: 'reindex', as: 'reindex'
get 'healthcheck'
get 'newsletter_status', as: 'newsletter_status', constraints: {format: 'json'}
Expand Down
18 changes: 2 additions & 16 deletions test/features/articles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,10 @@ def fill_form_with_valid_article
end

scenario "user contacts seller" do
fill_in 'contact[text]', with: 'foobar'
fill_in 'contact_form[text]', with: 'foobar'
click_button I18n.t('article.show.contact.action')

page.must_have_content I18n.t 'article.show.contact.success_notice'
end

scenario "user contacts seller but unchecks email transfer acceptance" do
fill_in 'contact[text]', with: 'foobar'
uncheck 'contact[email_transfer_accepted]'
click_button I18n.t('article.show.contact.action')

page.must_have_content I18n.t 'article.show.contact.acceptance_error'
end

scenario "user contacts seller with blank message" do
fill_in 'contact[text]', with: ''
click_button I18n.t('article.show.contact.action')
page.must_have_content I18n.t 'article.show.contact.empty_error'
page.must_have_content I18n.t 'users.profile.contact.success_notice'
end

end
Expand Down
34 changes: 3 additions & 31 deletions test/features/user_profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,10 @@
end

scenario "user contacts seller" do
within('.contact_seller') do
fill_in 'contact[text]', with: 'foobar'
click_button I18n.t('users.profile.contact.action')
within('#contact_form') do
fill_in 'contact_form[text]', with: 'foobar'
click_button I18n.t('article.show.contact.action')
end
page.must_have_content I18n.t 'users.profile.contact.success_notice'
end

scenario "user contacts seller but unchecks email transfer acceptance" do
within('.contact_seller') do
fill_in 'contact[text]', with: 'foobar'
uncheck 'contact[email_transfer_accepted]'
click_button I18n.t('users.profile.contact.action')
end
page.must_have_content I18n.t('users.profile.contact.acceptance_error')
end

scenario "user contacts seller with blank message" do
within('.contact_seller') do
fill_in 'contact[text]', with: ''
click_button I18n.t('users.profile.contact.action')
end
page.must_have_content I18n.t('users.profile.contact.empty_error')
end

scenario "user contacts seller with message that exceeds 2000 character limit" do
text = ''
2001.times { text += 'a' }

within('.contact_seller') do
fill_in 'contact[text]', with: text
click_button I18n.t('users.profile.contact.action')
end
page.must_have_content I18n.t('users.profile.contact.long_error')
end
end
2 changes: 1 addition & 1 deletion test/mailers/article_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
end

it "#contact" do
mail = ArticleMailer.contact(user, article, 'foobar')
mail = ArticleMailer.contact(sender: user, resource_id: article.id, text: 'foobar')

mail.must deliver_to article.seller_email
mail.must have_subject I18n.t('article.show.contact.mail_subject')
Expand Down
2 changes: 1 addition & 1 deletion test/mailers/user_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
let(:text) { 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' }

it '#contact' do
mail = UserMailer.contact(sender: sender, receiver: receiver, text: text)
mail = UserMailer.contact(sender: sender, resource_id: receiver.id, text: text)

mail.must deliver_to receiver.email
mail.subject.must_equal("[Fairmondo] ein/e Nutzer*in hat eine Frage an Dich")
Expand Down

0 comments on commit ec5f74a

Please sign in to comment.