Skip to content

Commit

Permalink
Allow controller to indicate selected navigation menu item (#234)
Browse files Browse the repository at this point in the history
* first attempt to fix navigation

* fixing selected for creating a new subgroup

* fixing profiles controller

* fixing a test

* changing item_component param from current_page to selected

* fixing selected menu item for groups & subgroups

* removing unnecessary before_action from profiles_controller

* removing redundant database call
  • Loading branch information
ksierks authored Oct 12, 2023
1 parent 2306465 commit 324736f
Show file tree
Hide file tree
Showing 21 changed files with 156 additions and 63 deletions.
4 changes: 2 additions & 2 deletions app/components/layout/sidebar/item_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
hover:bg-slate-200
dark:text-slate-200
dark:hover:bg-slate-700
<%= class_names('Layout-Sidebar__Item--selected bg-slate-200 dark:bg-slate-700': current_page?(url)) %>
<%= class_names('Layout-Sidebar__Item--selected bg-slate-200 dark:bg-slate-700': selected) %>
"
>
<% if current_page?(url) %>
<% if selected %>
<div aria-hidden="true" class="w-1 h-6 my-0 mr-1 rounded-lg bg-primary-600"></div>
<% else %>
<div aria-hidden="true" class="w-1 h-6 my-0 mr-1 bg-transparent"></div>
Expand Down
5 changes: 3 additions & 2 deletions app/components/layout/sidebar/item_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ module Layout
module Sidebar
# Sidebar item component
class ItemComponent < Component
attr_reader :url, :label, :icon
attr_reader :url, :label, :icon, :selected

def initialize(url:, label:, icon: nil)
def initialize(url:, label:, icon: nil, selected: false)
@url = url
@label = label
@icon = icon
@selected = selected
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/dashboard/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Dashboard
# Dashboard groups controller
class GroupsController < ApplicationController
before_action :current_page

def index
@q = Group.ransack(params[:q])
set_default_sort
Expand Down Expand Up @@ -41,5 +43,9 @@ def toggle_group
def load_groups
@groups = authorized_scope(Group, type: :relation).without_descendants
end

def current_page
@current_page = 'groups'
end
end
end
6 changes: 6 additions & 0 deletions app/controllers/dashboard/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Dashboard
# Dashboard Projects Controller
class ProjectsController < ApplicationController
before_action :current_page

def index # rubocop:disable Metrics/AbcSize
@q = Project.ransack(params[:q])
set_default_sort
Expand Down Expand Up @@ -32,5 +34,9 @@ def load_projects(finder_params)
authorized_scope(Project, type: :relation)
end
end

def current_page
@current_page = 'projects'
end
end
end
6 changes: 6 additions & 0 deletions app/controllers/groups/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Groups
class MembersController < Groups::ApplicationController
include MembershipActions

before_action :current_page

def member_params
params.require(:member).permit(:user_id, :access_level, :type, :namespace_id, :created_by_id)
end
Expand Down Expand Up @@ -40,5 +42,9 @@ def member_namespace
@group ||= Group.find_by_full_path(request.params[:group_id]) # rubocop:disable Rails/DynamicFindBy
@group
end

def current_page
@current_page = 'members'
end
end
end
6 changes: 5 additions & 1 deletion app/controllers/groups/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Groups
# Controller actions for Samples within a Group
class SamplesController < ApplicationController
layout 'groups'
before_action :group, only: %i[index]
before_action :group, :current_page, only: %i[index]

def index
authorize! @group, to: :sample_listing?
Expand Down Expand Up @@ -34,5 +34,9 @@ def context_crumbs
}]
end
end

def current_page
@current_page = 'samples'
end
end
end
22 changes: 21 additions & 1 deletion app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# Controller actions for Groups
class GroupsController < Groups::ApplicationController # rubocop:disable Metrics/ClassLength
layout :resolve_layout
before_action :parent_group, only: %i[new]
before_action :group, only: %i[edit show destroy update transfer]
before_action :authorized_namespaces, except: %i[index show destroy]
before_action :current_page

def index
redirect_to dashboard_groups_path
Expand All @@ -29,7 +31,6 @@ def show
end

def new
@group = Group.find(params[:parent_id]) if params[:parent_id]
authorize! @group, to: :create_subgroup? if params[:parent_id]

@new_group = Group.new(parent_id: @group&.id)
Expand Down Expand Up @@ -101,6 +102,10 @@ def transfer # rubocop:disable Metrics/AbcSize

private

def parent_group
@group = Group.find(params[:parent_id]) if params[:parent_id]
end

def group
@group ||= Group.find_by_full_path(request.params[:group_id] || request.params[:id]) # rubocop:disable Rails/DynamicFindBy
end
Expand Down Expand Up @@ -159,6 +164,21 @@ def context_crumbs
end
end

def current_page
@current_page = case action_name
when 'new'
if @group
'details'
else
'groups'
end
when 'show'
'details'
else
'settings'
end
end

protected

def namespace
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/profiles/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ def destroy
@user.destroy
redirect_to new_user_session_url
end

def current_page
@current_page = 'account'
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/profiles/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Profiles
# Base Controller for the users profile
class ApplicationController < ApplicationController
before_action :set_user
before_action :set_user, :current_page

layout 'profiles'

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/profiles/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ def update
def update_password_params
params.require(:user).permit(:password, :password_confirmation, :current_password)
end

def current_page
@current_page = 'password'
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/profiles/personal_access_tokens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ def personal_access_token_params
def active_access_tokens
@active_access_tokens = current_user.personal_access_tokens.active
end

def current_page
@current_page = 'access tokens'
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/profiles/preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ class PreferencesController < Profiles::ApplicationController
def show
authorize! @user, to: :read?
end

def current_page
@current_page = 'preferences'
end
end
end
6 changes: 2 additions & 4 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

# Controller for the user profile page
class ProfilesController < Profiles::ApplicationController
before_action :set_user

# Get the profile page
def show
authorize! @user, to: :read?
Expand Down Expand Up @@ -35,7 +33,7 @@ def update_params
)
end

def set_user
@user = current_user
def current_page
@current_page = 'profile'
end
end
6 changes: 6 additions & 0 deletions app/controllers/projects/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Projects
class MembersController < Projects::ApplicationController
include MembershipActions

before_action :current_page

def member_params
params.require(:member).permit(:user_id, :access_level, :type, :namespace_id, :created_by_id)
end
Expand Down Expand Up @@ -39,5 +41,9 @@ def context_crumbs
def member_namespace
@project.namespace
end

def current_page
@current_page = 'members'
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/projects/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Projects
# Controller actions for Samples
class SamplesController < Projects::ApplicationController
before_action :sample, only: %i[show edit update destroy]
before_action :current_page

def index
authorize! @project, to: :sample_listing?
Expand Down Expand Up @@ -89,5 +90,9 @@ def context_crumbs
path: namespace_project_samples_path
}]
end

def current_page
@current_page = 'samples'
end
end
end
12 changes: 12 additions & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ProjectsController < Projects::ApplicationController # rubocop:disable Met
layout :resolve_layout
before_action :project, only: %i[show edit update activity transfer destroy]
before_action :authorized_namespaces, only: %i[edit new update create transfer]
before_action :current_page

def index
redirect_to dashboard_projects_path
Expand Down Expand Up @@ -166,4 +167,15 @@ def namespace
def namespace_path
namespace_project_path(@namespace.parent, @project)
end

def current_page
@current_page = case action_name
when 'show'
'details'
when 'new'
'projects'
else
'settings'
end
end
end
6 changes: 4 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
<%= render section.with_item(
icon: "rectangle_stack",
label: t(:"general.default_sidebar.projects"),
url: dashboard_projects_path
url: dashboard_projects_path,
selected: @current_page == t(:"general.default_sidebar.projects").downcase
) %>
<%= render section.with_item(
icon: "squares_2x2",
label: t(:"general.default_sidebar.groups"),
url: dashboard_groups_path
url: dashboard_groups_path,
selected: @current_page == t(:"general.default_sidebar.groups").downcase
) %>
<% end %>
<% layout.with_body do %>
Expand Down
12 changes: 8 additions & 4 deletions app/views/layouts/groups.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@
<%= render section.with_item(
url: group_path(@group),
icon: "squares_2x2",
label: t(:"groups.sidebar.details")
label: t(:"groups.sidebar.details"),
selected: @current_page == t(:"groups.sidebar.details").downcase
) %>
<%= render section.with_item(
url: group_samples_path(@group),
icon: "beaker",
label: t(:"projects.sidebar.samples")
label: t(:"projects.sidebar.samples"),
selected: @current_page == t(:"projects.sidebar.samples").downcase
) %>
<%= render section.with_item(
url: group_members_path(@group),
icon: "users",
label: t(:"groups.sidebar.members")
label: t(:"groups.sidebar.members"),
selected: @current_page == t(:"groups.sidebar.members").downcase
) %>
<% if allowed_to?(:update?, @group) %>
<%= render section.with_item(
url: edit_group_path(@group),
icon: "cog_6_tooth",
label: t(:"groups.sidebar.settings")
label: t(:"groups.sidebar.settings"),
selected: @current_page == t(:"groups.sidebar.settings").downcase
) %>
<% end %>
<% end %>
Expand Down
Loading

0 comments on commit 324736f

Please sign in to comment.