Skip to content

Commit

Permalink
Merge pull request #17625 from opf/fix-required-hierarchy
Browse files Browse the repository at this point in the history
Fix WP creation with required hierarchy CFs
  • Loading branch information
NobodysNightmare authored Jan 16, 2025
2 parents 4472737 + 454ccac commit c436989
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/forms/custom_fields/custom_field_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def form_arguments(custom_field)
# - initial values for user inputs are not displayed
# - allow/disallow-non-open version setting is not yet respected in the version selector
# - rich text editor is not yet supported
# - hierarchy should not use a flat list

def single_value_custom_field_input(builder, custom_field)
form_args = form_arguments(custom_field)
Expand All @@ -83,6 +84,8 @@ def single_value_custom_field_input(builder, custom_field)
CustomFields::Inputs::Int.new(builder, **form_args)
when "float"
CustomFields::Inputs::Float.new(builder, **form_args)
when "hierarchy"
CustomFields::Inputs::SingleSelectList.new(builder, **form_args)
when "list"
CustomFields::Inputs::SingleSelectList.new(builder, **form_args)
when "date"
Expand All @@ -100,6 +103,8 @@ def multi_value_custom_field_input(builder, custom_field)
form_args = form_arguments(custom_field)

case custom_field.field_format
when "hierarchy"
CustomFields::Inputs::MultiSelectList.new(builder, **form_args)
when "list"
CustomFields::Inputs::MultiSelectList.new(builder, **form_args)
when "user"
Expand Down
35 changes: 31 additions & 4 deletions app/forms/custom_fields/inputs/multi_select_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class CustomFields::Inputs::MultiSelectList < CustomFields::Inputs::Base::Autoco
)

custom_value_form.autocompleter(**input_attributes) do |list|
@custom_field.custom_options.each do |custom_option|
list_items.each do |item|
list.option(
label: custom_option.value,
value: custom_option.id,
selected: selected?(custom_option)
label: item.fetch(:label),
value: item.fetch(:value),
selected: item.fetch(:selected)
)
end
end
Expand All @@ -57,6 +57,33 @@ def decorated?
true
end

def list_items
case @custom_field.field_format
when "hierarchy"
hierarchy_items.map do |item|
{
label: item.ancestry_path,
value: item.id,
selected: @custom_values.pluck(:value).map(&:to_i).include?(item.id)
}
end
else
@custom_field.custom_options.map do |custom_option|
{
label: custom_option.value,
value: custom_option.id,
selected: selected?(custom_option)
}
end
end
end

def hierarchy_items
CustomFields::Hierarchy::HierarchicalItemService.new
.get_descendants(item: @custom_field.hierarchy_root, include_self: false)
.value_or([])
end

def selected?(custom_option)
if @custom_values.any?
@custom_values.pluck(:value).map { |value| value&.to_i }.include?(custom_option.id)
Expand Down
36 changes: 33 additions & 3 deletions app/forms/custom_fields/inputs/single_select_list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
Expand Down Expand Up @@ -34,10 +36,11 @@ class CustomFields::Inputs::SingleSelectList < CustomFields::Inputs::Base::Autoc
custom_value_form.hidden(**input_attributes.merge(value: ""))

custom_value_form.autocompleter(**input_attributes) do |list|
@custom_value.custom_field.custom_options.each do |custom_option|
list_items.each do |item|
list.option(
label: custom_option.value, value: custom_option.id,
selected: selected?(custom_option)
label: item.fetch(:label),
value: item.fetch(:value),
selected: item.fetch(:selected)
)
end
end
Expand All @@ -49,6 +52,33 @@ def decorated?
true
end

def list_items
case @custom_field.field_format
when "hierarchy"
hierarchy_items.map do |item|
{
label: item.ancestry_path,
value: item.id,
selected: item.id == @custom_value.value&.to_i
}
end
else
@custom_field.custom_options.map do |custom_option|
{
label: custom_option.value,
value: custom_option.id,
selected: selected?(custom_option)
}
end
end
end

def hierarchy_items
CustomFields::Hierarchy::HierarchicalItemService.new
.get_descendants(item: @custom_field.hierarchy_root, include_self: false)
.value_or([])
end

def selected?(custom_option)
custom_option.id == @custom_value.value&.to_i || custom_option.id == @custom_field.default_value&.to_i
end
Expand Down

0 comments on commit c436989

Please sign in to comment.