From 2bfdc421aecd0f4d27bf8ed27633682ac2b54ef3 Mon Sep 17 00:00:00 2001 From: taitus Date: Tue, 29 Dec 2020 12:14:42 +0100 Subject: [PATCH] Allow updating the status of notifications You can update the same "notifications" section that we allow you to update in "my account". This "subscriptions" section differs from the "my account" section because we do not need to be logged in to update the status of the notifications. --- app/controllers/subscriptions_controller.rb | 14 ++++++++++ config/routes/account.rb | 2 +- spec/system/subscriptions_spec.rb | 30 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 spec/system/subscriptions_spec.rb diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index e5c4fa34a..4ec340cff 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -5,6 +5,12 @@ class SubscriptionsController < ApplicationController def edit end + def update + @user.update!(subscriptions_params) + redirect_to edit_subscriptions_path(token: @user.subscriptions_token), + notice: t("flash.actions.save_changes.notice") + end + private def set_user @@ -14,4 +20,12 @@ class SubscriptionsController < ApplicationController current_user || raise(CanCan::AccessDenied) end end + + def subscriptions_params + params.require(:user).permit(allowed_params) + end + + def allowed_params + [:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter] + end end diff --git a/config/routes/account.rb b/config/routes/account.rb index 128107207..e4baa306e 100644 --- a/config/routes/account.rb +++ b/config/routes/account.rb @@ -2,4 +2,4 @@ resource :account, controller: "account", only: [:show, :update, :delete] do get :erase, on: :collection end -resource :subscriptions, only: [:edit] +resource :subscriptions, only: [:edit, :update] diff --git a/spec/system/subscriptions_spec.rb b/spec/system/subscriptions_spec.rb new file mode 100644 index 000000000..d480f8a38 --- /dev/null +++ b/spec/system/subscriptions_spec.rb @@ -0,0 +1,30 @@ +require "rails_helper" + +describe "Subscriptions" do + let(:user) { create(:user, subscriptions_token: SecureRandom.base58(32)) } + + context "Update" do + scenario "Allow updating the status notification" do + user.update!(email_on_comment: false, + email_on_comment_reply: true, + newsletter: true, + email_digest: false, + email_on_direct_message: true) + visit edit_subscriptions_path(token: user.subscriptions_token) + + check "Notify me by email when someone comments on my proposals or debates" + uncheck "Notify me by email when someone replies to my comments" + uncheck "Receive by email website relevant information" + check "Receive a summary of proposal notifications" + uncheck "Receive emails about direct messages" + click_button "Save changes" + + expect(page).to have_content "Changes saved" + expect(page).to have_field "Notify me by email when someone comments on my contents", checked: true + expect(page).to have_field "Notify me by email when someone replies to my comments", checked: false + expect(page).to have_field "Receive by email website relevant information", checked: false + expect(page).to have_field "Receive a summary of proposal notifications", checked: true + expect(page).to have_field "Receive emails about direct messages", checked: false + end + end +end