diff --git a/app/helpers/mailer_helper.rb b/app/helpers/mailer_helper.rb new file mode 100644 index 000000000..ad6f042af --- /dev/null +++ b/app/helpers/mailer_helper.rb @@ -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 \ No newline at end of file diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 15a8eb54a..844f61a87 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -1,25 +1,26 @@ class Mailer < ApplicationMailer helper :text_with_links + helper :mailer def comment(comment) @comment = comment - @debate = comment.debate - mail(to: @debate.author.email, subject: t('mailer.comment.subject')) + @commentable = comment.commentable + 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 def reply(reply) @reply = reply - @debate = @reply.debate + @commentable = @reply.commentable parent = Comment.find(@reply.parent_id) @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 def email_verification(user, recipient, token) @user = user @recipient = recipient @token = token - mail(to: @recipient, subject: "Verifica tu email") + mail(to: @recipient, subject: t('mailers.email_verification.subject')) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 187e35037..e9bb59775 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -36,10 +36,6 @@ class Comment < ActiveRecord::Base c_type.constantize.find(c_id) end - def debate - commentable if commentable.class == Debate - end - def author_id user_id end diff --git a/app/views/mailer/comment.html.erb b/app/views/mailer/comment.html.erb index 16ace7990..a9834bd0b 100644 --- a/app/views/mailer/comment.html.erb +++ b/app/views/mailer/comment.html.erb @@ -5,11 +5,11 @@

- <%= t("mailers.comment.hi") %> <%= @debate.author.name %>, + <%= t("mailers.comment.hi") %> <%= @commentable.author.name %>,

- <%= 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;" %>

diff --git a/app/views/mailer/reply.html.erb b/app/views/mailer/reply.html.erb index d006e205a..0c1844507 100644 --- a/app/views/mailer/reply.html.erb +++ b/app/views/mailer/reply.html.erb @@ -9,7 +9,7 @@

- <%= 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;" %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index 95a40fa0d..a50d1415c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -300,11 +300,6 @@ en: check: Select check_all: All check_none: None - mailer: - comment: - subject: Someone has commented on your debate - reply: - subject: Someone has replied to your comment unauthorized: default: "You are not authorized to access this page." manage: diff --git a/config/locales/es.yml b/config/locales/es.yml index c196d080c..bf39516d3 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -300,11 +300,6 @@ es: check: Seleccionar check_all: Todos check_none: Ninguno - mailer: - comment: - subject: Alguien ha comentado en tu propuesta - reply: - subject: Alguien ha respondido a tu comentario unauthorized: default: "No tienes permiso para acceder a esta página." manage: diff --git a/config/locales/mailers.en.yml b/config/locales/mailers.en.yml index c5aa00a97..030dc13de 100644 --- a/config/locales/mailers.en.yml +++ b/config/locales/mailers.en.yml @@ -1,14 +1,17 @@ en: mailers: comment: + subject: "Someone has commented on your %{commentable}" hi: Hello - title: New comment on your debate + title: New comment new_comment_by_html: "There is a new comment by %{commenter} on" reply: + subject: Someone has replied to your comment hi: Hello title: New reply on your comment new_reply_by_html: "There'is a new reply by %{commenter} to your comment on" email_verification: + subject: Verify your email title: Please verify yourself 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" diff --git a/config/locales/mailers.es.yml b/config/locales/mailers.es.yml index 9b37b2dd5..1ca753ab5 100644 --- a/config/locales/mailers.es.yml +++ b/config/locales/mailers.es.yml @@ -1,14 +1,17 @@ es: mailers: comment: + subject: "Alguien ha comentado en tu %{commentable}" hi: Hola - title: Nuevo comentario en tu debate + title: Nuevo comentario new_comment_by_html: "Hay un nuevo comentario de %{commenter} en" reply: + subject: Alguien ha respondido a tu comentario hi: Hola title: Nueva respuesta a tu comentario new_reply_by_html: "Hay una nueva respuesta de %{commenter} a tu comentario en" email_verification: + subject: Verifica tu email 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}." click_here_to_verify: "en este enlace" diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index 1ce4b529f..3058de44c 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -24,6 +24,35 @@ feature 'Emails' do expect(email).to have_body_text(edit_user_password_path) 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 scenario "Send email on debate comment", :js do user = create(:user, email_on_comment: true) @@ -61,7 +90,7 @@ feature 'Emails' do email = open_last_email expect(email).to have_subject('Someone has replied to your comment') 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 scenario "Do not send email about own replies to own comments", :js do diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index 4e2dcdb18..d1a6bcdf6 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -44,13 +44,14 @@ module CommonActions click_button 'Send me reset password' end - def comment_on(debate, user = nil) + def comment_on(commentable, user = nil) user ||= create(: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' expect(page).to have_content 'Have you thought about...?'