Skip to content

Commit

Permalink
Enhancement: Subgroup projects searching and pagination (#792)
Browse files Browse the repository at this point in the history
* chore: Working on search in subgroups

* chore: Working on auto-submit

* chore: Just need to figure out flattening on search

* chore: This wasn't needed

* chore: Working search for subgroups and project

* test: This does not need to be there anymore

* chore: Fixed Shared groups and projects content

* test: Update test for pagination

* chore: Fixed flaky test

* chore: Fixed test

* test: Test searching

* chore: Cleaned up methods

* feat: Updated with ability to set the icon size

* chore: Removed the search from the turbo response
  • Loading branch information
joshsadam authored Oct 3, 2024
1 parent c21f739 commit 42d0ffd
Show file tree
Hide file tree
Showing 22 changed files with 161 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<ul class="groups-list namespace-list-tree flex flex-col">
<ul class="flex flex-col groups-list namespace-list-tree">
<%= render NamespaceTree::RowComponent.with_collection(
namespaces,
path: path,
path_args: path_args,
type: @type,
collapsed: collapsed,
render_flat_list: render_flat_list,
icon_size: icon_size,
) %>
</ul>
6 changes: 4 additions & 2 deletions app/components/namespace_tree/namespace_tree_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
module NamespaceTree
# Component to render a namespace tree
class NamespaceTreeComponent < Component
attr_reader :parent, :namespaces, :path, :path_args, :collapsed, :render_flat_list
attr_reader :collapsed, :icon_size, :namespaces, :parent, :path, :path_args, :render_flat_list

# rubocop: disable Metrics/ParameterLists
def initialize(namespaces:, type:, parent: nil, path: nil, path_args: {}, render_flat_list: false)
def initialize(namespaces:, type:, parent: nil, path: nil, path_args: {}, render_flat_list: false,
icon_size: :small)
@parent = parent
@namespaces = namespaces
@path = path
@path_args = path_args
@type = type
@collapsed = true
@render_flat_list = render_flat_list
@icon_size = icon_size
end

# rubocop: enable Metrics/ParameterLists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= viral_icon(name: :squares_2x2, classes: "h-5 w-5 text-slate-400 mr-2") %>
<%= viral_avatar(
name: @namespace.name,
size: :medium,
size: @icon_size,
colour_string: "#{@namespace.name}-#{@namespace.id}",
data: {
turbo: false,
Expand All @@ -13,7 +13,7 @@
<%= viral_icon(name: :rectangle_stack, classes: "h-5 w-5 text-slate-400 mr-2") %>
<%= viral_avatar(
name: @namespace.name,
size: :small,
size: @icon_size,
colour_string: "#{@namespace.name}-#{@namespace.id}",
data: {
turbo: false,
Expand Down
5 changes: 4 additions & 1 deletion app/components/namespace_tree/row/row_contents_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ module NamespaceTree
module Row
# Component for the contents of NamespaceTree row
class RowContentsComponent < Viral::Component
def initialize(namespace:, path: nil, path_args: {}, collapsed: false, sample_count: 0)
# rubocop: disable Metrics/ParameterLists
def initialize(namespace:, path: nil, path_args: {}, collapsed: false, sample_count: 0, icon_size: :small)
@namespace = namespace
@path = path
@path_args = path_args
@collapsed = collapsed
@sample_count = sample_count
@icon_size = icon_size
end
# rubocop: enable Metrics/ParameterLists
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
>
<div
class="
flex
items-center
w-full
py-3
pr-3
text-left
namespace-entry-contents
flex items-center w-full py-3 pr-3 text-left namespace-entry-contents
"
data-action="click->groups--row#toggle"
aria-label="<%= t(:'dashboard.groups.index.row_aria_label', name: @namespace.name) %>"
Expand All @@ -32,7 +26,8 @@
namespace: @namespace,
path: @path,
path_args: @path_args,
collapsed: @collapsed
collapsed: @collapsed,
icon_size: @icon_size,
) %>
</div>
<% unless @collapsed %>
Expand All @@ -41,7 +36,8 @@
namespaces: @children,
path: @path,
path_args: @path_args,
type: @type
type: @type,
icon_size: @icon_size,
) %>
<% end %>
</li>
3 changes: 2 additions & 1 deletion app/components/namespace_tree/row/with_children_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ module Row
# Component for the contents of a namespace row that has children
class WithChildrenComponent < Viral::Component
# rubocop:disable Metrics/ParameterLists
def initialize(namespace:, children:, type:, path: nil, path_args: {}, collapsed: false)
def initialize(namespace:, children:, type:, path: nil, path_args: {}, collapsed: false, icon_size: :small)
@namespace = namespace
@children = children
@type = type
@path = path
@path_args = path_args
@collapsed = collapsed
@icon_size = icon_size
end

# rubocop:enable Metrics/ParameterLists
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<li
class="namespace-entry <%= @namespace.description.present? ? "has-description" : nil %>"
>
<div class="namespace-entry-contents py-3 pr-3 flex items-center">
<span class="folder-toggle-wrap mr-2 flex items-center">
<div class="flex items-center py-3 pr-3 namespace-entry-contents">
<span class="flex items-center mr-2 folder-toggle-wrap">
<%= viral_icon(name: :blank, classes: "h-3 w-3") %>
</span>
<%= render NamespaceTree::Row::RowContentsComponent.new(
namespace: @namespace,
path: @path,
path_args: @path_args,
sample_count: @sample_count,
icon_size: @icon_size,
) %>
</div>
</li>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module NamespaceTree
module Row
# Component for the contents of a namespace row that has no children
class WithoutChildrenComponent < Viral::Component
def initialize(namespace:, path: nil, path_args: {})
def initialize(namespace:, path: nil, path_args: {}, icon_size: :small)
@namespace = namespace
@path = path
@path_args = path_args
@sample_count = @namespace.type == 'Project' ? @namespace.project.samples.size : 0
@icon_size = icon_size
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions app/components/namespace_tree/row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ class RowComponent < ViewComponent::Base

erb_template <<~ERB
<% if @namespace.children_of_type?(@type) && !@render_flat_list %>
<%= render NamespaceTree::Row::WithChildrenComponent.new(namespace: @namespace, type: @type, children: Group.none, path: @path, path_args: @path_args, collapsed: @collapsed) %>
<%= render NamespaceTree::Row::WithChildrenComponent.new(namespace: @namespace, type: @type, children: Group.none, path: @path, path_args: @path_args, collapsed: @collapsed, icon_size: @icon_size) %>
<% else %>
<%= render NamespaceTree::Row::WithoutChildrenComponent.new(namespace: @namespace, path: @path, path_args: @path_args) %>
<%= render NamespaceTree::Row::WithoutChildrenComponent.new(namespace: @namespace, path: @path, path_args: @path_args, icon_size: @icon_size) %>
<% end %>
ERB

# rubocop:disable Metrics/ParameterLists
def initialize(namespace:, type:, path: nil, path_args: {}, collapsed: true, render_flat_list: false)
def initialize(namespace:, type:, path: nil, path_args: {}, collapsed: true, render_flat_list: false,
icon_size: :small)
@namespace = namespace
@type = type
@path = path
@path_args = path_args
@collapsed = collapsed
@render_flat_list = render_flat_list
@icon_size = icon_size
end

# rubocop:enable Metrics/ParameterLists
Expand Down
8 changes: 6 additions & 2 deletions app/components/namespace_tree_container_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
class NamespaceTreeContainerComponent < ViewComponent::Base
erb_template <<-ERB
<div class="namespace-tree-container">
<%= render NamespaceTree::NamespaceTreeComponent.new(namespaces: @namespaces, path: @path, path_args: @path_args, type: @type, render_flat_list: @render_flat_list) %>
<%= render NamespaceTree::NamespaceTreeComponent.new(namespaces: @namespaces, path: @path, path_args: @path_args, type: @type, render_flat_list: @render_flat_list, icon_size: @icon_size) %>
</div>
ERB

def initialize(namespaces:, path: nil, path_args: {}, type: Group.sti_name, render_flat_list: false)
# rubocop: disable Metrics/ParameterLists
def initialize(namespaces:, path: nil, path_args: {}, type: Group.sti_name, render_flat_list: false,
icon_size: :small)
@namespaces = namespaces
@path = path
@path_args = path_args
@type = type
@render_flat_list = render_flat_list
@icon_size = icon_size
end
# rubocop: enable Metrics/ParameterLists
end
3 changes: 1 addition & 2 deletions app/controllers/dashboard/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class GroupsController < ApplicationController
before_action :current_page

def index
@render_flat_list = flat_list_requested?
@q = build_ransack_query
set_default_sort
@pagy, @groups = pagy(@q.result.include_route)
Expand Down Expand Up @@ -54,7 +53,7 @@ def toggle_group
end

def authorized_groups
if @render_flat_list
if flat_list_requested?
authorized_scope(Group, type: :relation)
else
authorized_scope(Group, type: :relation).without_descendants
Expand Down
11 changes: 1 addition & 10 deletions app/controllers/groups/subgroups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ class SubgroupsController < ApplicationController

def index
authorize! @group, to: :read?
respond_to do |format|
format.html { redirect_to group_path(@group) }
format.turbo_stream do
if params.key? :parent_id
render_subgroup
else
@namespaces = namespace_children
end
end
end
render_subgroup
end

private
Expand Down
21 changes: 21 additions & 0 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def index

def show
authorize! @group, to: :read?

@q = if flat_list_requested?
namespace_descendants.ransack(params[:q])
else
namespace_children.ransack(params[:q])
end
set_default_sort
@pagy, @namespaces = pagy(@q.result.include_route)
end

def new
Expand Down Expand Up @@ -105,6 +113,14 @@ def transfer # rubocop:disable Metrics/AbcSize

private

def set_default_sort
@q.sorts = 'created_at desc' if @q.sorts.empty?
end

def flat_list_requested?
params.dig(:q, :name_or_puid_cont).present?
end

def parent_group
@group = Group.find(params[:parent_id]) if params[:parent_id]
end
Expand All @@ -130,6 +146,11 @@ def namespace_children
)
end

def namespace_descendants
@group.self_and_descendants_of_type([Namespaces::ProjectNamespace.sti_name,
Group.sti_name])
end

def resolve_layout
case action_name
when 'show', 'edit', 'update', 'activity'
Expand Down
7 changes: 7 additions & 0 deletions app/models/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ def self_and_descendants
self.class.joins(:route).where(route_path.matches_any([full_path, "#{full_path}/%"]))
end

def self_and_descendants_of_type(types)
route_path = Route.arel_table[:path]

Namespace.joins(:route).where(route_path.matches_any([full_path, "#{full_path}/%"]))
.where(type: types)
end

def self_and_descendant_ids
self_and_descendants.as_ids
end
Expand Down
3 changes: 2 additions & 1 deletion app/views/dashboard/groups/group.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
children: @children,
collapsed: @collapsed,
path: "dashboard_groups_path",
type: [Group.sti_name]
type: [Group.sti_name],
icon_size: :medium,
) %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/dashboard/groups/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
>
<div class="flex flex-row items-center ml-auto space-x-2 font-normal">
<%= search_form_for @q, url: dashboard_groups_url, html: { "data-controller": "filters", "data-turbo-permanent": "true" } do |f| %>
<%= f.hidden_field :format, value: "turbo_stream" %>
<%= f.label :name_or_puid_cont, "SEARCH", class: "sr-only" %>
<div class="relative lg:w-72">
<div
Expand Down Expand Up @@ -56,6 +55,7 @@
namespaces: @groups,
path: "dashboard_groups_path",
render_flat_list: @render_flat_list,
icon_size: :medium,
) %>
<div class="flex flex-row-reverse">
<%= render Viral::Pagy::PaginationComponent.new(
Expand Down
Loading

0 comments on commit 42d0ffd

Please sign in to comment.