Commit 1dc44ca6 authored by Alexandru Croitor's avatar Alexandru Croitor

Fix create service spec

Remove circular inverse_of between project and project_namespace
to fix create service spec.
parent 26e68dbe
......@@ -4,8 +4,6 @@ module Namespaces
class ProjectNamespace < Namespace
has_one :project, foreign_key: :project_namespace_id, inverse_of: :project_namespace
validates :project, presence: true
def self.sti_name
'Project'
end
......
......@@ -147,7 +147,7 @@ class Project < ApplicationRecord
belongs_to :namespace
# Sync deletion via DB Trigger to ensure we do not have
# a project without a project_namespace (or vice-versa)
belongs_to :project_namespace, autosave: true, class_name: 'Namespaces::ProjectNamespace', foreign_key: 'project_namespace_id', inverse_of: :project
belongs_to :project_namespace, autosave: true, class_name: 'Namespaces::ProjectNamespace', foreign_key: 'project_namespace_id'
alias_method :parent, :namespace
alias_attribute :parent_id, :namespace_id
......@@ -476,7 +476,7 @@ class Project < ApplicationRecord
validates :project_feature, presence: true
validates :namespace, presence: true
validates :project_namespace, presence: true, on: :create, if: -> { self.namespace.blank? || self.root_namespace.project_namespace_creation_enabled? }
validates :project_namespace, presence: true, if: -> { self.namespace && self.root_namespace.project_namespace_creation_enabled? }
validates :name, uniqueness: { scope: :namespace_id }
validates :import_url, public_url: { schemes: ->(project) { project.persisted? ? VALID_MIRROR_PROTOCOLS : VALID_IMPORT_PROTOCOLS },
ports: ->(project) { project.persisted? ? VALID_MIRROR_PORTS : VALID_IMPORT_PORTS },
......@@ -2919,23 +2919,19 @@ class Project < ApplicationRecord
@online_runners_with_tags ||= active_runners.with_tags.online
end
def create_project_namespace
project_namespace = Namespaces::ProjectNamespace.new(project: self)
sync_attributes(project_namespace)
self.project_namespace = project_namespace
end
def ensure_project_namespace_in_sync
if new_record? && !project_namespace
create_project_namespace if !self.namespace || self.root_namespace.project_namespace_creation_enabled?
else
if self.namespace && self.root_namespace.project_namespace_creation_enabled?
if new_record? && !project_namespace
build_project_namespace
sync_attributes(project_namespace)
end
sync_attributes(project_namespace) if sync_project_namespace?
end
end
def sync_project_namespace?
changes.keys & [:name, :path, :namespace_id, :namespace, :visibility_level] && project_namespace.present?
(changes.keys & %w(name path namespace_id namespace visibility_level)).any? && project_namespace.present?
end
def sync_attributes(project_namespace)
......
......@@ -16,7 +16,7 @@ RSpec.describe Project, factory_default: :keep do
describe 'associations' do
it { is_expected.to belong_to(:group) }
it { is_expected.to belong_to(:namespace) }
it { is_expected.to belong_to(:project_namespace).class_name('Namespaces::ProjectNamespace').with_foreign_key('project_namespace_id').inverse_of(:project) }
it { is_expected.to belong_to(:project_namespace).class_name('Namespaces::ProjectNamespace').with_foreign_key('project_namespace_id') }
it { is_expected.to belong_to(:creator).class_name('User') }
it { is_expected.to belong_to(:pool_repository) }
it { is_expected.to have_many(:users) }
......@@ -316,8 +316,8 @@ RSpec.describe Project, factory_default: :keep do
end
it 'validates the visibility' do
expect_any_instance_of(described_class).to receive(:visibility_level_allowed_as_fork).twice.and_call_original
expect_any_instance_of(described_class).to receive(:visibility_level_allowed_by_group).twice.and_call_original
expect_any_instance_of(described_class).to receive(:visibility_level_allowed_as_fork).and_call_original
expect_any_instance_of(described_class).to receive(:visibility_level_allowed_by_group).and_call_original
create(:project)
end
......
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