Skip to content

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

Refactor ProjectRepresenter to stop referencing Projects::Status attributes

Aaron Contreras3656e31b20eb
diff --git a/app/models/project.rb b/app/models/project.rb
index c2754797d7a..7dcedbde5d0 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -149,8 +149,6 @@ class Project < ApplicationRecord
friendly_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.rb
index cfabe28b6c7..3cb4def0416 100644
--- a/lib/api/v3/projects/project_representer.rb
+++ b/lib/api/v3/projects/project_representer.rb
@@ -39,32 +39,10 @@ module API
include API::Decorators::FormattableProperty
extend ::API::V3::Utilities::CustomFieldInjector::RepresenterClass
- cached_representer key_parts: %i(status),
- disabled: false
+ cached_representer disabled: false
self_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 API
resource :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)
}.compact
else
@@ -231,28 +209,15 @@ module API
end
},
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.rb
index 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' do
let(:project) do
build_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)
end
end
- let(:status) do
- build_stubbed(:project_status)
- end
let(:parent_project) do
build_stubbed(:project).tap do |parent|
allow(parent)
@@ -131,7 +129,7 @@ describe API::V3::Projects::ProjectRepresenter, 'rendering' do
end
it_behaves_like 'formattable property', 'statusExplanation' do
- let(:value) { status.explanation }
+ let(:value) { project.status_explanation }
end
it_behaves_like 'has UTC ISO 8601 date and time' do
@@ -345,12 +343,13 @@ describe API::V3::Projects::ProjectRepresenter, 'rendering' do
describe 'status' do
it_behaves_like 'has a titled link' do
let(: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' do
let(:link) { 'status' }
@@ -640,13 +639,6 @@ describe API::V3::Projects::ProjectRepresenter, 'rendering' do
expect(representer.json_cache_key)
.not_to eql former_cache_key
end
-
- 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
- end
end
end