From 9d4af0dcd3733d43a17d3c4254c2426a2f77663f Mon Sep 17 00:00:00 2001 From: Andrew Kvalheim Date: Thu, 31 Mar 2022 17:30:50 -0700 Subject: [PATCH 1/2] Simplify `User.comment_notifiable` scope --- app/jobs/event_comment_mail_job.rb | 4 +--- app/models/comment.rb | 8 ++++---- app/models/user.rb | 2 +- spec/models/user_spec.rb | 4 ++-- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/jobs/event_comment_mail_job.rb b/app/jobs/event_comment_mail_job.rb index bcc46ca7ea..08f273b915 100644 --- a/app/jobs/event_comment_mail_job.rb +++ b/app/jobs/event_comment_mail_job.rb @@ -4,9 +4,7 @@ class EventCommentMailJob < ApplicationJob queue_as :default def perform(comment) - conference = comment.commentable.program.conference - - User.comment_notifiable(conference).each do |user| + User.comment_notifiable(comment.conference_id).each do |user| Mailbot.event_comment_mail(comment, user).deliver_now end end diff --git a/app/models/comment.rb b/app/models/comment.rb index ee1a07a8ea..9cc0007f2f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -57,13 +57,13 @@ def self.find_commentable(commentable_str, commentable_id) commentable_str.constantize.find(commentable_id) end + def conference_id + commentable.program.conference_id + end + private def send_notification EventCommentMailJob.perform_later(self) end - - def conference_id - commentable.program.conference_id - end end diff --git a/app/models/user.rb b/app/models/user.rb index 993d489280..16d9eda4e9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -37,7 +37,7 @@ def for_registration conference after_save :touch_events # add scope - scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference.id)} + scope :comment_notifiable, ->(conference_id) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference_id)} # scopes for user distributions scope :recent, lambda { diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 29bb59cb50..218035334b 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -88,11 +88,11 @@ let(:cfp_user) { create(:user, role_ids: [cfp_role.id]) } it 'includes organizer and cfp user' do - expect(User.comment_notifiable(conference)).to include(organizer, cfp_user) + expect(User.comment_notifiable(conference.id)).to include(organizer, cfp_user) end it 'excludes ordinary user' do - expect(User.comment_notifiable(conference)).not_to include(user) + expect(User.comment_notifiable(conference.id)).not_to include(user) end end From 18433a6776ed0fdbf4fd977c7849f601742c9f30 Mon Sep 17 00:00:00 2001 From: Andrew Kvalheim Date: Thu, 31 Mar 2022 17:31:08 -0700 Subject: [PATCH 2/2] Add setting to disable email notifications of comments --- app/controllers/admin/emails_controller.rb | 1 + app/models/comment.rb | 6 ++++++ app/views/admin/emails/index.html.haml | 4 ++++ ...401000955_add_send_on_event_comment_to_email_settings.rb | 5 +++++ db/schema.rb | 3 ++- 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220401000955_add_send_on_event_comment_to_email_settings.rb diff --git a/app/controllers/admin/emails_controller.rb b/app/controllers/admin/emails_controller.rb index ba456efe3e..c31dea0d44 100644 --- a/app/controllers/admin/emails_controller.rb +++ b/app/controllers/admin/emails_controller.rb @@ -29,6 +29,7 @@ def email_params :send_on_accepted, :send_on_rejected, :send_on_confirmed_without_registration, :send_on_submitted_proposal, :submitted_proposal_subject, :submitted_proposal_body, + :send_on_event_comment, :registration_subject, :accepted_subject, :rejected_subject, :confirmed_without_registration_subject, :registration_body, :accepted_body, :rejected_body, :confirmed_without_registration_body, :send_on_conference_dates_updated, :conference_dates_updated_subject, :conference_dates_updated_body, diff --git a/app/models/comment.rb b/app/models/comment.rb index 9cc0007f2f..abaf89a9f7 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -63,7 +63,13 @@ def conference_id private + def conference + commentable.program.conference + end + def send_notification + return unless conference.email_settings.send_on_event_comment? + EventCommentMailJob.perform_later(self) end end diff --git a/app/views/admin/emails/index.html.haml b/app/views/admin/emails/index.html.haml index 41a43825ee..03b97e0b42 100644 --- a/app/views/admin/emails/index.html.haml +++ b/app/views/admin/emails/index.html.haml @@ -35,6 +35,10 @@ %a.btn.btn-link.control_label.template_help_link{ 'data-name' => 'registration_help' } Show Help = render partial: 'help', locals: { id: 'registration_help', show_event_variables: false } #proposal.tab-pane{ role: 'tabpanel' } + .checkbox + %label + = f.check_box :send_on_event_comment + Send an email to all organizers and CfP team members when a comment is added? .checkbox %label = f.check_box :send_on_submitted_proposal, data: { name: 'email_settings_proposal_submited_subject'}, class: 'send_on_radio' diff --git a/db/migrate/20220401000955_add_send_on_event_comment_to_email_settings.rb b/db/migrate/20220401000955_add_send_on_event_comment_to_email_settings.rb new file mode 100644 index 0000000000..8f5a9f3e1d --- /dev/null +++ b/db/migrate/20220401000955_add_send_on_event_comment_to_email_settings.rb @@ -0,0 +1,5 @@ +class AddSendOnEventCommentToEmailSettings < ActiveRecord::Migration[7.0] + def change + add_column :email_settings, :send_on_event_comment, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 2ad25c7ea7..b0e19bf349 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20181229233811) do +ActiveRecord::Schema.define(version: 2022_04_01_000955) do create_table "answers", force: :cascade do |t| t.string "title" @@ -192,6 +192,7 @@ t.boolean "send_on_submitted_proposal", default: false t.string "submitted_proposal_subject" t.text "submitted_proposal_body" + t.boolean "send_on_event_comment", default: true end create_table "event_schedules", force: :cascade do |t|