Commit a2a7ad29 authored by Alexis Reigel's avatar Alexis Reigel

add project_settings (Project#settings)

parent d6694100
...@@ -232,6 +232,14 @@ class Project < ActiveRecord::Base ...@@ -232,6 +232,14 @@ class Project < ActiveRecord::Base
has_many :project_badges, class_name: 'ProjectBadge' 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 :variables, allow_destroy: true
accepts_nested_attributes_for :project_feature, update_only: true accepts_nested_attributes_for :project_feature, update_only: true
accepts_nested_attributes_for :import_data 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 @@ ActiveRecord::Schema.define(version: 20180418053107) do ...@@ -1504,6 +1504,12 @@ ActiveRecord::Schema.define(version: 20180418053107) do
add_index "project_import_data", ["project_id"], name: "index_project_import_data_on_project_id", using: :btree 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| create_table "project_statistics", force: :cascade do |t|
t.integer "project_id", null: false t.integer "project_id", null: false
t.integer "namespace_id", null: false t.integer "namespace_id", null: false
...@@ -2185,6 +2191,7 @@ ActiveRecord::Schema.define(version: 20180418053107) do ...@@ -2185,6 +2191,7 @@ ActiveRecord::Schema.define(version: 20180418053107) do
add_foreign_key "project_features", "projects", name: "fk_18513d9b92", on_delete: :cascade 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_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_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 "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_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 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 @@ describe Project do ...@@ -115,6 +115,26 @@ describe Project do
expect(subject.boards.size).to eq 1 expect(subject.boards.size).to eq 1
end end
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 end
describe 'modules' do 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