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 `Projects::SetAttributesServices` to use columns in projects
Aaron Contreras4cb9e124db8b
diff --git a/app/services/projects/set_attributes_service.rb b/app/services/projects/set_attributes_service.rbindex 6336539c163..970af5ae2b7 100644--- a/app/services/projects/set_attributes_service.rb+++ b/app/services/projects/set_attributes_service.rb@@ -31,12 +31,9 @@ module Projectsprivatedef set_attributes(params)- attributes = params.dup- status_attributes = attributes.delete(:status) || {}+ ret = super(params.except(:status_code))- ret = super(attributes)-- update_status(status_attributes)+ set_status_code(params[:status_code]) if status_code_provided?(params)retend@@ -71,41 +68,29 @@ module Projects.distinctend- def update_status(attributes)- with_hack_around_faulty_enum(attributes) do |safe_attributes|- if model.status- model.status.attributes = safe_attributes- else- model.build_status(safe_attributes)- end- end+ def status_code_provided?(params)+ params.key?(:status_code)end- # Hack around ArgumentError on faulty enum values- # https://github.com/rails/rails/issues/13971- def with_hack_around_faulty_enum(attributes)- faulty_code = if faulty_code?(attributes)- attributes.delete(:code)- end-- yield(attributes)-- if faulty_code+ def set_status_code(status_code)+ if faulty_code?(status_code)# set an arbitrary status code first to get rails internal into correct state- model.status.code = first_not_set_code+ model.status_code = first_not_set_code# hack into rails internals to set faulty code- code_attributes = model.status.instance_variable_get(:@attributes)['code']- code_attributes.instance_variable_set(:@value_before_type_cast, faulty_code)- code_attributes.instance_variable_set(:@value, faulty_code)+ code_attributes = model.instance_variable_get(:@attributes)['status_code']+ code_attributes.instance_variable_set(:@value_before_type_cast, status_code)+ code_attributes.instance_variable_set(:@value, status_code)+ else+ model.status_code = status_codeendend- def faulty_code?(attributes)- attributes && attributes[:code] && Projects::Status.codes.keys.exclude?(attributes[:code].to_s)+ def faulty_code?(status_code)+ status_code && Project.status_codes.keys.exclude?(status_code.to_s)enddef first_not_set_code- (Projects::Status.codes.keys - [model.status.code]).first+ (Project.status_codes.keys - [model.status_code]).firstendendenddiff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rbindex 18b4dfce9b4..90ff280d2ea 100644--- a/spec/services/projects/set_attributes_service_spec.rb+++ b/spec/services/projects/set_attributes_service_spec.rb@@ -103,6 +103,20 @@ describe Projects::SetAttributesService, type: :model do.not_to have_received(:save)end+ shared_examples 'setting status attributes' do+ let(:status_explanation) { 'A magic dwells in each beginning.' }++ it 'sets the project status code' do+ expect(subject.result.status_code)+ .to eq status_code+ end++ it 'sets the project status explanation' do+ expect(subject.result.status_explanation)+ .to eq status_explanation+ end+ end+context 'for a new record' dolet(:project) doProject.new@@ -264,123 +278,49 @@ describe Projects::SetAttributesService, type: :model doenddescribe 'project status' do- context 'with a value provided' do+ context 'with valid status attributes' do+ let(:status_code) { 'on_track' }let(:call_attributes) do{- status: {- code: 'on_track',- explanation: 'A magic dwells in each beginning.'- }+ status_code:,+ status_explanation:}end- it 'set the project status code' do- expect(subject.result.status.code)- .to eql 'on_track'- end+ include_examples 'setting status attributes'+ end- it 'set the project status explanation' do- expect(subject.result.status.explanation)- .to eql 'A magic dwells in each beginning.'+ context 'with an invalid status code provided' do+ let(:status_code) { 'wrong' }+ let(:call_attributes) do+ {+ status_code:,+ status_explanation:+ }end- it 'does not persist the status' do- expect(subject.result.status)- .to be_new_record- end+ include_examples 'setting status attributes'endendendcontext 'for an existing project' dodescribe 'project status' do- context 'with the project not having a status before' do- context 'with a value provided' do- let(:call_attributes) do- {- status: {- code: 'on_track',- explanation: 'A magic dwells in each beginning.'- }- }- end-- it 'set the project status code' do- expect(subject.result.status.code)- .to eql 'on_track'- end-- it 'set the project status explanation' do- expect(subject.result.status.explanation)- .to eql 'A magic dwells in each beginning.'- end-- it 'does not persist the status' do- expect(subject.result.status)- .to be_new_record- end- end-- context 'with an invalid code' do- let(:call_attributes) do- {- status: {- code: 'bogus',- explanation: 'A magic dwells in each beginning.'- }- }- end-- it 'set the project status code' do- expect(subject.result.status.code)- .to eql 'bogus'- end-- it 'set the project status explanation' do- expect(subject.result.status.explanation)- .to eql 'A magic dwells in each beginning.'- end-- it 'does not persist the status' do- expect(subject.result.status)- .to be_new_record- end- end+ let(:project) do+ build_stubbed(:project, :on_track)end- context 'with the project having a status before' do- let(:project_status) do- build_stubbed(:project_status)- end- let(:project) do- build_stubbed(:project, status: project_status)+ context 'with a value provided' do+ let(:status_code) { 'at_risk' }+ let(:status_explanation) { 'Still some magic there.' }+ let(:call_attributes) do+ {+ status_code:,+ status_explanation:+ }end- context 'with a value provided' do- let(:call_attributes) do- {- status: {- code: 'at_risk',- explanation: 'Still some magic there.'- }- }- end-- it 'set the project status code' do- expect(subject.result.status.code)- .to eql 'at_risk'- end-- it 'set the project status explanation' do- expect(subject.result.status.explanation)- .to eql 'Still some magic there.'- end-- it 'does not persist the status' do- expect(subject.result.status)- .to be_changed- end- end+ include_examples 'setting status attributes'endendend