prevents users to receive email notifications about own comments and replies

This commit is contained in:
David Gil
2015-09-10 14:16:45 +02:00
parent 8ed434cc8b
commit d9300f1c10
4 changed files with 89 additions and 48 deletions

View File

@@ -7,10 +7,7 @@ class CommentsController < ApplicationController
respond_to :html, :js respond_to :html, :js
def create def create
if @comment.save unless @comment.save
Mailer.comment(@comment).deliver_later if email_on_debate_comment?
Mailer.reply(@comment).deliver_later if email_on_comment_reply?
else
render :new render :new
end end
end end
@@ -55,14 +52,6 @@ class CommentsController < ApplicationController
@commentable = Comment.find_commentable(comment_params[:commentable_type], comment_params[:commentable_id]) @commentable = Comment.find_commentable(comment_params[:commentable_type], comment_params[:commentable_id])
end end
def email_on_debate_comment?
@comment.commentable.author.email_on_debate_comment?
end
def email_on_comment_reply?
@comment.reply? && @comment.parent.author.email_on_comment_reply?
end
def administrator_comment? def administrator_comment?
["1", true].include?(comment_params[:as_administrator]) && can?(:comment_as_administrator, Debate) ["1", true].include?(comment_params[:as_administrator]) && can?(:comment_as_administrator, Debate)
end end

View File

@@ -0,0 +1,33 @@
class CommentNotifier
def initialize(args = {})
@comment = args.fetch(:comment)
@author = @comment.author
end
def process
send_comment_email
send_reply_email
end
private
def send_comment_email
Mailer.comment(@comment).deliver_later if email_on_debate_comment?
end
def send_reply_email
Mailer.reply(@comment).deliver_later if email_on_comment_reply?
end
def email_on_debate_comment?
commentable_author = @comment.commentable.author
commentable_author != @author && commentable_author.email_on_debate_comment?
end
def email_on_comment_reply?
return false unless @comment.reply?
parent_author = @comment.parent.author
parent_author != @author && parent_author.email_on_comment_reply?
end
end

View File

@@ -24,7 +24,8 @@ 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
scenario "Debate comment", :js do context 'Debate comments' do
scenario "Send email on debate comment", :js do
user = create(:user, email_on_debate_comment: true) user = create(:user, email_on_debate_comment: true)
debate = create(:debate, author: user) debate = create(:debate, author: user)
comment_on(debate) comment_on(debate)
@@ -35,7 +36,25 @@ feature 'Emails' do
expect(email).to have_body_text(debate_path(debate)) expect(email).to have_body_text(debate_path(debate))
end end
scenario "Comment reply", :js do scenario 'Do not send email about own debate comments', :js do
user = create(:user, email_on_debate_comment: true)
debate = create(:debate, author: user)
comment_on(debate, user: user)
expect { open_last_email }.to raise_error "No email has been sent!"
end
scenario 'Do not send email about debate comment unless set in preferences', :js do
user = create(:user, email_on_debate_comment: false)
debate = create(:debate, author: user)
comment_on(debate)
expect { open_last_email }.to raise_error "No email has been sent!"
end
end
context 'Comment replies' do
scenario "Send email on comment reply", :js do
user = create(:user, email_on_comment_reply: true) user = create(:user, email_on_comment_reply: true)
reply_to(user) reply_to(user)
@@ -45,10 +64,9 @@ feature 'Emails' do
expect(email).to have_body_text(debate_path(Comment.first.debate)) expect(email).to have_body_text(debate_path(Comment.first.debate))
end end
scenario 'Do not send email about debate comment unless set in preferences', :js do scenario "Do not send email about own replies to own comments", :js do
user = create(:user, email_on_debate_comment: false) user = create(:user, email_on_comment_reply: true)
debate = create(:debate, author: user) reply_to(user, user: user)
comment_on(debate)
expect { open_last_email }.to raise_error "No email has been sent!" expect { open_last_email }.to raise_error "No email has been sent!"
end end
@@ -59,5 +77,6 @@ feature 'Emails' do
expect { open_last_email }.to raise_error "No email has been sent!" expect { open_last_email }.to raise_error "No email has been sent!"
end end
end
end end

View File

@@ -44,8 +44,8 @@ module CommonActions
click_button 'Send me reset password instructions' click_button 'Send me reset password instructions'
end end
def comment_on(debate) def comment_on(debate, options = {})
user = create(:user) user = options.fetch(:user) { create(:user) }
login_as(user) login_as(user)
visit debate_path(debate) visit debate_path(debate)
@@ -56,10 +56,10 @@ module CommonActions
expect(page).to have_content 'Have you thought about...?' expect(page).to have_content 'Have you thought about...?'
end end
def reply_to(user) def reply_to(original_user, options = {})
manuela = create(:user) manuela = options.fetch(:user) { create(:user) }
debate = create(:debate) debate = create(:debate)
comment = create(:comment, commentable: debate, user: user) comment = create(:comment, commentable: debate, user: original_user)
login_as(manuela) login_as(manuela)
visit debate_path(debate) visit debate_path(debate)