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 BaseContract with specs for Create/Update Contract
Aaron Contrerascab006f68f8c
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex 66d76674d9b..32bee318904 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -42,9 +42,10 @@ module Projectsattribute :parent dovalidate_parent_assignableend- attribute :status do+ attribute :status_code dovalidate_status_code_includedend+ attribute :status_explanationattribute :templated dovalidate_templated_set_by_adminend@@ -68,7 +69,7 @@ module Projectsdelegate :assignable_versions, to: :modeldef assignable_status_codes- Projects::Status.codes.keys+ Project.status_codes.keysendprivate@@ -94,7 +95,7 @@ module Projectsenddef validate_status_code_included- errors.add :status, :inclusion if model.status&.code && !Projects::Status.codes.keys.include?(model.status.code.to_s)+ errors.add :status, :inclusion if model.status_code && Project.status_codes.keys.exclude?(model.status_code.to_s)enddef validate_templated_set_by_admindiff --git a/spec/contracts/projects/create_contract_spec.rb b/spec/contracts/projects/create_contract_spec.rbindex be557e0364b..a8f6e1a7b75 100644--- a/spec/contracts/projects/create_contract_spec.rb+++ b/spec/contracts/projects/create_contract_spec.rb@@ -38,7 +38,8 @@ describe Projects::CreateContract doactive: project_active,public: project_public,parent: project_parent,- status: project_status)+ status_code: project_status_code,+ status_explanation: project_status_explanation)endlet(:permissions) { [:add_project] }let!(:allowed_to) dodiff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rbindex 6037b08685e..a17685f5855 100644--- a/spec/contracts/projects/shared_contract_examples.rb+++ b/spec/contracts/projects/shared_contract_examples.rb@@ -43,7 +43,8 @@ shared_examples_for 'project contract' dolet(:project_description) { 'Project description' }let(:project_active) { true }let(:project_public) { true }- let(:project_status) { build_stubbed(:project_status) }+ let(:project_status_code) { 'on_track' }+ let(:project_status_explanation) { 'some explanation' }let(:project_parent) dobuild_stubbed(:project)end@@ -132,17 +133,37 @@ shared_examples_for 'project contract' doendend- context 'if status is nil' do- let(:project_status) { nil }+ context 'if status code is nil' do+ let(:project_status_code) { nil }++ it_behaves_like 'is valid'+ end++ context 'if status explanation is nil' do+ let(:project_status_explanation) { nil }it_behaves_like 'is valid'endcontext 'if status code is invalid' dobefore do- allow(project_status)- .to receive(:code)- .and_return('bogus')+ # Questions for Jens:+ # I'm unsure if this should even be in a spec. It implies a dependency+ # between the SetAttributesService and the Contract which I'm not sure+ # is good. However, it might be warranted as this is only shifting the+ # behavior of what would've happened either way into the Project itself+ # instead of an association which is why it didn't pose the need for hack+ # on this spec.++ # Hack in order to handle setting an Enum value without raising an+ # ArgumentError and letting the Contract perform the validation.+ #+ # This is the behavior that would be expected to be performed by+ # the SetAttributesService at that layer of the flow.+ bogus_project_status_code = 'bogus'+ code_attributes = project.instance_variable_get(:@attributes)['status_code']+ code_attributes.instance_variable_set(:@value_before_type_cast, bogus_project_status_code)+ code_attributes.instance_variable_set(:@value, bogus_project_status_code)endit 'is invalid' dodiff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rbindex 46c33fc4586..1e9f4890927 100644--- a/spec/contracts/projects/update_contract_spec.rb+++ b/spec/contracts/projects/update_contract_spec.rb@@ -35,7 +35,8 @@ describe Projects::UpdateContract dobuild_stubbed(:project,active: project_active,public: project_public,- status: project_status).tap do |p|+ status_code: project_status_code,+ status_explanation: project_status_explanation).tap do |p|# in order to actually have something changedp.name = project_namep.parent = project_parent