Commit e2eb9487 authored by Toon Claes's avatar Toon Claes

Ensure settings are created when you access them

parent 2d15c330
...@@ -190,7 +190,7 @@ class Project < ApplicationRecord ...@@ -190,7 +190,7 @@ class Project < ApplicationRecord
has_one :error_tracking_setting, inverse_of: :project, class_name: 'ErrorTracking::ProjectErrorTrackingSetting' has_one :error_tracking_setting, inverse_of: :project, class_name: 'ErrorTracking::ProjectErrorTrackingSetting'
has_one :metrics_setting, inverse_of: :project, class_name: 'ProjectMetricsSetting' has_one :metrics_setting, inverse_of: :project, class_name: 'ProjectMetricsSetting'
has_one :grafana_integration, inverse_of: :project has_one :grafana_integration, inverse_of: :project
has_one :settings, inverse_of: :project, class_name: 'ProjectSettings' has_one :settings, ->(project) { where_or_create_by(project: project) }, inverse_of: :project, class_name: 'ProjectSettings'
# Merge Requests for target project should be removed with it # Merge Requests for target project should be removed with it
has_many :merge_requests, foreign_key: 'target_project_id', inverse_of: :target_project has_many :merge_requests, foreign_key: 'target_project_id', inverse_of: :target_project
......
...@@ -4,4 +4,8 @@ class ProjectSettings < ApplicationRecord ...@@ -4,4 +4,8 @@ class ProjectSettings < ApplicationRecord
belongs_to :project, inverse_of: :settings belongs_to :project, inverse_of: :settings
self.primary_key = :project_id self.primary_key = :project_id
def self.where_or_create_by(attrs)
where(primary_key => safe_find_or_create_by(attrs))
end
end end
...@@ -156,6 +156,12 @@ describe Project do ...@@ -156,6 +156,12 @@ describe Project do
expect(project.pages_metadatum).to be_an_instance_of(ProjectPagesMetadatum) expect(project.pages_metadatum).to be_an_instance_of(ProjectPagesMetadatum)
expect(project.pages_metadatum).to be_persisted expect(project.pages_metadatum).to be_persisted
end end
it 'creates settings if needed when accessed' do
expect do
expect(project.settings).to be_persisted
end.to change { ProjectSettings.count }.by(1)
end
end end
context 'updating cd_cd_settings' do context 'updating cd_cd_settings' do
......
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