From a2fd49efb697fffa79767d4b8e3b831f590e6cb6 Mon Sep 17 00:00:00 2001 From: Adrian Marin Date: Fri, 1 Nov 2024 16:11:55 +0200 Subject: [PATCH 1/3] refactor: support rails authentication out of the box --- app/components/avo/sidebar_component.html.erb | 15 ++++++---- .../avo/sidebar_profile_component.html.erb | 16 +++++----- .../avo/sidebar_profile_component.rb | 29 +++++++++++++++---- .../js/controllers/toggle_controller.js | 2 +- spec/dummy/app/avo/resources/bad.rb | 12 -------- 5 files changed, 42 insertions(+), 32 deletions(-) delete mode 100644 spec/dummy/app/avo/resources/bad.rb diff --git a/app/components/avo/sidebar_component.html.erb b/app/components/avo/sidebar_component.html.erb index 0d39a929b7..f43d39f811 100644 --- a/app/components/avo/sidebar_component.html.erb +++ b/app/components/avo/sidebar_component.html.erb @@ -1,7 +1,12 @@ -
+<%= content_tag :div, + class: class_names( + "avo-sidebar fixed z-[60] t-0 application-sidebar w-64 flex-1 border-r lg:border-none bg-none xh-dvh h-[calc(100dvh-4rem)] bg-application lg:bg-transparent", + "print:hidden": Avo.configuration.hide_layout_when_printing, + "hidden": !@sidebar_open, + ), + data: { + sidebar_target: stimulus_target + } do %>
@@ -61,4 +66,4 @@ <% end %> <%= render Avo::SidebarProfileComponent.new user: helpers._current_user %>
-
+<% end %> diff --git a/app/components/avo/sidebar_profile_component.html.erb b/app/components/avo/sidebar_profile_component.html.erb index 1e679aa517..45aa44cd8f 100644 --- a/app/components/avo/sidebar_profile_component.html.erb +++ b/app/components/avo/sidebar_profile_component.html.erb @@ -21,14 +21,14 @@ <%= helpers.svg "avo/three-dots", class: 'h-4' %>
diff --git a/app/components/avo/sidebar_profile_component.rb b/app/components/avo/sidebar_profile_component.rb index e21416267c..ff942a7bee 100644 --- a/app/components/avo/sidebar_profile_component.rb +++ b/app/components/avo/sidebar_profile_component.rb @@ -3,6 +3,8 @@ class Avo::SidebarProfileComponent < Avo::BaseComponent prop :user + delegate :main_app, to: :helpers + def avatar if @user.respond_to?(:avatar) && @user.avatar.present? @user.avatar @@ -31,13 +33,28 @@ def title end end - def destroy_user_session_path - # If `sign_out_path_name` is configured, use it. Otherwise construct the - # path name based on `current_user_resource_name`. - (Avo.configuration.sign_out_path_name || "destroy_#{Avo.configuration.current_user_resource_name}_session_path").to_sym + def sign_out_method + :delete + end + + def sign_out_path + return Avo.configuration.sign_out_path_name if Avo.configuration.sign_out_path_name.present? + return :session_path if possibly_rails_authentication? + + default_sign_out_path + end + + def default_sign_out_path + default_path = "destroy_#{Avo.configuration.current_user_resource_name}_session_path".to_sym + + default_path if main_app.respond_to?(default_path) + end + + def can_sign_out_user? + sign_out_path.present? && main_app.respond_to?(sign_out_path&.to_sym) end - def can_destroy_user? - helpers.main_app.respond_to?(destroy_user_session_path) + def possibly_rails_authentication? + defined?(Authentication) && Authentication.private_instance_methods.include?(:require_authentication) && Authentication.private_instance_methods.include?(:authenticated?) end end diff --git a/app/javascript/js/controllers/toggle_controller.js b/app/javascript/js/controllers/toggle_controller.js index 1041e783f4..eae5d53470 100644 --- a/app/javascript/js/controllers/toggle_controller.js +++ b/app/javascript/js/controllers/toggle_controller.js @@ -22,7 +22,7 @@ export default class extends Controller { if (this.hasPanelTarget) { const isInExemptionContainer = this.hasExemptionContainersValue && this.exemptionContainerTargets.some((container) => container.contains(e.target)) - if (!isInExemptionContainer) { + if (!isInExemptionContainer && !this.panelTarget.classList.contains('hidden')) { leave(this.panelTarget) } } diff --git a/spec/dummy/app/avo/resources/bad.rb b/spec/dummy/app/avo/resources/bad.rb deleted file mode 100644 index 32746317f1..0000000000 --- a/spec/dummy/app/avo/resources/bad.rb +++ /dev/null @@ -1,12 +0,0 @@ -class Avo::Resources::Bad < Avo::BaseResource - self.title = :id - self.includes = [] - # self.search = { - # query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) } - # } - - def fields - field :id, as: :id - # add fields here - end -end From a79a20cad53487ce65b34f65277f3e145bdd7a84 Mon Sep 17 00:00:00 2001 From: Adrian Marin Date: Fri, 1 Nov 2024 21:13:19 +0200 Subject: [PATCH 2/3] lint --- app/components/avo/sidebar_profile_component.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/components/avo/sidebar_profile_component.rb b/app/components/avo/sidebar_profile_component.rb index ff942a7bee..59c8162c5b 100644 --- a/app/components/avo/sidebar_profile_component.rb +++ b/app/components/avo/sidebar_profile_component.rb @@ -45,7 +45,7 @@ def sign_out_path end def default_sign_out_path - default_path = "destroy_#{Avo.configuration.current_user_resource_name}_session_path".to_sym + default_path = :"destroy_#{Avo.configuration.current_user_resource_name}_session_path" default_path if main_app.respond_to?(default_path) end From c3b90f4c7871d127438753ae18cf74ef1ffd5320 Mon Sep 17 00:00:00 2001 From: Adrian Marin Date: Fri, 22 Nov 2024 09:48:20 +0200 Subject: [PATCH 3/3] wip --- app/components/avo/sidebar_profile_component.rb | 6 +----- app/controllers/avo/application_controller.rb | 5 +++++ app/helpers/avo/application_helper.rb | 4 ++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/components/avo/sidebar_profile_component.rb b/app/components/avo/sidebar_profile_component.rb index 59c8162c5b..177e8fa687 100644 --- a/app/components/avo/sidebar_profile_component.rb +++ b/app/components/avo/sidebar_profile_component.rb @@ -39,7 +39,7 @@ def sign_out_method def sign_out_path return Avo.configuration.sign_out_path_name if Avo.configuration.sign_out_path_name.present? - return :session_path if possibly_rails_authentication? + return :session_path if helpers.possibly_rails_authentication? default_sign_out_path end @@ -53,8 +53,4 @@ def default_sign_out_path def can_sign_out_user? sign_out_path.present? && main_app.respond_to?(sign_out_path&.to_sym) end - - def possibly_rails_authentication? - defined?(Authentication) && Authentication.private_instance_methods.include?(:require_authentication) && Authentication.private_instance_methods.include?(:authenticated?) - end end diff --git a/app/controllers/avo/application_controller.rb b/app/controllers/avo/application_controller.rb index 0895cb295d..c818b2e220 100644 --- a/app/controllers/avo/application_controller.rb +++ b/app/controllers/avo/application_controller.rb @@ -6,6 +6,11 @@ class ApplicationController < ::ActionController::Base Avo::ApplicationController.include Pundit end + # # Support Rails default authentication generator + # if defined?(Authentication) + # include Authentication + # end + include Avo::InitializesAvo include Avo::CommonController include Avo::ApplicationHelper diff --git a/app/helpers/avo/application_helper.rb b/app/helpers/avo/application_helper.rb index c9babc1418..d3bc0f216a 100644 --- a/app/helpers/avo/application_helper.rb +++ b/app/helpers/avo/application_helper.rb @@ -141,6 +141,10 @@ def chart_color(index) Avo.configuration.branding.chart_colors[index % Avo.configuration.branding.chart_colors.length] end + def possibly_rails_authentication? + defined?(Authentication) && Authentication.private_instance_methods.include?(:require_authentication) && Authentication.private_instance_methods.include?(:authenticated?) + end + private # Taken from the original library