From 784774d142e4d68a803eb4d51c864aaad7f0c73e Mon Sep 17 00:00:00 2001 From: Kurt Galiatsatos Date: Mon, 7 Dec 2020 19:37:34 -0500 Subject: [PATCH] Allow users to change email reminder preference (#59) This commit adds a new column `receive_reminder_email` to the users table and made it possible for users to view and edit their preference through the application. --- app/controllers/users_controller.rb | 2 +- app/mailers/commitment_reminder_mailer.rb | 15 +++++++----- app/models/user.rb | 2 ++ app/views/users/edit.html.erb | 5 ++++ app/views/users/show.html.erb | 5 ++++ ...225_add_receive_reminder_email_to_users.rb | 5 ++++ db/schema.rb | 3 ++- spec/mailers/commit_reminder_mailer_spec.rb | 24 +++++++++++++++++++ spec/system/user_edits_profile_spec.rb | 4 +++- 9 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20201128221225_add_receive_reminder_email_to_users.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b49d6a9..32eb13b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -30,6 +30,6 @@ def create private def user_params - params.require(:user).permit(:first_name, :last_name, :email, :password) + params.require(:user).permit(:first_name, :last_name, :email, :password, :receive_reminder_email) end end diff --git a/app/mailers/commitment_reminder_mailer.rb b/app/mailers/commitment_reminder_mailer.rb index 79fbcc2..9454805 100644 --- a/app/mailers/commitment_reminder_mailer.rb +++ b/app/mailers/commitment_reminder_mailer.rb @@ -4,7 +4,7 @@ class CommitmentReminderMailer < ApplicationMailer def post_call_reminder(call) @call = call build_commitment_summaries - mail(to: recipients, + mail(to: user_emails, subject: "Commitments This Week", template_name: "reminder") end @@ -12,7 +12,7 @@ def post_call_reminder(call) def mid_week_reminder(call) @call = call build_commitment_summaries - mail(to: recipients, + mail(to: user_emails, subject: "How's this week's commitment going?", template_name: "reminder") end @@ -20,12 +20,15 @@ def mid_week_reminder(call) private def build_commitment_summaries - @user_commitment_summaries = @call - .users + @user_commitment_summaries = call_users .map { |user| UserCommitmentSummary.new(user, @call) } end - def recipients - @call.users.map(&:email) + def user_emails + call_users.map(&:email) + end + + def call_users + @call.users.receives_reminders end end diff --git a/app/models/user.rb b/app/models/user.rb index bf61b33..e73714f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,8 @@ class User < ApplicationRecord has_many :groups, through: :memberships has_many :created_groups, class_name: "Group", foreign_key: :creator_id + scope :receives_reminders, -> { where(receive_reminder_email: true) } + def full_name "#{first_name} #{last_name}" end diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index b17d763..769fad9 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -26,6 +26,11 @@ <%= form.text_field :email %> +
+ <%= form.label :receive_reminder_email %> + <%= form.check_box :receive_reminder_email %> +
+
<%= form.submit %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 3a7d862..af889b0 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -3,6 +3,11 @@
<%= current_user.full_name %>
<%= current_user.email %>
+ <% if current_user.receive_reminder_email %> +
Subscribed to reminder emails
+ <% else %> +
Not subscribed to reminder emails
+ <% end %>