Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: support rails authentication generator out of the box #3376

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/components/avo/sidebar_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<div
class="avo-sidebar fixed z-[60] t-0 application-sidebar w-64 flex-1 border-r lg:border-none bg-none h-[calc(100dvh-4rem)] bg-application lg:bg-transparent <%= 'print:hidden' if Avo.configuration.hide_layout_when_printing %> <%= 'hidden' unless @sidebar_open %>"
data-sidebar-target="<%= stimulus_target %>"
>
<%= 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 %>
<div class="flex flex-col w-full h-full">
<div class="flex-1 flex flex-col justify-between overflow-auto h-full pt-3 mac-styled-scrollbar">
<div class="space-y-6 mb-4">
Expand Down Expand Up @@ -61,4 +66,4 @@
<% end %>
<%= render Avo::SidebarProfileComponent.new user: helpers._current_user %>
</div>
</div>
<% end %>
16 changes: 8 additions & 8 deletions app/components/avo/sidebar_profile_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
<%= helpers.svg "avo/three-dots", class: 'h-4' %>
</a>
<div
class="hidden absolute flex flex-col inset-auto right-0 -mt-12 bg-white rounded min-w-[200px] shadow-context -translate-y-full z-40"
class="hidden absolute flex flex-col inset-auto right-0 -mt-28 bg-white rounded min-w-[200px] shadow-context z-40"
data-toggle-target="panel"
data-transition-enter="transition ease-out duration-100"
data-transition-enter-start="transform opacity-0 translate-y-1"
data-transition-enter="transition ease-in-out duration-100"
data-transition-enter-start="transform opacity-0 translate-y-3"
data-transition-enter-end="transform opacity-100 translate-y-0"
data-transition-leave="transition ease-in duration-75"
data-transition-leave-start="transform opacity-100 translate-y-0"
data-transition-leave-end="transform opacity-0 translate-y-1"
data-transition-leave-end="transform opacity-0 translate-y-3"
>
<% if Avo.plugin_manager.installed?(:avo_menu) && Avo.has_profile_menu? %>
<div class="text-black space-y-4">
Expand All @@ -40,9 +40,9 @@
</div>
<% end %>
<%= render "avo/partials/profile_menu_extra" %>
<% if can_destroy_user? %>
<%= form_with url: helpers.main_app.send(destroy_user_session_path),
method: :delete,
<% if can_sign_out_user? %>
<%= form_with url: helpers.main_app.send(sign_out_path),
method: sign_out_method,
data: {
controller: :"sign-out",
sign_out_confirm_value: t('avo.are_you_sure'),
Expand All @@ -53,7 +53,7 @@
<%= helpers.svg "avo/logout", class: 'h-4 mr-1' %> <%= t('avo.sign_out') %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
</div>
</div>
25 changes: 19 additions & 6 deletions app/components/avo/sidebar_profile_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -31,13 +33,24 @@ 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 helpers.possibly_rails_authentication?

default_sign_out_path
end

def default_sign_out_path
default_path = :"destroy_#{Avo.configuration.current_user_resource_name}_session_path"

default_path if main_app.respond_to?(default_path)
end

def can_destroy_user?
helpers.main_app.respond_to?(destroy_user_session_path)
def can_sign_out_user?
sign_out_path.present? && main_app.respond_to?(sign_out_path&.to_sym)
end
end
4 changes: 4 additions & 0 deletions app/helpers/avo/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/js/controllers/toggle_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Loading