Skip to content

Commit

Permalink
Merge pull request #5293 from solidusio/elia/admin/connect-auth-to-si…
Browse files Browse the repository at this point in the history
…debar

[Admin] connect auth helpers to the sidebar
  • Loading branch information
elia authored Aug 1, 2023
2 parents a90918b + 64ebbed commit c08ab05
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SolidusAdmin::Sidebar::AccountNav::Component < SolidusAdmin::BaseComponent
# @param account_path [String]
# @param logout_path [String]
# @param logout_method [Symbol]
def initialize(user_label: "Alice Doe", account_path: "#", logout_path: "#", logout_method: :delete)
def initialize(user_label:, account_path:, logout_path:, logout_method:)
@user_label = user_label
@account_path = account_path
@logout_path = logout_path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
</ul>
</div>
<div class="mt-auto">
<%= render @account_nav_component.new %>
<%= render @account_nav_component.new(
user_label: helpers.current_solidus_admin_user.email,
account_path: solidus_admin.account_path,
logout_path: helpers.solidus_admin_logout_path,
logout_method: helpers.solidus_admin_logout_method,
) %>
</div>
</nav>
6 changes: 1 addition & 5 deletions admin/app/controllers/solidus_admin/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module SolidusAdmin::Auth
extend ActiveSupport::Concern

included do
if ::SolidusAdmin::Config.authentication_adapter
include ::SolidusAdmin::Config.authentication_adapter.constantize
end

before_action :authenticate_solidus_admin_user!

helper_method :current_solidus_admin_user
Expand All @@ -26,7 +22,7 @@ def current_solidus_admin_user
end

def solidus_admin_logout_path
send SolidusAdmin::Config.logout_link_path
SolidusAdmin::Config.logout_link_path
end

def solidus_admin_logout_method
Expand Down
2 changes: 2 additions & 0 deletions admin/app/controllers/solidus_admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class BaseController < ApplicationController
include ::SolidusAdmin::Auth
include Spree::Core::ControllerHelpers::Store

include SolidusAdmin::AuthAdapters::Backend if defined?(Spree::Backend)

include ::GearedPagination::Controller

layout 'solidus_admin/application'
Expand Down
6 changes: 1 addition & 5 deletions admin/lib/solidus_admin/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,10 @@ def main_nav
preference :current_user_method, :string, default: :spree_current_user

# The path used to logout the user in the admin interface.
preference :logout_link_path, :string, default: :admin_logout_path
preference :logout_link_path, :string, default: '/admin/logout'

# The HTTP method used to logout the user in the admin interface.
preference :logout_link_method, :string, default: :delete

# A module that will be included in the BaseController to add authentication support
# methods, can be `nil` if no module is needed.
preference :authentication_adapter, :string, default: 'SolidusAdmin::AuthAdapters::Backend'
end
end

Expand Down
9 changes: 9 additions & 0 deletions admin/lib/solidus_admin/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ module ControllerHelper
extend ActiveSupport::Concern

included do
include SolidusAdmin::Auth
helper ActionView::Helpers
helper SolidusAdmin::ContainerHelper
helper_method :current_component
end

private

def spree_current_user
Spree::LegacyUser.new(email: "[email protected]")
end

def authenticate_solidus_backend_user!
# noop
end

def current_component
@current_component ||= begin
# Lookbook sets the @preview instance variable with a PreviewEntity instance, while ViewComponent uses the preview class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
<div class="flex h-screen">
<div class="flex h-4/5">
<div class="self-end">
<%= render current_component.new(
user_label: "Alice Doe"
) %>
</div>
<div class="mb-8">
<h6 class="text-gray-500 mb-3 mt-0">
Short name
</h6>

<div class="w-[17.78rem] pt-20 rounded border-dotted border-2 border-gray-300">
<%= render current_component.new(
user_label: "Alice Doe",
account_path: "#account",
logout_path: "#logout",
logout_method: :delete
) %>
</div>
<div class="flex h-4/5">
<div class="self-end">
<%= render current_component.new(
user_label: "Alice Supercalifragilisticexpialidocious"
) %>
</div>
</div>

<div class="mb-8">
<h6 class="text-gray-500 mb-3 mt-0">
Long name
</h6>

<div class="w-[17.78rem] pt-20 rounded border-dotted border-2 border-gray-300">
<%= render current_component.new(
user_label: "Alice Supercalifragilisticexpialidocious",
account_path: "#account",
logout_path: "#logout",
logout_method: :delete
) %>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,23 @@
render_preview(:overview)
end

it "renders link to the account" do
it "renders correctly" do
component = described_class.new(
account_path: "/admin/account"
user_label: "Alice",
account_path: "/admin/account",
logout_path: "/admin/logout",
logout_method: :delete,
)

render_inline(component)

expect(page).to have_link("Account", href: "/admin/account", visible: :any)
end

it "renders link to logout" do
component = described_class.new(
logout_path: "/admin/logout"
)

render_inline(component)

expect(page).to have_link("Logout", href: "/admin/logout", visible: :any)
end

it "renders user lanel" do
component = described_class.new(
user_label: "Alice"
)

render_inline(component)
aggregate_failures do
expect(page).to have_content("Alice")

expect(page).to have_content("Alice")
# Links are hidden within a <details> element
expect(page).to have_link("Account", href: "/admin/account", visible: :any)
expect(page).to have_link("Logout", href: "/admin/logout", visible: :any)
expect(page.find_link("Logout", visible: :any)["data-method"]).to eq("delete")
end
end
end
2 changes: 2 additions & 0 deletions admin/spec/components/solidus_admin/sidebar/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "spec_helper"

RSpec.describe SolidusAdmin::Sidebar::Component, type: :component do
before { allow(vc_test_controller).to receive(:spree_current_user).and_return(build(:user)) }

it "renders the overview preview" do
render_preview(:overview)
end
Expand Down
1 change: 1 addition & 0 deletions admin/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
Spree::TestingSupport::FactoryBot.add_paths_and_load!

# VIEW COMPONENTS
Rails.application.config.view_component.test_controller = "SolidusAdmin::BaseController"
require "view_component/test_helpers"
require "view_component/system_test_helpers"

Expand Down

0 comments on commit c08ab05

Please sign in to comment.