diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 96aecf083..358ff5516 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -7,10 +7,7 @@ class CommentsController < ApplicationController respond_to :html, :js def create - if @comment.save - Mailer.comment(@comment).deliver_later if email_on_debate_comment? - Mailer.reply(@comment).deliver_later if email_on_comment_reply? - else + unless @comment.save render :new end end @@ -55,14 +52,6 @@ class CommentsController < ApplicationController @commentable = Comment.find_commentable(comment_params[:commentable_type], comment_params[:commentable_id]) 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? ["1", true].include?(comment_params[:as_administrator]) && can?(:comment_as_administrator, Debate) end diff --git a/app/models/comment_notifier.rb b/app/models/comment_notifier.rb new file mode 100644 index 000000000..8a82e9df0 --- /dev/null +++ b/app/models/comment_notifier.rb @@ -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 diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index 86c794fac..cd4d85116 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -24,40 +24,59 @@ feature 'Emails' do expect(email).to have_body_text(edit_user_password_path) end - scenario "Debate comment", :js do - user = create(:user, email_on_debate_comment: true) - debate = create(:debate, author: user) - comment_on(debate) + context 'Debate comments' do + scenario "Send email on debate comment", :js do + user = create(:user, email_on_debate_comment: true) + debate = create(:debate, author: user) + comment_on(debate) - email = open_last_email - expect(email).to have_subject('Someone has commented on your debate') - expect(email).to deliver_to(debate.author) - expect(email).to have_body_text(debate_path(debate)) + email = open_last_email + expect(email).to have_subject('Someone has commented on your debate') + expect(email).to deliver_to(debate.author) + expect(email).to have_body_text(debate_path(debate)) + end + + 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 - scenario "Comment reply", :js do - user = create(:user, email_on_comment_reply: true) - reply_to(user) + context 'Comment replies' do + scenario "Send email on comment reply", :js do + user = create(:user, email_on_comment_reply: true) + reply_to(user) - 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)) + 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)) + end + + scenario "Do not send email about own replies to own comments", :js do + user = create(:user, email_on_comment_reply: true) + reply_to(user, user: user) + + expect { open_last_email }.to raise_error "No email has been sent!" + end + + scenario "Do not send email about comment reply unless set in preferences", :js do + user = create(:user, email_on_comment_reply: false) + reply_to(user) + + expect { open_last_email }.to raise_error "No email has been sent!" + end 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 - - scenario "Do not send email about comment reply unless set in preferences", :js do - user = create(:user, email_on_comment_reply: false) - reply_to(user) - - expect { open_last_email }.to raise_error "No email has been sent!" - end - -end \ No newline at end of file +end diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index 61974864d..d7f2106a7 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -44,8 +44,8 @@ module CommonActions click_button 'Send me reset password instructions' end - def comment_on(debate) - user = create(:user) + def comment_on(debate, options = {}) + user = options.fetch(:user) { create(:user) } login_as(user) visit debate_path(debate) @@ -56,10 +56,10 @@ module CommonActions expect(page).to have_content 'Have you thought about...?' end - def reply_to(user) - manuela = create(:user) + def reply_to(original_user, options = {}) + manuela = options.fetch(:user) { create(:user) } debate = create(:debate) - comment = create(:comment, commentable: debate, user: user) + comment = create(:comment, commentable: debate, user: original_user) login_as(manuela) visit debate_path(debate)