diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index b7efb327e..844f61a87 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -4,23 +4,23 @@ class Mailer < ApplicationMailer 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/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/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