Merge pull request #549 from AyuntamientoMadrid/fix-mailers

Fix mailers
This commit is contained in:
Raimond Garcia
2015-09-24 13:05:16 +02:00
11 changed files with 59 additions and 28 deletions

View File

@@ -0,0 +1,8 @@
module MailerHelper
def commentable_url(commentable)
return debate_url(commentable) if commentable.is_a?(Debate)
return proposal_url(commentable) if commentable.is_a?(Proposal)
end
end

View File

@@ -1,25 +1,26 @@
class Mailer < ApplicationMailer class Mailer < ApplicationMailer
helper :text_with_links helper :text_with_links
helper :mailer
def comment(comment) def comment(comment)
@comment = comment @comment = comment
@debate = comment.debate @commentable = comment.commentable
mail(to: @debate.author.email, subject: t('mailer.comment.subject')) mail(to: @commentable.author.email, subject: t('mailers.comment.subject', commentable: t("activerecord.models.#{@commentable.class.name.downcase}").downcase)) if @commentable.present? && @commentable.author.present?
end end
def reply(reply) def reply(reply)
@reply = reply @reply = reply
@debate = @reply.debate @commentable = @reply.commentable
parent = Comment.find(@reply.parent_id) parent = Comment.find(@reply.parent_id)
@recipient = parent.author @recipient = parent.author
mail(to: @recipient.email, subject: t('mailer.reply.subject')) mail(to: @recipient.email, subject: t('mailers.reply.subject')) if @commentable.present? && @recipient.present?
end end
def email_verification(user, recipient, token) def email_verification(user, recipient, token)
@user = user @user = user
@recipient = recipient @recipient = recipient
@token = token @token = token
mail(to: @recipient, subject: "Verifica tu email") mail(to: @recipient, subject: t('mailers.email_verification.subject'))
end end
end end

View File

@@ -36,10 +36,6 @@ class Comment < ActiveRecord::Base
c_type.constantize.find(c_id) c_type.constantize.find(c_id)
end end
def debate
commentable if commentable.class == Debate
end
def author_id def author_id
user_id user_id
end end

View File

@@ -5,11 +5,11 @@
</h1> </h1>
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;"> <p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t("mailers.comment.hi") %> <strong><%= @debate.author.name %></strong>, <%= t("mailers.comment.hi") %> <strong><%= @commentable.author.name %></strong>,
</p> </p>
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;"> <p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t("mailers.comment.new_comment_by_html", commenter: @comment.author.name) %> <%= link_to @debate.title, debate_url(@debate), style: "color: #2895F1; text-decoration:none;" %> <%= t("mailers.comment.new_comment_by_html", commenter: @comment.author.name) %> <%= link_to @commentable.title, commentable_url(@commentable), style: "color: #2895F1; text-decoration:none;" %>
</p> </p>
<p style="border-left: 2px solid #DEE0E3;font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-style: italic;font-weight: normal;line-height: 24px;margin-left: 20px;padding: 10px;"> <p style="border-left: 2px solid #DEE0E3;font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-style: italic;font-weight: normal;line-height: 24px;margin-left: 20px;padding: 10px;">

View File

@@ -9,7 +9,7 @@
</p> </p>
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;"> <p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t("mailers.reply.new_reply_by_html", commenter: @reply.author.name) %> <%= link_to @debate.title, debate_url(@debate), style: "color: #2895F1; text-decoration:none;" %> <%= t("mailers.reply.new_reply_by_html", commenter: @reply.author.name) %> <%= link_to @commentable.title, commentable_url(@commentable), style: "color: #2895F1; text-decoration:none;" %>
</p> </p>
<p style="border-left: 2px solid #DEE0E3;font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-style: italic;font-weight: normal;line-height: 24px;margin-left: 20px;padding: 10px;"> <p style="border-left: 2px solid #DEE0E3;font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-style: italic;font-weight: normal;line-height: 24px;margin-left: 20px;padding: 10px;">

View File

@@ -300,11 +300,6 @@ en:
check: Select check: Select
check_all: All check_all: All
check_none: None check_none: None
mailer:
comment:
subject: Someone has commented on your debate
reply:
subject: Someone has replied to your comment
unauthorized: unauthorized:
default: "You are not authorized to access this page." default: "You are not authorized to access this page."
manage: manage:

View File

@@ -300,11 +300,6 @@ es:
check: Seleccionar check: Seleccionar
check_all: Todos check_all: Todos
check_none: Ninguno check_none: Ninguno
mailer:
comment:
subject: Alguien ha comentado en tu propuesta
reply:
subject: Alguien ha respondido a tu comentario
unauthorized: unauthorized:
default: "No tienes permiso para acceder a esta página." default: "No tienes permiso para acceder a esta página."
manage: manage:

View File

@@ -1,14 +1,17 @@
en: en:
mailers: mailers:
comment: comment:
subject: "Someone has commented on your %{commentable}"
hi: Hello hi: Hello
title: New comment on your debate title: New comment
new_comment_by_html: "There is a new comment by <b>%{commenter}</b> on" new_comment_by_html: "There is a new comment by <b>%{commenter}</b> on"
reply: reply:
subject: Someone has replied to your comment
hi: Hello hi: Hello
title: New reply on your comment title: New reply on your comment
new_reply_by_html: "There'is a new reply by <b>%{commenter}</b> to your comment on" new_reply_by_html: "There'is a new reply by <b>%{commenter}</b> to your comment on"
email_verification: email_verification:
subject: Verify your email
title: Please verify yourself title: Please verify yourself
instructions_html: "We need to verify you using this email, which we got from the Census. %{verification_link}" instructions_html: "We need to verify you using this email, which we got from the Census. %{verification_link}"
click_here_to_verify: "Please click here to verify yourself" click_here_to_verify: "Please click here to verify yourself"

View File

@@ -1,14 +1,17 @@
es: es:
mailers: mailers:
comment: comment:
subject: "Alguien ha comentado en tu %{commentable}"
hi: Hola hi: Hola
title: Nuevo comentario en tu debate title: Nuevo comentario
new_comment_by_html: "Hay un nuevo comentario de <b>%{commenter}</b> en" new_comment_by_html: "Hay un nuevo comentario de <b>%{commenter}</b> en"
reply: reply:
subject: Alguien ha respondido a tu comentario
hi: Hola hi: Hola
title: Nueva respuesta a tu comentario title: Nueva respuesta a tu comentario
new_reply_by_html: "Hay una nueva respuesta de <b>%{commenter}</b> a tu comentario en" new_reply_by_html: "Hay una nueva respuesta de <b>%{commenter}</b> a tu comentario en"
email_verification: email_verification:
subject: Verifica tu email
title: Verifica tu cuenta con el siguiente enlace title: Verifica tu cuenta con el siguiente enlace
instructions_html: "Para terminar de verificar tu cuenta de usuario en el Portal de Gobierno Abierto del Ayuntamiento de Madrid, necesitamos que pulses %{verification_link}." instructions_html: "Para terminar de verificar tu cuenta de usuario en el Portal de Gobierno Abierto del Ayuntamiento de Madrid, necesitamos que pulses %{verification_link}."
click_here_to_verify: "en este enlace" click_here_to_verify: "en este enlace"

View File

@@ -24,6 +24,35 @@ feature 'Emails' do
expect(email).to have_body_text(edit_user_password_path) expect(email).to have_body_text(edit_user_password_path)
end end
context 'Proposal comments' do
scenario "Send email on proposal comment", :js do
user = create(:user, email_on_comment: true)
proposal = create(:proposal, author: user)
comment_on(proposal)
email = open_last_email
expect(email).to have_subject('Someone has commented on your proposal')
expect(email).to deliver_to(proposal.author)
expect(email).to have_body_text(proposal_path(proposal))
end
scenario 'Do not send email about own proposal comments', :js do
user = create(:user, email_on_comment: true)
proposal = create(:proposal, author: user)
comment_on(proposal, user)
expect { open_last_email }.to raise_error "No email has been sent!"
end
scenario 'Do not send email about proposal comment unless set in preferences', :js do
user = create(:user, email_on_comment: false)
proposal = create(:proposal, author: user)
comment_on(proposal)
expect { open_last_email }.to raise_error "No email has been sent!"
end
end
context 'Debate comments' do context 'Debate comments' do
scenario "Send email on debate comment", :js do scenario "Send email on debate comment", :js do
user = create(:user, email_on_comment: true) user = create(:user, email_on_comment: true)
@@ -61,7 +90,7 @@ feature 'Emails' do
email = open_last_email email = open_last_email
expect(email).to have_subject('Someone has replied to your comment') expect(email).to have_subject('Someone has replied to your comment')
expect(email).to deliver_to(user) expect(email).to deliver_to(user)
expect(email).to have_body_text(debate_path(Comment.first.debate)) expect(email).to have_body_text(debate_path(Comment.first.commentable))
end end
scenario "Do not send email about own replies to own comments", :js do scenario "Do not send email about own replies to own comments", :js do

View File

@@ -44,13 +44,14 @@ module CommonActions
click_button 'Send me reset password' click_button 'Send me reset password'
end end
def comment_on(debate, user = nil) def comment_on(commentable, user = nil)
user ||= create(:user) user ||= create(:user)
login_as(user) login_as(user)
visit debate_path(debate) commentable_path = commentable.is_a?(Proposal) ? proposal_path(commentable) : debate_path(commentable)
visit commentable_path
fill_in "comment-body-debate_#{debate.id}", with: 'Have you thought about...?' fill_in "comment-body-#{commentable.class.name.downcase}_#{commentable.id}", with: 'Have you thought about...?'
click_button 'Publish comment' click_button 'Publish comment'
expect(page).to have_content 'Have you thought about...?' expect(page).to have_content 'Have you thought about...?'