diff --git a/app/models/my_module_status_flow.rb b/app/models/my_module_status_flow.rb index 2dbb3e019..2cd6e422d 100644 --- a/app/models/my_module_status_flow.rb +++ b/app/models/my_module_status_flow.rb @@ -21,4 +21,22 @@ class MyModuleStatusFlow < ApplicationRecord def final_status my_module_statuses.left_outer_joins(:next_status).find_by('next_statuses_my_module_statuses.id': nil) end + + def self.ensure_default + return if MyModuleStatusFlow.global.any? + + status_flow = MyModuleStatusFlow.create!(name: Extends::DEFAULT_FLOW_NAME, visibility: :global) + prev_id = nil + Extends::DEFAULT_FLOW_STATUSES.each do |status| + new_status = MyModuleStatus.create!(my_module_status_flow: status_flow, + name: status[:name], + color: status[:color], + previous_status_id: prev_id) + prev_id = new_status.id + + status[:conditions]&.each { |condition| condition.constantize.create!(my_module_status: new_status) } + status[:implications]&.each { |implication| implication.constantize.create!(my_module_status: new_status) } + status[:consequences]&.each { |consequence| consequence.constantize.create!(my_module_status: new_status) } + end + end end diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 0caf096d8..25f7c4064 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -317,4 +317,12 @@ class Extends }.freeze DASHBOARD_BLACKLIST_ACTIVITY_TYPES = %i(export_protocol_in_repository copy_protocol_in_repository).freeze + + DEFAULT_FLOW_NAME = 'SciNote Free default task flow' + + DEFAULT_FLOW_STATUSES = [ + { name: 'Not started', color: '#406d86' }, + { name: 'In progress', color: '#0065ff' }, + { name: 'Completed', color: '#00b900' } + ] end diff --git a/db/seeds.rb b/db/seeds.rb index 5f9f93555..ae67f6d2d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,5 +1,7 @@ include UsersGenerator +MyModuleStatusFlow.ensure_default + if User.count.zero? if ENV['ADMIN_NAME'].present? && ENV['ADMIN_EMAIL'].present? && diff --git a/spec/models/my_module_status_flow_spec.rb b/spec/models/my_module_status_flow_spec.rb index 1c2976e4c..cb83f6d02 100644 --- a/spec/models/my_module_status_flow_spec.rb +++ b/spec/models/my_module_status_flow_spec.rb @@ -46,4 +46,21 @@ describe MyModuleStatusFlow, type: :model do it { expect(my_module_team_workflow).to validate_presence_of :team } end end + + describe 'self.ensure_default' do + context 'when there is no global flow' do + it 'adds new global workflow' do + expect { described_class.ensure_default } + .to change { MyModuleStatusFlow.global.count }.by(1).and(change { MyModuleStatus.count }.by(3)) + end + end + + context 'when there is global flow' do + it 'adds new global workflow' do + described_class.ensure_default + + expect { described_class.ensure_default }.not_to(change { MyModuleStatusFlow.global.count }) + end + end + end end