Use version groups for the version html autocompleters.

This commit is contained in:
Dombi Attila
2025-02-24 15:18:19 +02:00
parent d215656e51
commit 453d55e089
5 changed files with 45 additions and 98 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ module Filter
end
def custom_field_list_autocomplete_options(filter)
items = if filter.custom_field.field_format == "version"
items = if filter.custom_field.version?
filter.allowed_values.map { |name, id, project_name| { name:, id:, project_name: } }
else
filter.allowed_values.map { |name, id| { name:, id: } }
+9 -76
View File
@@ -64,76 +64,6 @@ module CustomFieldsHelper
]
end
# Return custom field html tag corresponding to its format
def custom_field_tag(name, custom_value) # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity
custom_field = custom_value.custom_field
field_name = "#{name}[custom_field_values][#{custom_field.id}]"
field_id = "#{name}_custom_field_values_#{custom_field.id}"
field_format = OpenProject::CustomFieldFormat.find_by(name: custom_field.field_format)
tag = case field_format.try(:edit_as)
when "date"
angular_component_tag "opce-basic-single-date-picker",
inputs: {
required: custom_field.is_required,
value: custom_value.value,
id: field_id,
name: field_name
}
when "text"
styled_text_area_tag(field_name, custom_value.value, id: field_id, rows: 3, container_class: "-middle",
required: custom_field.is_required)
when "bool"
hidden_tag = hidden_field_tag(field_name, "0")
checkbox_tag = styled_check_box_tag(field_name, "1", custom_value.typed_value, id: field_id,
required: custom_field.is_required)
hidden_tag + checkbox_tag
when "list"
include_blank = !custom_field.is_required? ||
(custom_field.default_value.blank? ? I18n.t(:actionview_instancetag_blank_option) : false)
options = [custom_field.possible_values_options(custom_value.customized), custom_value.value]
styled_select_tag(
field_name,
options_for_select(*options),
id: field_id,
container_class: "-middle",
required: custom_field.is_required,
include_blank:
)
else
styled_text_field_tag(field_name, custom_value.value, id: field_id, container_class: "-middle",
required: custom_field.is_required)
end
tag = content_tag :span, tag, lang: custom_field.name_locale, class: "form--field-container"
if custom_value.errors.empty?
tag
else
ActionView::Base.wrap_with_error_span(tag, custom_value, "value")
end
end
# Return custom field label tag
def custom_field_label_tag(name, custom_value)
content_tag "label", h(custom_value.custom_field.name) +
(custom_value.custom_field.is_required? ? content_tag("span", " *", class: "required") : ""),
for: "#{name}_custom_field_values_#{custom_value.custom_field.id}",
class: "form--label #{custom_value.errors.empty? ? nil : 'error'}",
lang: custom_value.custom_field.name_locale
end
def hidden_custom_field_label_tag(name, custom_value)
content_tag "label", h(custom_value.custom_field.name) +
(custom_value.custom_field.is_required? ? content_tag("span", " *", class: "required") : ""),
for: "#{name}_custom_field_values_#{custom_value.custom_field.id}",
class: "hidden-for-sighted",
lang: custom_value.custom_field.name_locale
end
def blank_custom_field_label_tag(name, custom_field)
content_tag "label", h(custom_field.name) +
(custom_field.is_required? ? content_tag("span", " *", class: "required") : ""),
@@ -141,11 +71,6 @@ module CustomFieldsHelper
class: "form--label"
end
# Return custom field tag with its label tag
def custom_field_tag_with_label(name, custom_value)
custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value)
end
def custom_field_tag_for_bulk_edit(name, custom_field, project = nil) # rubocop:disable Metrics/AbcSize
field_name = "#{name}[custom_field_values][#{custom_field.id}]"
field_id = "#{name}_custom_field_values_#{custom_field.id}"
@@ -173,8 +98,16 @@ module CustomFieldsHelper
unset_label = custom_field.field_format == "user" ? :label_nobody : :label_none
base_options << [I18n.t(unset_label), "none"]
end
possible_values = custom_field.possible_values_options(project)
options = if custom_field.version?
grouped_options_for_select(possible_values.group_by(&:last).to_a)
else
options_for_select(possible_values)
end
styled_select_tag(field_name,
options_for_select(base_options + custom_field.possible_values_options(project)),
options_for_select(base_options) + options,
id: field_id,
multiple: custom_field.multi_value?,
include_blank: I18n.t(:label_no_change_option))
+19 -13
View File
@@ -165,8 +165,10 @@ class CustomField < ApplicationRecord
# You MUST NOT pass a customizable if this CF has any other format
def possible_values(obj = nil)
case field_format
when "user", "version"
possible_values_options(obj).map(&:last)
when "user"
possible_users(obj).pluck(:id).map(&:to_s)
when "version"
possible_versions(obj).pluck(:id).map(&:to_s)
when "list"
custom_options
else
@@ -316,22 +318,26 @@ class CustomField < ApplicationRecord
private
def possible_version_values_options(obj)
def possible_versions(obj)
project = deduce_project(obj)
versions = deduce_versions(project)
deduce_versions(project)
end
versions.includes(:project)
.sort
.map { |u| [u.name, u.id.to_s, u.project.name] }
def possible_version_values_options(obj)
possible_versions(obj).references(:project)
.sort
.map { |u| [u.name, u.id.to_s, u.project.name] }
end
def possible_users(obj)
project = deduce_project(obj)
deduce_principals(project)
end
def possible_user_values_options(obj)
project = deduce_project(obj)
users = deduce_principals(project)
users.select(*user_format_columns, "id", "type")
.sort
.map { |u| [u.name, u.id.to_s] }
possible_users(obj).select(*user_format_columns, "id", "type")
.sort
.map { |u| [u.name, u.id.to_s] }
end
def possible_list_values_options
+3 -1
View File
@@ -1,3 +1,5 @@
# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -51,7 +53,7 @@ module Projects::Versions
Version.shared_with(self)
end
# Returns all versions a work package can be assigned to. Opposed to
# Returns all versions a work package can be assigned to. Opposed to
# #shared_versions this returns an array of Versions, not a scope.
#
# The main benefit is in scenarios where work packages' projects are eager
+13 -7
View File
@@ -83,17 +83,23 @@ class CustomFieldFormBuilder < TabularFormBuilder
end
end
# rubocop:enable Metrics/AbcSize
def custom_field_input_list(field, input_options)
customized = Array(custom_value).first&.customized
possible_options = custom_field.possible_values_options(customized)
select_options = custom_field_select_options_for_object
selected_options = Array(custom_value).map(&:value)
selectable_options = template.options_for_select(possible_options, selected_options)
selectable_options = custom_field_input_list_options(customized, custom_value)
input_options[:multiple] = custom_field.multi_value?
select(field, selectable_options, select_options, input_options).html_safe
select(field, selectable_options, custom_field_select_options_for_object, input_options)
end
def custom_field_input_list_options(customized, selected)
options = custom_field.possible_values_options(customized)
selected_options = Array(selected).map(&:value)
if custom_field.version?
template.grouped_options_for_select(options.group_by(&:last).to_a, selected_options)
else
template.options_for_select(options, selected_options)
end
end
def custom_field_select_options_for_object