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
refactor: Extract BasicData::ModelSeeder class
Christophe Bliardea7cbd3f1cd0
diff --git a/app/seeders/basic_data/activity_seeder.rb b/app/seeders/basic_data/activity_seeder.rbindex b7ec60afa99..f1d9ff61636 100644--- a/app/seeders/basic_data/activity_seeder.rb+++ b/app/seeders/basic_data/activity_seeder.rb@@ -26,25 +26,16 @@# See COPYRIGHT and LICENSE files for more details.#++module BasicData- class ActivitySeeder < Seeder- def seed_data!- TimeEntryActivity.transaction do- seed_data.each('time_entry_activities') do |activity_data|- TimeEntryActivity.create(- name: activity_data['name'],- position: activity_data['position'],- is_default: true?(activity_data['is_default'])- )- end- end- end-- def applicable?- TimeEntryActivity.none?- end+ class ActivitySeeder < ModelSeeder+ self.model_class = TimeEntryActivity+ self.seed_data_model_key = 'time_entry_activities'- def not_applicable_message- 'Skipping activities as there are already some configured'+ def model_attributes(activity_data)+ {+ name: activity_data['name'],+ is_default: true?(activity_data['is_default']),+ position: activity_data['position']+ }endendenddiff --git a/app/seeders/basic_data/model_seeder.rb b/app/seeders/basic_data/model_seeder.rbnew file mode 100644index 00000000000..88ae5f1cc0a--- /dev/null+++ b/app/seeders/basic_data/model_seeder.rb@@ -0,0 +1,67 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#+++module BasicData+ class ModelSeeder < Seeder+ class_attribute :model_class+ class_attribute :seed_data_model_key++ def seed_data!+ model_class.transaction do+ Array(seed_data.lookup(seed_data_model_key)).each do |model_data|+ model = model_class.create!(model_attributes(model_data))+ seed_data.store_reference(model_data['reference'], model)+ end+ end+ end++ def model_attributes(model_data)+ raise NotImplementedError+ end++ def applicable?+ model_class.none?+ end++ def not_applicable_message+ "Skipping #{model_human_name} as there are already some configured"+ end++ def model_human_name+ seed_data_model_key.humanize(capitalize: false)+ end++ protected++ def color_id(name)+ @color_ids_by_name ||= Color.pluck(:name, :id).to_h+ @color_ids_by_name[name] or raise "Cannot find color #{name}"+ end+ end+enddiff --git a/app/seeders/basic_data/priority_seeder.rb b/app/seeders/basic_data/priority_seeder.rbindex 5730a1734f3..eb19fee25b6 100644--- a/app/seeders/basic_data/priority_seeder.rb+++ b/app/seeders/basic_data/priority_seeder.rb@@ -26,26 +26,11 @@# See COPYRIGHT and LICENSE files for more details.#++module BasicData- class PrioritySeeder < Seeder- def seed_data!- IssuePriority.transaction do- Array(seed_data.lookup('priorities')).each do |priorities_data|- IssuePriority.create!(priority_attributes(priorities_data))- end- end- end-- def applicable?- IssuePriority.none?- end-- def not_applicable_message- 'Skipping priorities as there are already some configured'- end-- private+ class PrioritySeeder < ModelSeeder+ self.model_class = IssuePriority+ self.seed_data_model_key = 'priorities'- def priority_attributes(priority_data)+ def model_attributes(priority_data){name: priority_data['name'],color_id: color_id(priority_data['color_name']),diff --git a/app/seeders/basic_data/status_seeder.rb b/app/seeders/basic_data/status_seeder.rbindex d00e7d6b03f..1a69e6a4b33 100644--- a/app/seeders/basic_data/status_seeder.rb+++ b/app/seeders/basic_data/status_seeder.rb@@ -26,19 +26,11 @@# See COPYRIGHT and LICENSE files for more details.#++module BasicData- class StatusSeeder < Seeder- def seed_data!- Status.transaction do- Array(seed_data.lookup('statuses')).each do |status_data|- status = Status.create!(status_attributes(status_data))- seed_data.store_reference(status_data['reference'], status)- end- end- end-- private+ class StatusSeeder < ModelSeeder+ self.model_class = Status+ self.seed_data_model_key = 'statuses'- def status_attributes(status_data)+ def model_attributes(status_data){name: status_data['name'],color_id: color_id(status_data['color_name']),diff --git a/app/seeders/seeder.rb b/app/seeders/seeder.rbindex 15912937256..2cc469b8680 100644--- a/app/seeders/seeder.rb+++ b/app/seeders/seeder.rb@@ -97,11 +97,6 @@ class SeederSeeder.logger.error messageend- def color_id(name)- @color_ids_by_name ||= Color.pluck(:name, :id).to_h- @color_ids_by_name[name] or raise "Cannot find color #{name}"- end-def without_notifications(&)Journal::NotificationConfiguration.with(false, &)enddiff --git a/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb b/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rbindex f7a591e2769..39a00ec3c5e 100644--- a/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb+++ b/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb@@ -78,17 +78,10 @@ describe RootSeeder,)end- it 'creates workflows' do- expect(Workflow.count).to eq(182)- end-- it 'creates statuses' do- expect(Status.count).to eq(4)- end-- it 'creates time entry activities' do- expect(TimeEntryActivity.count).to eq(3)- end+ include_examples 'it creates records', model: IssuePriority, expected_count: 4+ include_examples 'it creates records', model: Status, expected_count: 4+ include_examples 'it creates records', model: TimeEntryActivity, expected_count: 3+ include_examples 'it creates records', model: Workflow, expected_count: 182enddescribe 'demo data' dodiff --git a/spec/seeders/root_seeder_shared_examples.rb b/spec/seeders/root_seeder_shared_examples.rbindex 5c8c09c537d..21346c9556d 100644--- a/spec/seeders/root_seeder_shared_examples.rb+++ b/spec/seeders/root_seeder_shared_examples.rb@@ -36,3 +36,9 @@ RSpec.shared_examples 'no email deliveries' do.to be_emptyendend++RSpec.shared_examples 'it creates records' do |model:, expected_count:|+ it "creates #{expected_count} records of #{model}" do+ expect(model.count).to eq(expected_count)+ end+enddiff --git a/spec/seeders/root_seeder_standard_edition_spec.rb b/spec/seeders/root_seeder_standard_edition_spec.rbindex 4fd6ff012f3..d76fb43658f 100644--- a/spec/seeders/root_seeder_standard_edition_spec.rb+++ b/spec/seeders/root_seeder_standard_edition_spec.rb@@ -78,17 +78,10 @@ describe RootSeeder,)end- it 'creates workflows' do- expect(Workflow.count).to eq(1172)- end-- it 'creates statuses' do- expect(Status.count).to eq(14)- end-- it 'creates time entry activities' do- expect(TimeEntryActivity.count).to eq(6)- end+ include_examples 'it creates records', model: IssuePriority, expected_count: 4+ include_examples 'it creates records', model: Status, expected_count: 14+ include_examples 'it creates records', model: TimeEntryActivity, expected_count: 6+ include_examples 'it creates records', model: Workflow, expected_count: 1172enddescribe 'demo data' do