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 ProjectRepresenter to stop referencing Projects::Status attributes
Aaron Contreras3656e31b20eb
diff --git a/app/models/project.rb b/app/models/project.rbindex c2754797d7a..7dcedbde5d0 100644--- a/app/models/project.rb+++ b/app/models/project.rb@@ -149,8 +149,6 @@ class Project < ApplicationRecordfriendly_id :identifier, use: :finders- delegate :explanation, to: :status, allow_nil: true, prefix: true-scope :has_module, ->(mod) {where(["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s])}diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rbindex cfabe28b6c7..3cb4def0416 100644--- a/lib/api/v3/projects/project_representer.rb+++ b/lib/api/v3/projects/project_representer.rb@@ -39,32 +39,10 @@ module APIinclude API::Decorators::FormattablePropertyextend ::API::V3::Utilities::CustomFieldInjector::RepresenterClass- cached_representer key_parts: %i(status),- disabled: false+ cached_representer disabled: falseself_link- def from_hash(body)- # Representable is broken when passing nil as parameters- # it will set the property :status and :statusExplanation- # regardless of what the setter actually does- # Bug opened at https://github.com/trailblazer/representable/issues/234- super(body).tap do |struct|- next unless struct.respond_to?(:status_attributes)-- # Set the status attribute properly- struct.status = struct.status_attributes-- # Remove temporary attributes workaround- struct.delete(:status_attributes)-- # Remove nil status_explanation when passed as nil- if struct.respond_to?(:status_explanation)- struct.delete(:status_explanation)- end- end- end-link :createWorkPackage,cache_if: -> { current_user_allowed_to(:add_work_packages, context: represented) } do{@@ -212,16 +190,16 @@ module APIresource :status,getter: ->(*) {- next unless represented.status&.code+ next unless represented.status_code::API::V3::Projects::Statuses::StatusRepresenter- .create(represented.status.code, current_user:, embed_links:)+ .create(represented.status_code, current_user:, embed_links:)},link: ->(*) {- if represented.status&.code+ if represented.status_code{- href: api_v3_paths.project_status(represented.status.code),- title: I18n.t(:"activerecord.attributes.projects/status.codes.#{represented.status.code}",+ href: api_v3_paths.project_status(represented.status_code),+ title: I18n.t(:"activerecord.attributes.project.status_codes.#{represented.status_code}",default: nil)}.compactelse@@ -231,28 +209,15 @@ module APIend},setter: ->(fragment:, represented:, **) {- represented.status_attributes ||= API::ParserStruct.new-- link = ::API::Decorators::LinkObject.new(represented.status_attributes,+ link = ::API::Decorators::LinkObject.new(represented,path: :project_status,- property_name: :status,- getter: :code,- setter: :'code=')-+ property_name: :status_code,+ setter: :'status_code=')link.from_hash(fragment)}- property :status_explanation,- writable: -> { represented.writable?(:status) },- getter: ->(*) {- ::API::Decorators::Formattable.new(status&.explanation,- object: self,- plain: false)- },- setter: ->(fragment:, represented:, **) {- represented.status_attributes ||= API::ParserStruct.new- represented.status_attributes[:explanation] = fragment["raw"]- }+ formattable_property :status_explanation,+ writable: -> { represented.writable?(:status) }def _type'Project'diff --git a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_representer_rendering_spec.rbindex 4fbf633d3c1..bd97da4fc33 100644--- a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb+++ b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb@@ -35,9 +35,10 @@ describe API::V3::Projects::ProjectRepresenter, 'rendering' dolet(:project) dobuild_stubbed(:project,+ :on_track,+ status_explanation: 'some explanation',parent: parent_project,- description: 'some description',- status:).tap do |p|+ description: 'some description').tap do |p|allow(p).to receive(:available_custom_fields).and_return([int_custom_field, version_custom_field])@@ -56,9 +57,6 @@ describe API::V3::Projects::ProjectRepresenter, 'rendering' do.and_return(ancestors)endend- let(:status) do- build_stubbed(:project_status)- endlet(:parent_project) dobuild_stubbed(:project).tap do |parent|allow(parent)@@ -131,7 +129,7 @@ describe API::V3::Projects::ProjectRepresenter, 'rendering' doendit_behaves_like 'formattable property', 'statusExplanation' do- let(:value) { status.explanation }+ let(:value) { project.status_explanation }endit_behaves_like 'has UTC ISO 8601 date and time' do@@ -345,12 +343,13 @@ describe API::V3::Projects::ProjectRepresenter, 'rendering' dodescribe 'status' doit_behaves_like 'has a titled link' dolet(:link) { 'status' }- let(:href) { api_v3_paths.project_status(project.status.code) }- let(:title) { I18n.t(:"activerecord.attributes.projects/status.codes.#{project.status.code}") }+ let(:status_code) { project.status_code }+ let(:href) { api_v3_paths.project_status(status_code) }+ let(:title) { I18n.t(:"activerecord.attributes.project.status_codes.#{status_code}") }end- context 'if the status is nil' do- let(:status) { nil }+ context 'if the status_code is nil' do+ before { project.status_code = nil }it_behaves_like 'has an untitled link' dolet(:link) { 'status' }@@ -640,13 +639,6 @@ describe API::V3::Projects::ProjectRepresenter, 'rendering' doexpect(representer.json_cache_key).not_to eql former_cache_keyend-- it 'changes when the project status is updated' do- project.status.updated_at = 20.seconds.from_now-- expect(representer.json_cache_key)- .not_to eql former_cache_key- endendend