From 23786b444a3d21fe7aea77d23937d9fece41fcb7 Mon Sep 17 00:00:00 2001 From: Jestin Palamuttam <34907800+jestinjoshi@users.noreply.github.com> Date: Tue, 1 Oct 2024 02:58:15 +0530 Subject: [PATCH] Fix: Escape button not being handled on settings pages (#1210) * fix: escape button handler * feat: location logic for settings page * fix: linting errors * fix: linting error * refactor: settings test --- app/controllers/application_controller.rb | 2 +- app/controllers/concerns/store_location.rb | 31 ++++++++++++++++++++++ app/views/layouts/_sidebar.html.erb | 2 +- app/views/settings/_nav.html.erb | 6 ++--- test/system/settings_test.rb | 2 +- 5 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 app/controllers/concerns/store_location.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2a3427fb..3830648a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,5 @@ class ApplicationController < ActionController::Base - include AutoSync, Authentication, Invitable, SelfHostable + include AutoSync, Authentication, Invitable, SelfHostable, StoreLocation include Pagy::Backend # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. diff --git a/app/controllers/concerns/store_location.rb b/app/controllers/concerns/store_location.rb new file mode 100644 index 00000000..2bf87a64 --- /dev/null +++ b/app/controllers/concerns/store_location.rb @@ -0,0 +1,31 @@ +module StoreLocation + extend ActiveSupport::Concern + + included do + helper_method :previous_path + before_action :store_return_to + after_action :clear_previous_path + end + + def previous_path + session[:return_to] || fallback_path + end + +private + + def store_return_to + if params[:return_to].present? + session[:return_to] = params[:return_to] + end + end + + def clear_previous_path + if request.fullpath == session[:return_to] + session.delete(:return_to) + end + end + + def fallback_path + root_path + end +end diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb index 74cc5c64..0cec2340 100644 --- a/app/views/layouts/_sidebar.html.erb +++ b/app/views/layouts/_sidebar.html.erb @@ -30,7 +30,7 @@