From 852b5613c5eda43a9dce20ca9a71301f5630b1c6 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 31 Jul 2018 12:31:15 -0700 Subject: Fix namespaces_helper reporting multiple groups --- app/helpers/namespaces_helper.rb | 7 +++++-- spec/helpers/namespaces_helper_spec.rb | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/helpers/namespaces_helper.rb b/app/helpers/namespaces_helper.rb index 9008db1b300..19aa630007d 100644 --- a/app/helpers/namespaces_helper.rb +++ b/app/helpers/namespaces_helper.rb @@ -14,8 +14,11 @@ module NamespacesHelper extra_group = Group.find(extra_group) if Namespace.find(extra_group).kind == 'group' end - if extra_group && extra_group.is_a?(Group) && (!Group.exists?(name: extra_group.name) || Ability.allowed?(current_user, :read_group, extra_group)) - groups |= [extra_group] + if extra_group && extra_group.is_a?(Group) + # Avoid duplicate groups if one already exists by that name + existing_group = Group.find_by(name: extra_group.name) + extra_group = existing_group if existing_group + groups |= [extra_group] if Ability.allowed?(current_user, :read_group, extra_group) end options = [] diff --git a/spec/helpers/namespaces_helper_spec.rb b/spec/helpers/namespaces_helper_spec.rb index 343e140f5fb..b12b2587510 100644 --- a/spec/helpers/namespaces_helper_spec.rb +++ b/spec/helpers/namespaces_helper_spec.rb @@ -31,6 +31,15 @@ describe NamespacesHelper do expect(options).to include(user.name) end + it 'avoids duplicate groups when extra_group is used' do + allow(helper).to receive(:current_user).and_return(admin) + + options = helper.namespaces_options(user_group.id, display_path: true, extra_group: build(:group, name: admin_group.name)) + + expect(options.scan("data-name=\"#{admin_group.name}\"").count).to eq(1) + expect(options).to include(admin_group.name) + end + it 'returns only groups if groups_only option is true' do allow(helper).to receive(:current_user).and_return(user) -- cgit v1.2.1