OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Look up existing status data and store them as references
Christophe Bliard747c2c0f72e8
diff --git a/app/seeders/basic_data/model_seeder.rb b/app/seeders/basic_data/model_seeder.rbindex 11fb06a8be4..5e784d5e5f7 100644--- a/app/seeders/basic_data/model_seeder.rb+++ b/app/seeders/basic_data/model_seeder.rb@@ -29,8 +29,13 @@#++module BasicDataclass ModelSeeder < Seeder+ # The class of the model for creating records.class_attribute :model_class+ # The key under which the data is found in the seed file.class_attribute :seed_data_model_key+ # The names of the attributes used to lookup an existing model in an already+ # seeded database. Optional.+ class_attribute :attribute_names_for_lookupsdef seed_data!model_class.transaction do@@ -53,6 +58,17 @@ module BasicDatamodel_class.none?end+ def lookup_existing_references+ return if attribute_names_for_lookups.blank?++ models_data.each do |model_data|+ lookup_attributes = model_attributes(model_data).slice(*attribute_names_for_lookups)+ if model = model_class.find_by(lookup_attributes)+ seed_data.store_reference(model_data['reference'], model)+ end+ end+ end+def not_applicable_message"Skipping #{model_human_name} as there are already some configured"enddiff --git a/app/seeders/basic_data/status_seeder.rb b/app/seeders/basic_data/status_seeder.rbindex 42aaca52f22..97806c5cd0d 100644--- a/app/seeders/basic_data/status_seeder.rb+++ b/app/seeders/basic_data/status_seeder.rb@@ -29,6 +29,7 @@ module BasicDataclass StatusSeeder < ModelSeederself.model_class = Statusself.seed_data_model_key = 'statuses'+ self.attribute_names_for_lookups = %i[name is_closed is_default]self.needs = [BasicData::ColorSeeder,BasicData::ColorSchemeSeederdiff --git a/spec/seeders/basic_data/status_seeder_spec.rb b/spec/seeders/basic_data/status_seeder_spec.rbindex 5914caa9512..3078b809a23 100644--- a/spec/seeders/basic_data/status_seeder_spec.rb+++ b/spec/seeders/basic_data/status_seeder_spec.rb@@ -94,6 +94,26 @@ RSpec.describe BasicData::StatusSeeder docreated_status = Status.lastexpect(seed_data.find_reference(:status_closed)).to eq(created_status)end++ context 'when seeding a second time' do+ subject(:second_seeder) { described_class.new(second_seed_data) }++ let(:second_seed_data) { basic_seed_data.merge(Source::SeedData.new(data_hash)) }++ before do+ second_seeder.seed!+ end++ it 'registers existing matching statuses as references in the seed data' do+ # using the first seed data as the expected value+ expect(second_seed_data.find_reference(:status_closed))+ .to eq(seed_data.find_reference(:status_closed))+ expect(second_seed_data.find_reference(:status_new))+ .to eq(seed_data.find_reference(:status_new))+ expect(second_seed_data.find_reference(:status_in_progress))+ .to eq(seed_data.find_reference(:status_in_progress))+ end+ endendcontext 'without statuses defined' do