diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 12a1116c..4759ef20 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -55,22 +55,13 @@ class NotificationsController < ApplicationController end def unsubscribe - # TODO will a logged out user be unsubscribed after logging in? - # need to use devise stored_url or whatever - if current_user.nil? - flash[:notice] = 'Continue to unsubscribe from emails by logging in.' - return redirect_to "#{sign_in_path}?redirect_to=#{unsubscribe_notifications_path}" - end + unsubscribe_redirect_if_logged_out! + check_if_already_unsubscribed! + return if performed? # if one of these checks already redirected, we're done - if current_user.emails_allowed == false - return redirect_to edit_user_path(current_user), notice: 'You were already unsubscribed from emails.' - end - - current_user.emails_allowed = false - success = current_user.save - - if success - redirect_to edit_user_path(current_user), notice: 'You will no longer receive emails from Metamaps.' + if current_user.update(emails_allowed: false) + redirect_to edit_user_path(current_user), + notice: 'You will no longer receive emails from Metamaps.' else flash[:alert] = 'Sorry, something went wrong. You have not been unsubscribed from emails.' redirect_to edit_user_path(current_user) @@ -79,6 +70,19 @@ class NotificationsController < ApplicationController private + def unsubscribe_redirect_if_logged_out! + return if current_user.present? + + flash[:notice] = 'Continue to unsubscribe from emails by logging in.' + redirect_to "#{sign_in_path}?redirect_to=#{unsubscribe_notifications_path}" + end + + def check_if_already_unsubscribed! + return if current_user.emails_allowed + + redirect_to edit_user_path(current_user), notice: 'You were already unsubscribed from emails.' + end + def set_receipts @receipts = current_user.mailboxer_notification_receipts end diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 73d6ea73..1c9c0a1e 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -1,24 +1,25 @@ -class Users::SessionsController < Devise::SessionsController - after_action :store_location, only: [:new] +# frozen_string_literal: true +module Users + class SessionsController < Devise::SessionsController + after_action :store_location, only: [:new] - protected + protected - def after_sign_in_path_for(resource) - stored = stored_location_for(User) - return stored if stored + def after_sign_in_path_for(resource) + stored = stored_location_for(User) + return stored if stored - if request.referer&.match(sign_in_url) || request.referer&.match(sign_up_url) - super - else - request.referer || root_path + if request.referer&.match(sign_in_url) || request.referer&.match(sign_up_url) + super + else + request.referer || root_path + end end - end - private + private - def store_location - if params[:redirect_to] - store_location_for(User, params[:redirect_to]) + def store_location + store_location_for(User, params[:redirect_to]) if params[:redirect_to] end end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index c73b28f2..10961836 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -4,6 +4,6 @@ class ApplicationMailer < ActionMailer::Base layout 'mailer' def deliver - fail NotImplementedError('Please use Mailboxer to send your emails.') + raise NotImplementedError('Please use Mailboxer to send your emails.') end end diff --git a/app/models/user.rb b/app/models/user.rb index ef11e5f6..f6fcb60e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -114,11 +114,8 @@ class User < ApplicationRecord # Mailboxer hooks and helper functions def mailboxer_email(_message) - if emails_allowed - email - else - nil - end + return email if emails_allowed + # else return nil, which sends no email end def mailboxer_notifications diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 3b178fcc..2664fc8d 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -16,9 +16,9 @@