From 99de25de57e1bbf6e11f438eaf0fc068358425c0 Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Wed, 13 Sep 2023 03:28:12 +0000 Subject: [PATCH] Now the theme is store dwith the user but also persists in the cookie if the user is logged out --- app/controllers/application_controller.rb | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b87658c3c..2236c152a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -58,14 +58,49 @@ def show_tailwind_theme? # the new theme to be shown even if you're logged out. The feature flag just enables the button # that allows you to do the switching. Cookies are signed so the value can be manipulated by # users outside of pushing the button + u = current_user + if u + r = show_tailwind_theme_user? + # Synchronise the cookie with what's stored with the user + update_tailwind_theme_cookie(r) + r + else + show_tailwind_theme_cookie? + end + end + + sig { returns(T::Boolean) } + def show_tailwind_theme_cookie? cookies.signed[:planningalerts_theme] == "tailwind" end + sig { returns(T::Boolean) } + def show_tailwind_theme_user? + u = current_user + raise unless u + + u.tailwind_theme + end + sig { params(tailwind: T::Boolean).void } def update_tailwind_theme(tailwind) + update_tailwind_theme_cookie(tailwind) + update_tailwind_theme_user(tailwind) + end + + sig { params(tailwind: T::Boolean).void } + def update_tailwind_theme_cookie(tailwind) cookies.signed[:planningalerts_theme] = ("tailwind" if tailwind) end + sig { params(tailwind: T::Boolean).void } + def update_tailwind_theme_user(tailwind) + u = current_user + return unless u + + u.update!(tailwind_theme: tailwind) + end + sig { void } def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:name, :email, :password) }