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

Fix rendering of CFs

Oliver Günther0388f403645e
diff --git a/app/components/users/profile/attributes_component.html.erb b/app/components/users/profile/attributes_component.html.erb
index 4f6fe6e83d6..7ea20ddc5dc 100644
--- a/app/components/users/profile/attributes_component.html.erb
+++ b/app/components/users/profile/attributes_component.html.erb
@@ -25,10 +25,14 @@
end
end
- @user.visible_custom_field_values.each do |custom_value|
- if custom_value.value.present?
- details_container.with_row do
- "#{custom_value.custom_field.name}: #{custom_value.formatted_value}"
+ visible_custom_fields.each do |custom_field|
+ details_container.with_row(test_selector: "user-custom-field") do
+ concat "#{custom_field.name}: "
+ value = @user.formatted_custom_value_for(custom_field)
+ if value.is_a?(Array)
+ concat safe_join(value, ", ")
+ else
+ concat value
end
end
end
diff --git a/app/components/users/profile/attributes_component.rb b/app/components/users/profile/attributes_component.rb
index cc22932e683..74cec513682 100644
--- a/app/components/users/profile/attributes_component.rb
+++ b/app/components/users/profile/attributes_component.rb
@@ -46,6 +46,15 @@ module Users
def visible_user_information?
@user.pref.can_expose_mail? || @user.visible_custom_field_values.any? { _1.value.present? }
end
+
+ def visible_custom_fields
+ @user
+ .visible_custom_field_values
+ .select { |cv| cv.value.present? }
+ .group_by(&:custom_field)
+ .keys
+ .sort_by(&:name)
+ end
end
end
end
diff --git a/spec/components/users/profile/attributes_component_spec.rb b/spec/components/users/profile/attributes_component_spec.rb
index 643ff5472a7..6d582df8593 100644
--- a/spec/components/users/profile/attributes_component_spec.rb
+++ b/spec/components/users/profile/attributes_component_spec.rb
@@ -70,9 +70,13 @@ RSpec.describe Users::Profile::AttributesComponent, type: :component do
end
end
- describe "Custom fields" do
+ describe "Custom field" do
let(:custom_field) { create(:user_custom_field, :string, visible:) }
- let(:user) { build_stubbed(:user, custom_values: [build(:custom_value, custom_field:, value: "Hello custom field")]) }
+ let(:custom_values) do
+ [build(:custom_value, custom_field:, value: "Hello custom field")]
+ end
+ let(:visible) { true }
+ let(:user) { build_stubbed(:user, custom_values:) }
current_user { build(:admin) }
@@ -80,12 +84,8 @@ RSpec.describe Users::Profile::AttributesComponent, type: :component do
render_inline(component)
end
- context "when visible" do
- let(:visible) { true }
-
- it "renders the field" do
- expect(page).to have_text("Hello custom field")
- end
+ it "renders the field" do
+ expect(page).to have_text("Hello custom field")
end
context "when not visible" do
@@ -95,5 +95,29 @@ RSpec.describe Users::Profile::AttributesComponent, type: :component do
expect(page).to have_no_text("Hello custom field")
end
end
+
+ context "with multiple custom fields" do
+ let(:list_custom_field) { create(:user_custom_field, :multi_list, visible:, name: "Ze list") }
+ let(:text_custom_field) { create(:user_custom_field, :text, visible:, name: "A portrait") }
+ let(:custom_values) do
+ [
+ build(:custom_value, custom_field: list_custom_field, value: list_custom_field.possible_values[0]),
+ build(:custom_value, custom_field: list_custom_field, value: list_custom_field.possible_values[1]),
+ build(:custom_value, custom_field: list_custom_field, value: list_custom_field.possible_values[2]),
+ build(:custom_value, custom_field: text_custom_field, value: "This is **formatted** text.")
+ ]
+ end
+
+ it "renders the fields correctly and sorted" do
+ # correct render of formattable
+ expect(page).to have_css("strong", text: "formatted")
+ # correct render of multi select
+ expect(page).to have_text("A, B, C")
+ # alphabetical order
+ items = page.all(:test_id, "user-custom-field")
+ expect(items[0]).to have_text("A portrait")
+ expect(items[1]).to have_text("Ze list")
+ end
+ end
end
end