Commit 0a626e83 authored by Brett Walker's avatar Brett Walker Committed by Jan Provaznik

Convert usage of hardcoded namespace types

into their STI equivalent names.  For example,
`type: 'Group'` would become `type: Group.sti_name`

This is because we are now overriding the STI name for namespaces.  So
we should really use those values rather than hardcoding them.
parent 7fb79dba
...@@ -5,7 +5,7 @@ class CustomEmoji < ApplicationRecord ...@@ -5,7 +5,7 @@ class CustomEmoji < ApplicationRecord
belongs_to :namespace, inverse_of: :custom_emoji belongs_to :namespace, inverse_of: :custom_emoji
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'namespace_id' belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'namespace_id'
belongs_to :creator, class_name: "User", inverse_of: :created_custom_emoji belongs_to :creator, class_name: "User", inverse_of: :created_custom_emoji
# For now only external emoji are supported. See https://gitlab.com/gitlab-org/gitlab/-/issues/230467 # For now only external emoji are supported. See https://gitlab.com/gitlab-org/gitlab/-/issues/230467
......
...@@ -5,7 +5,7 @@ class CustomerRelations::Contact < ApplicationRecord ...@@ -5,7 +5,7 @@ class CustomerRelations::Contact < ApplicationRecord
self.table_name = "customer_relations_contacts" self.table_name = "customer_relations_contacts"
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'group_id' belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'group_id'
belongs_to :organization, optional: true belongs_to :organization, optional: true
strip_attributes! :phone, :first_name, :last_name strip_attributes! :phone, :first_name, :last_name
......
...@@ -5,7 +5,7 @@ class CustomerRelations::Organization < ApplicationRecord ...@@ -5,7 +5,7 @@ class CustomerRelations::Organization < ApplicationRecord
self.table_name = "customer_relations_organizations" self.table_name = "customer_relations_organizations"
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'group_id' belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'group_id'
strip_attributes! :name strip_attributes! :name
......
...@@ -43,7 +43,7 @@ class GroupMember < Member ...@@ -43,7 +43,7 @@ class GroupMember < Member
# Because source_type is `Namespace`... # Because source_type is `Namespace`...
def real_source_type def real_source_type
'Group' Group.sti_name
end end
def notifiable_options def notifiable_options
......
...@@ -138,11 +138,11 @@ class Namespace < ApplicationRecord ...@@ -138,11 +138,11 @@ class Namespace < ApplicationRecord
class << self class << self
def sti_class_for(type_name) def sti_class_for(type_name)
case type_name case type_name
when 'Group' when Group.sti_name
Group Group
when 'Project' when Namespaces::ProjectNamespace.sti_name
Namespaces::ProjectNamespace Namespaces::ProjectNamespace
when 'User' when Namespaces::UserNamespace.sti_name
# TODO: We create a normal Namespace until # TODO: We create a normal Namespace until
# https://gitlab.com/gitlab-org/gitlab/-/merge_requests/68894 is ready # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/68894 is ready
Namespace Namespace
......
...@@ -9,7 +9,7 @@ module Packages ...@@ -9,7 +9,7 @@ module Packages
mount_file_store_uploader Packages::Composer::CacheUploader mount_file_store_uploader Packages::Composer::CacheUploader
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'namespace_id' belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'namespace_id'
belongs_to :namespace belongs_to :namespace
validates :namespace, presence: true validates :namespace, presence: true
......
...@@ -159,7 +159,7 @@ class Project < ApplicationRecord ...@@ -159,7 +159,7 @@ class Project < ApplicationRecord
# Relations # Relations
belongs_to :pool_repository belongs_to :pool_repository
belongs_to :creator, class_name: 'User' belongs_to :creator, class_name: 'User'
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'namespace_id' belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'namespace_id'
belongs_to :namespace belongs_to :namespace
# Sync deletion via DB Trigger to ensure we do not have # Sync deletion via DB Trigger to ensure we do not have
# a project without a project_namespace (or vice-versa) # a project without a project_namespace (or vice-versa)
...@@ -851,7 +851,7 @@ class Project < ApplicationRecord ...@@ -851,7 +851,7 @@ class Project < ApplicationRecord
end end
def group_ids def group_ids
joins(:namespace).where(namespaces: { type: 'Group' }).select(:namespace_id) joins(:namespace).where(namespaces: { type: Group.sti_name }).select(:namespace_id)
end end
# Returns ids of projects with issuables available for given user # Returns ids of projects with issuables available for given user
......
...@@ -167,7 +167,7 @@ RSpec.describe BillingPlansHelper do ...@@ -167,7 +167,7 @@ RSpec.describe BillingPlansHelper do
end end
describe '#use_new_purchase_flow?' do describe '#use_new_purchase_flow?' do
where type: ['Group', nil], where type: [Group.sti_name, nil],
plan: Plan.all_plans, plan: Plan.all_plans,
trial_active: [true, false] trial_active: [true, false]
...@@ -183,7 +183,7 @@ RSpec.describe BillingPlansHelper do ...@@ -183,7 +183,7 @@ RSpec.describe BillingPlansHelper do
subject { helper.use_new_purchase_flow?(namespace) } subject { helper.use_new_purchase_flow?(namespace) }
it do it do
result = type == 'Group' && (plan == Plan::FREE || trial_active) result = type == Group.sti_name && (plan == Plan::FREE || trial_active)
is_expected.to be(result) is_expected.to be(result)
end end
...@@ -349,7 +349,7 @@ RSpec.describe BillingPlansHelper do ...@@ -349,7 +349,7 @@ RSpec.describe BillingPlansHelper do
let(:plan) { double('Plan') } let(:plan) { double('Plan') }
it 'is upgradable' do it 'is upgradable' do
group = double('Group', upgradable?: true) group = double(Group.sti_name, upgradable?: true)
expect(helper).to receive(:plan_upgrade_url) expect(helper).to receive(:plan_upgrade_url)
...@@ -357,7 +357,7 @@ RSpec.describe BillingPlansHelper do ...@@ -357,7 +357,7 @@ RSpec.describe BillingPlansHelper do
end end
it 'is purchasable' do it 'is purchasable' do
group = double('Group', upgradable?: false) group = double(Group.sti_name, upgradable?: false)
expect(helper).to receive(:plan_purchase_url) expect(helper).to receive(:plan_purchase_url)
helper.plan_purchase_or_upgrade_url(group, plan) helper.plan_purchase_or_upgrade_url(group, plan)
......
...@@ -4,7 +4,7 @@ FactoryBot.define do ...@@ -4,7 +4,7 @@ FactoryBot.define do
factory :group, class: 'Group', parent: :namespace do factory :group, class: 'Group', parent: :namespace do
sequence(:name) { |n| "group#{n}" } sequence(:name) { |n| "group#{n}" }
path { name.downcase.gsub(/\s/, '_') } path { name.downcase.gsub(/\s/, '_') }
type { 'Group' } type { Group.sti_name }
owner { nil } owner { nil }
project_creation_level { ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS } project_creation_level { ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS }
......
...@@ -7,6 +7,10 @@ RSpec.describe Namespace do ...@@ -7,6 +7,10 @@ RSpec.describe Namespace do
include GitHelpers include GitHelpers
include ReloadHelpers include ReloadHelpers
let_it_be(:group_sti_name) { Group.sti_name }
let_it_be(:project_sti_name) { Namespaces::ProjectNamespace.sti_name }
let_it_be(:user_sti_name) { Namespaces::UserNamespace.sti_name }
let!(:namespace) { create(:namespace, :with_namespace_settings) } let!(:namespace) { create(:namespace, :with_namespace_settings) }
let(:gitlab_shell) { Gitlab::Shell.new } let(:gitlab_shell) { Gitlab::Shell.new }
let(:repository_storage) { 'default' } let(:repository_storage) { 'default' }
...@@ -38,20 +42,22 @@ RSpec.describe Namespace do ...@@ -38,20 +42,22 @@ RSpec.describe Namespace do
context 'validating the parent of a namespace' do context 'validating the parent of a namespace' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
# rubocop:disable Lint/BinaryOperatorWithIdenticalOperands
where(:parent_type, :child_type, :error) do where(:parent_type, :child_type, :error) do
nil | 'User' | nil nil | ref(:user_sti_name) | nil
nil | 'Group' | nil nil | ref(:group_sti_name) | nil
nil | 'Project' | 'must be set for a project namespace' nil | ref(:project_sti_name) | 'must be set for a project namespace'
'Project' | 'User' | 'project namespace cannot be the parent of another namespace' ref(:project_sti_name) | ref(:user_sti_name) | 'project namespace cannot be the parent of another namespace'
'Project' | 'Group' | 'project namespace cannot be the parent of another namespace' ref(:project_sti_name) | ref(:group_sti_name) | 'project namespace cannot be the parent of another namespace'
'Project' | 'Project' | 'project namespace cannot be the parent of another namespace' ref(:project_sti_name) | ref(:project_sti_name) | 'project namespace cannot be the parent of another namespace'
'Group' | 'User' | 'cannot not be used for user namespace' ref(:group_sti_name) | ref(:user_sti_name) | 'cannot not be used for user namespace'
'Group' | 'Group' | nil ref(:group_sti_name) | ref(:group_sti_name) | nil
'Group' | 'Project' | nil ref(:group_sti_name) | ref(:project_sti_name) | nil
'User' | 'User' | 'cannot not be used for user namespace' ref(:user_sti_name) | ref(:user_sti_name) | 'cannot not be used for user namespace'
'User' | 'Group' | 'user namespace cannot be the parent of another namespace' ref(:user_sti_name) | ref(:group_sti_name) | 'user namespace cannot be the parent of another namespace'
'User' | 'Project' | nil ref(:user_sti_name) | ref(:project_sti_name) | nil
end end
# rubocop:enable Lint/BinaryOperatorWithIdenticalOperands
with_them do with_them do
it 'validates namespace parent' do it 'validates namespace parent' do
...@@ -170,7 +176,7 @@ RSpec.describe Namespace do ...@@ -170,7 +176,7 @@ RSpec.describe Namespace do
let(:namespace) { Namespace.find(create(:namespace, type: namespace_type, parent: parent).id) } let(:namespace) { Namespace.find(create(:namespace, type: namespace_type, parent: parent).id) }
context 'creating a Group' do context 'creating a Group' do
let(:namespace_type) { 'Group' } let(:namespace_type) { group_sti_name }
it 'is valid' do it 'is valid' do
expect(namespace).to be_a(Group) expect(namespace).to be_a(Group)
...@@ -180,7 +186,7 @@ RSpec.describe Namespace do ...@@ -180,7 +186,7 @@ RSpec.describe Namespace do
end end
context 'creating a ProjectNamespace' do context 'creating a ProjectNamespace' do
let(:namespace_type) { 'Project' } let(:namespace_type) { project_sti_name }
let(:parent) { create(:group) } let(:parent) { create(:group) }
it 'is valid' do it 'is valid' do
...@@ -191,7 +197,7 @@ RSpec.describe Namespace do ...@@ -191,7 +197,7 @@ RSpec.describe Namespace do
end end
context 'creating a UserNamespace' do context 'creating a UserNamespace' do
let(:namespace_type) { 'User' } let(:namespace_type) { user_sti_name }
it 'is valid' do it 'is valid' do
# TODO: We create a normal Namespace until # TODO: We create a normal Namespace until
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment