Read about our upcoming Code of Conduct on this issue

Commit e7b30602 authored by Alexis Reigel's avatar Alexis Reigel
Browse files

add project_settings (Project#settings)

parent 1598969f5ae1
......@@ -232,6 +232,14 @@ class Project < ActiveRecord::Base
has_many :project_badges, class_name: 'ProjectBadge'
has_one :settings, -> (project) {
query = where(project_id: project)
query.presence || begin
ProjectSettings.create(project_id: project.id)
query
end
}, class_name: 'ProjectSettings'
accepts_nested_attributes_for :variables, allow_destroy: true
accepts_nested_attributes_for :project_feature, update_only: true
accepts_nested_attributes_for :import_data
......
class ProjectSettings < ActiveRecord::Base
belongs_to :project
end
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class CreateProjectSettings < ActiveRecord::Migration
DOWNTIME = false
def change
create_table :project_settings do |t|
t.references :project, index: true, foreign_key: { on_delete: :cascade }
end
end
end
......@@ -1504,6 +1504,12 @@
add_index "project_import_data", ["project_id"], name: "index_project_import_data_on_project_id", using: :btree
create_table "project_settings", force: :cascade do |t|
t.integer "project_id"
end
add_index "project_settings", ["project_id"], name: "index_project_settings_on_project_id", using: :btree
create_table "project_statistics", force: :cascade do |t|
t.integer "project_id", null: false
t.integer "namespace_id", null: false
......@@ -2185,6 +2191,7 @@
add_foreign_key "project_features", "projects", name: "fk_18513d9b92", on_delete: :cascade
add_foreign_key "project_group_links", "projects", name: "fk_daa8cee94c", on_delete: :cascade
add_foreign_key "project_import_data", "projects", name: "fk_ffb9ee3a10", on_delete: :cascade
add_foreign_key "project_settings", "projects", on_delete: :cascade
add_foreign_key "project_statistics", "projects", on_delete: :cascade
add_foreign_key "protected_branch_merge_access_levels", "protected_branches", name: "fk_8a3072ccb3", on_delete: :cascade
add_foreign_key "protected_branch_push_access_levels", "protected_branches", name: "fk_9ffc86a3d9", on_delete: :cascade
......
FactoryBot.define do
factory :project_settings do
project
end
end
......@@ -115,6 +115,26 @@
expect(subject.boards.size).to eq 1
end
end
describe '#settings' do
it 'creates lazily a settings record when the project does not have one associated' do
project = create :project
expect(ProjectSettings.count).to eq 0
expect(project.settings).to be_a ProjectSettings
expect(ProjectSettings.count).to eq 1
end
it 'returns the associated record when the project has one associated' do
project = create :project, settings: create(:project_settings)
expect(ProjectSettings.count).to eq 1
expect(project.settings).to be_a ProjectSettings
expect(ProjectSettings.count).to eq 1
end
end
end
describe 'modules' 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