Commit 6c981091 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '341063-use-proper-sti-names-in-code-for-various-namespace-types' into 'master'

Use proper STI names in code for various namespace types

See merge request gitlab-org/gitlab!70547
parents 40988573 0a626e83
......@@ -5,7 +5,7 @@ class CustomEmoji < ApplicationRecord
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
# For now only external emoji are supported. See https://gitlab.com/gitlab-org/gitlab/-/issues/230467
......
......@@ -5,7 +5,7 @@ class CustomerRelations::Contact < ApplicationRecord
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
strip_attributes! :phone, :first_name, :last_name
......
......@@ -5,7 +5,7 @@ class CustomerRelations::Organization < ApplicationRecord
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
......
......@@ -43,7 +43,7 @@ class GroupMember < Member
# Because source_type is `Namespace`...
def real_source_type
'Group'
Group.sti_name
end
def notifiable_options
......
......@@ -138,11 +138,11 @@ class Namespace < ApplicationRecord
class << self
def sti_class_for(type_name)
case type_name
when 'Group'
when Group.sti_name
Group
when 'Project'
when Namespaces::ProjectNamespace.sti_name
Namespaces::ProjectNamespace
when 'User'
when Namespaces::UserNamespace.sti_name
# TODO: We create a normal Namespace until
# https://gitlab.com/gitlab-org/gitlab/-/merge_requests/68894 is ready
Namespace
......
......@@ -9,7 +9,7 @@ module Packages
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
validates :namespace, presence: true
......
......@@ -159,7 +159,7 @@ class Project < ApplicationRecord
# Relations
belongs_to :pool_repository
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
# Sync deletion via DB Trigger to ensure we do not have
# a project without a project_namespace (or vice-versa)
......@@ -852,7 +852,7 @@ class Project < ApplicationRecord
end
def group_ids
joins(:namespace).where(namespaces: { type: 'Group' }).select(:namespace_id)
joins(:namespace).where(namespaces: { type: Group.sti_name }).select(:namespace_id)
end
# Returns ids of projects with issuables available for given user
......
......@@ -167,7 +167,7 @@ RSpec.describe BillingPlansHelper do
end
describe '#use_new_purchase_flow?' do
where type: ['Group', nil],
where type: [Group.sti_name, nil],
plan: Plan.all_plans,
trial_active: [true, false]
......@@ -183,7 +183,7 @@ RSpec.describe BillingPlansHelper do
subject { helper.use_new_purchase_flow?(namespace) }
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)
end
......@@ -349,7 +349,7 @@ RSpec.describe BillingPlansHelper do
let(:plan) { double('Plan') }
it 'is upgradable' do
group = double('Group', upgradable?: true)
group = double(Group.sti_name, upgradable?: true)
expect(helper).to receive(:plan_upgrade_url)
......@@ -357,7 +357,7 @@ RSpec.describe BillingPlansHelper do
end
it 'is purchasable' do
group = double('Group', upgradable?: false)
group = double(Group.sti_name, upgradable?: false)
expect(helper).to receive(:plan_purchase_url)
helper.plan_purchase_or_upgrade_url(group, plan)
......
......@@ -4,7 +4,7 @@ FactoryBot.define do
factory :group, class: 'Group', parent: :namespace do
sequence(:name) { |n| "group#{n}" }
path { name.downcase.gsub(/\s/, '_') }
type { 'Group' }
type { Group.sti_name }
owner { nil }
project_creation_level { ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS }
......
......@@ -7,6 +7,10 @@ RSpec.describe Namespace do
include GitHelpers
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(:gitlab_shell) { Gitlab::Shell.new }
let(:repository_storage) { 'default' }
......@@ -38,20 +42,22 @@ RSpec.describe Namespace do
context 'validating the parent of a namespace' do
using RSpec::Parameterized::TableSyntax
# rubocop:disable Lint/BinaryOperatorWithIdenticalOperands
where(:parent_type, :child_type, :error) do
nil | 'User' | nil
nil | 'Group' | nil
nil | 'Project' | 'must be set for a project namespace'
'Project' | 'User' | 'project namespace cannot be the parent of another namespace'
'Project' | 'Group' | 'project namespace cannot be the parent of another namespace'
'Project' | 'Project' | 'project namespace cannot be the parent of another namespace'
'Group' | 'User' | 'cannot not be used for user namespace'
'Group' | 'Group' | nil
'Group' | 'Project' | nil
'User' | 'User' | 'cannot not be used for user namespace'
'User' | 'Group' | 'user namespace cannot be the parent of another namespace'
'User' | 'Project' | nil
end
nil | ref(:user_sti_name) | nil
nil | ref(:group_sti_name) | nil
nil | ref(:project_sti_name) | 'must be set for a project namespace'
ref(:project_sti_name) | ref(:user_sti_name) | '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'
ref(:project_sti_name) | ref(:project_sti_name) | 'project namespace cannot be the parent of another namespace'
ref(:group_sti_name) | ref(:user_sti_name) | 'cannot not be used for user namespace'
ref(:group_sti_name) | ref(:group_sti_name) | nil
ref(:group_sti_name) | ref(:project_sti_name) | nil
ref(:user_sti_name) | ref(:user_sti_name) | 'cannot not be used for user namespace'
ref(:user_sti_name) | ref(:group_sti_name) | 'user namespace cannot be the parent of another namespace'
ref(:user_sti_name) | ref(:project_sti_name) | nil
end
# rubocop:enable Lint/BinaryOperatorWithIdenticalOperands
with_them do
it 'validates namespace parent' do
......@@ -170,7 +176,7 @@ RSpec.describe Namespace do
let(:namespace) { Namespace.find(create(:namespace, type: namespace_type, parent: parent).id) }
context 'creating a Group' do
let(:namespace_type) { 'Group' }
let(:namespace_type) { group_sti_name }
it 'is valid' do
expect(namespace).to be_a(Group)
......@@ -180,7 +186,7 @@ RSpec.describe Namespace do
end
context 'creating a ProjectNamespace' do
let(:namespace_type) { 'Project' }
let(:namespace_type) { project_sti_name }
let(:parent) { create(:group) }
it 'is valid' do
......@@ -191,7 +197,7 @@ RSpec.describe Namespace do
end
context 'creating a UserNamespace' do
let(:namespace_type) { 'User' }
let(:namespace_type) { user_sti_name }
it 'is valid' do
# 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