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

[#66641] Show project attributes in sections in the mobile app

Dombi Attilac4d1a50c9a9d
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ef0e75545cd..26524eb58f6 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1116,6 +1116,7 @@ en:
active_value:
true: "unarchived"
false: "archived"
+ attribute_groups: "Attribute Groups"
description: "Description"
enabled_modules: "Enabled modules"
identifier: "Identifier"
diff --git a/lib/api/v3/projects/schemas/project_custom_field_section_representer.rb b/lib/api/v3/projects/schemas/project_custom_field_section_representer.rb
new file mode 100644
index 00000000000..c73a92e6b5c
--- /dev/null
+++ b/lib/api/v3/projects/schemas/project_custom_field_section_representer.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module API
+ module V3
+ module Projects
+ module Schemas
+ class ProjectCustomFieldSectionRepresenter < ::API::Decorators::Single
+ property :name,
+ exec_context: :decorator
+
+ property :attributes,
+ exec_context: :decorator
+
+ def _type
+ "ProjectFormCustomFieldSection"
+ end
+
+ delegate :name, to: :represented
+
+ def attributes
+ represented.custom_fields.map do |cf|
+ convert_property(cf.attribute_name)
+ end
+ end
+
+ def convert_property(attribute)
+ ::API::Utilities::PropertyNameConverter.from_ar_name(attribute)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/projects/schemas/project_schema_representer.rb b/lib/api/v3/projects/schemas/project_schema_representer.rb
index 4eec612be78..d2744a762c6 100644
--- a/lib/api/v3/projects/schemas/project_schema_representer.rb
+++ b/lib/api/v3/projects/schemas/project_schema_representer.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -107,8 +109,75 @@ module API
schema :updated_at,
type: "DateTime"
- def self.represented_class
- ::Project
+ # Cannot be cached here due to custom field visibility checks on a user level.
+ # However caching is still applied further down in the `section_representation` method.
+ property :attribute_groups,
+ name_source: ->(*) { I18n.t("activerecord.attributes.project.attribute_groups") },
+ type: "[]String",
+ as: "_attributeGroups",
+ exec_context: :decorator,
+ uncacheable: true
+
+ class << self
+ def represented_class
+ ::Project
+ end
+
+ def attribute_group(property)
+ lambda do
+ key = property.to_s
+ attribute_group_map key
+ end
+ end
+ end
+
+ def attribute_groups
+ project_custom_field_sections.map do |section|
+ section_representation(section)
+ end
+ end
+
+ ##
+ # Return a map of attribute => group name
+ def attribute_group_map(key)
+ return nil if project_custom_field_sections.empty?
+
+ @attribute_group_map ||= project_custom_field_sections.each_with_object({}) do |section, hash|
+ section.custom_fields.each do |cf|
+ hash[cf.attribute_name(:camel_case)] = section.name
+ end
+ end
+
+ @attribute_group_map[key]
+ end
+
+ private
+
+ def project_custom_field_sections
+ @project_custom_field_sections ||= ProjectCustomFieldSection
+ .joins(:custom_fields)
+ .includes(:custom_fields)
+ .merge(ProjectCustomField.visible(current_user))
+ .group(:id, "custom_fields.id")
+ .order(:position, :position_in_custom_field_section)
+ end
+
+ def section_representation(section)
+ OpenProject::Cache.fetch(*section_cache_key(section)) do
+ ::JSON::parse(::API::V3::Projects::Schemas::ProjectCustomFieldSectionRepresenter
+ .new(section, current_user:, embed_links: true)
+ .to_json)
+ end
+ end
+
+ def section_cache_key(section)
+ ["project_schema_custom_field_section",
+ section.id,
+ I18n.locale,
+ represented.model,
+ represented.model.available_custom_fields.sort_by(&:id)]
+ .flatten
+ .compact
end
end
end
diff --git a/spec/lib/api/v3/projects/schemas/project_schema_representer_spec.rb b/spec/lib/api/v3/projects/schemas/project_schema_representer_spec.rb
index 6a56cf07283..287e076df1a 100644
--- a/spec/lib/api/v3/projects/schemas/project_schema_representer_spec.rb
+++ b/spec/lib/api/v3/projects/schemas/project_schema_representer_spec.rb
@@ -102,6 +102,73 @@ RSpec.describe API::V3::Projects::Schemas::ProjectSchemaRepresenter do
end
end
+ describe "_attributeGroups" do
+ context "without existing sections" do
+ it "renders an empty section attribute group" do
+ expect(subject)
+ .to be_json_eql([].to_json)
+ .at_path("_attributeGroups")
+ end
+ end
+
+ context "with existing sections" do
+ let!(:user_section) { create(:project_custom_field_section, name: "Common Project Attributes") }
+ let!(:user_custom_field) do
+ create(:text_project_custom_field, project_custom_field_section: user_section)
+ end
+ let!(:admin_section) { create(:project_custom_field_section, name: "Admin Project Attributes") }
+ let!(:admin_custom_field) do
+ create(:text_project_custom_field, project_custom_field_section: admin_section, admin_only: true)
+ end
+
+ before do
+ allow(contract.model)
+ .to receive(:available_custom_fields)
+ .and_return(ProjectCustomField.all)
+ end
+
+ context "with a user" do
+ it "renders section attribute group elements of the schema" do
+ expect(subject)
+ .to be_json_eql(
+ {
+ _type: "ProjectFormCustomFieldSection",
+ name: "Common Project Attributes",
+ attributes: ["customField#{user_custom_field.id}"]
+ }.to_json
+ )
+ .at_path("_attributeGroups/0")
+ end
+ end
+
+ context "with an admin" do
+ let(:current_user) { build_stubbed(:admin) }
+
+ it "renders section attribute group elements of the schema" do
+ expect(subject)
+ .to be_json_eql(
+ {
+ _type: "ProjectFormCustomFieldSection",
+ name: "Common Project Attributes",
+ attributes: ["customField#{user_custom_field.id}"]
+ }.to_json
+ )
+ .at_path("_attributeGroups/0")
+
+ expect(subject)
+ .to be_json_eql(
+ {
+ _type: "ProjectFormCustomFieldSection",
+ name: "Admin Project Attributes",
+ attributes: ["customField#{admin_custom_field.id}"]
+ }.to_json
+ )
+ .at_path("_attributeGroups/1")
+ end
+ end
+ end
+ end
+
describe "id" do
let(:path) { "id" }