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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
refactored datamodeling, reuse custom field forms in new project attribute context
Jonas Jabari029c655b7f36
diff --git a/app/components/settings/project_attributes/section/dialog_body_form_component.rb b/app/components/settings/project_attributes/section/dialog_body_form_component.rbindex 32fdb04726f..b358be6d90d 100644--- a/app/components/settings/project_attributes/section/dialog_body_form_component.rb+++ b/app/components/settings/project_attributes/section/dialog_body_form_component.rb@@ -34,10 +34,10 @@ module Settingsinclude OpPrimer::ComponentHelpersinclude OpTurbo::Streamable- def initialize(section: ProjectCustomFieldSection.new)+ def initialize(project_custom_field_section: ProjectCustomFieldSection.new)super- @project_custom_field_section = section+ @project_custom_field_section = project_custom_field_sectionendprivatediff --git a/app/components/settings/project_attributes/section/project_custom_field_component.rb b/app/components/settings/project_attributes/section/project_custom_field_component.rbdeleted file mode 100644index fe70d54ecb1..00000000000--- a/app/components/settings/project_attributes/section/project_custom_field_component.rb+++ /dev/null@@ -1,85 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2023 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 Settings- module ProjectAttributes- module Section- class ProjectCustomFieldComponent < ApplicationComponent- include ApplicationHelper- include OpPrimer::ComponentHelpers- include OpTurbo::Streamable-- def initialize(project_custom_field:)- super-- @project_custom_field = project_custom_field- end-- private-- def edit_action_item(menu)- menu.with_item(label: t("label_edit"),- href: edit_admin_settings_project_custom_field_path(@project_custom_field)) do |item|- item.with_leading_visual_icon(icon: :pencil)- end- end-- def move_actions(menu)- move_action_item(menu, :highest, t("label_agenda_item_move_to_top"), "move-to-top") unless @project_custom_field.first?- move_action_item(menu, :higher, t("label_agenda_item_move_up"), "chevron-up") unless @project_custom_field.first?- move_action_item(menu, :lower, t("label_agenda_item_move_down"), "chevron-down") unless @project_custom_field.last?- unless @project_custom_field.last?- move_action_item(menu, :lowest, t("label_agenda_item_move_to_bottom"),- "move-to-bottom")- end- end-- def move_action_item(menu, move_to, label_text, icon)- menu.with_item(label: label_text,- href: move_admin_settings_project_custom_field_path(@project_custom_field, move_to:),- form_arguments: {- method: :put, data: { 'turbo-stream': true }- }) do |item|- item.with_leading_visual_icon(icon:)- end- end-- # def delete_action_item(menu)- # menu.with_item(label: t("text_destroy"),- # scheme: :danger,- # href: meeting_agenda_item_path(@meeting_agenda_item.meeting, @meeting_agenda_item),- # form_arguments: {- # method: :delete, data: { confirm: t("text_are_you_sure"), 'turbo-stream': true }- # }) do |item|- # item.with_leading_visual_icon(icon: :trash)- # end- # end- end- end- end-enddiff --git a/app/components/settings/project_attributes/section/project_custom_field_component.html.erb b/app/components/settings/project_custom_field_sections/custom_field_row_component.html.erbsimilarity index 84%rename from app/components/settings/project_attributes/section/project_custom_field_component.html.erbrename to app/components/settings/project_custom_field_sections/custom_field_row_component.html.erbindex 7b5a3bb3bce..dbc4685be72 100644--- a/app/components/settings/project_attributes/section/project_custom_field_component.html.erb+++ b/app/components/settings/project_custom_field_sections/custom_field_row_component.html.erb@@ -7,7 +7,7 @@endcontent_container.with_column dorender(Primer::Beta::Text.new(font_weight: :bold)) do- "#{@project_custom_field.project_custom_field_section_mapping.position} #{@project_custom_field.name}"+ "#{@project_custom_field.position_in_custom_field_section} #{@project_custom_field.name}"endendend@@ -16,7 +16,7 @@menu.with_show_button(icon: "kebab-horizontal", 'aria-label': t("settings.project_attributes.label_project_custom_field_actions"), scheme: :invisible)edit_action_item(menu)move_actions(menu)- # delete_action_item(menu)+ delete_action_item(menu)endendenddiff --git a/app/components/settings/project_custom_field_sections/custom_field_row_component.rb b/app/components/settings/project_custom_field_sections/custom_field_row_component.rbnew file mode 100644index 00000000000..b36e95c8ef7--- /dev/null+++ b/app/components/settings/project_custom_field_sections/custom_field_row_component.rb@@ -0,0 +1,84 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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 Settings+ module ProjectCustomFieldSections+ class CustomFieldRowComponent < ApplicationComponent+ include ApplicationHelper+ include OpPrimer::ComponentHelpers+ include OpTurbo::Streamable++ def initialize(project_custom_field:)+ super++ @project_custom_field = project_custom_field+ end++ private++ def edit_action_item(menu)+ menu.with_item(label: t("label_edit"),+ href: edit_admin_settings_project_custom_field_path(@project_custom_field),+ data: { turbo: "false" }) do |item|+ item.with_leading_visual_icon(icon: :pencil)+ end+ end++ def move_actions(menu)+ move_action_item(menu, :highest, t("label_agenda_item_move_to_top"), "move-to-top") unless @project_custom_field.first?+ move_action_item(menu, :higher, t("label_agenda_item_move_up"), "chevron-up") unless @project_custom_field.first?+ move_action_item(menu, :lower, t("label_agenda_item_move_down"), "chevron-down") unless @project_custom_field.last?+ unless @project_custom_field.last?+ move_action_item(menu, :lowest, t("label_agenda_item_move_to_bottom"),+ "move-to-bottom")+ end+ end++ def move_action_item(menu, move_to, label_text, icon)+ menu.with_item(label: label_text,+ href: move_admin_settings_project_custom_field_path(@project_custom_field, move_to:),+ form_arguments: {+ method: :put, data: { 'turbo-stream': true }+ }) do |item|+ item.with_leading_visual_icon(icon:)+ end+ end++ def delete_action_item(menu)+ menu.with_item(label: t("text_destroy"),+ scheme: :danger,+ href: admin_settings_project_custom_field_path(@project_custom_field),+ form_arguments: {+ method: :delete, data: { confirm: t("text_are_you_sure"), 'turbo-stream': true }+ }) do |item|+ item.with_leading_visual_icon(icon: :trash)+ end+ end+ end+ end+enddiff --git a/app/components/settings/project_attributes/section/index_component.html.erb b/app/components/settings/project_custom_field_sections/index_component.html.erbsimilarity index 64%rename from app/components/settings/project_attributes/section/index_component.html.erbrename to app/components/settings/project_custom_field_sections/index_component.html.erbindex f932849eb99..22c1f3bad99 100644--- a/app/components/settings/project_attributes/section/index_component.html.erb+++ b/app/components/settings/project_custom_field_sections/index_component.html.erb@@ -1,11 +1,11 @@<%=component_wrapper(data: wrapper_data_attributes) doflex_layout(classes: 'dragula-container', data: { 'allowed-drop-type': 'section' }.merge(drop_target_config) ) do |flex|- @sections.each do |section|+ @project_custom_field_sections.each do |section|flex.with_row(data: draggable_item_config(section)) do- render(Settings::ProjectAttributes::SectionComponent.new(section:))+ render(Settings::ProjectCustomFieldSections::ShowComponent.new(project_custom_field_section: section))endendenddiff --git a/app/components/settings/project_attributes/section/index_component.rb b/app/components/settings/project_custom_field_sections/index_component.rbsimilarity index 56%rename from app/components/settings/project_attributes/section/index_component.rbrename to app/components/settings/project_custom_field_sections/index_component.rbindex d72ad581994..c4e6e2098d0 100644--- a/app/components/settings/project_attributes/section/index_component.rb+++ b/app/components/settings/project_custom_field_sections/index_component.rb@@ -27,42 +27,40 @@#++module Settings- module ProjectAttributes- module Section- class IndexComponent < ApplicationComponent- include ApplicationHelper- include OpPrimer::ComponentHelpers- include OpTurbo::Streamable+ module ProjectCustomFieldSections+ class IndexComponent < ApplicationComponent+ include ApplicationHelper+ include OpPrimer::ComponentHelpers+ include OpTurbo::Streamable- def initialize(sections:)- super+ def initialize(project_custom_field_sections:)+ super- @sections = sections- end+ @project_custom_field_sections = project_custom_field_sections+ end- private+ private- def wrapper_data_attributes- {- controller: 'generic-drag-and-drop',- 'application-target': 'dynamic'- }- end+ def wrapper_data_attributes+ {+ controller: 'generic-drag-and-drop',+ 'application-target': 'dynamic'+ }+ end- def drop_target_config- {- 'is-drag-and-drop-target': true,- 'target-allowed-drag-type': 'section' # the type of dragged items which are allowed to be dropped in this target- }- end+ def drop_target_config+ {+ 'is-drag-and-drop-target': true,+ 'target-allowed-drag-type': 'section' # the type of dragged items which are allowed to be dropped in this target+ }+ end- def draggable_item_config(section)- {- 'draggable-id': section.id,- 'draggable-type': 'section',- 'drop-url': drop_admin_settings_project_custom_field_section_path(section)- }- end+ def draggable_item_config(section)+ {+ 'draggable-id': section.id,+ 'draggable-type': 'section',+ 'drop-url': drop_admin_settings_project_custom_field_section_path(section)+ }endendenddiff --git a/app/components/settings/project_attributes/section_component.html.erb b/app/components/settings/project_custom_field_sections/show_component.html.erbsimilarity index 77%rename from app/components/settings/project_attributes/section_component.html.erbrename to app/components/settings/project_custom_field_sections/show_component.html.erbindex 6813016a9d3..fca775745c3 100644--- a/app/components/settings/project_attributes/section_component.html.erb+++ b/app/components/settings/project_custom_field_sections/show_component.html.erb@@ -1,7 +1,7 @@<%=component_wrapper dorender(Primer::Beta::BorderBox.new(mt: 3, data: {- id: @section.id, 'allowed-drop-type': 'custom-field'+ id: @project_custom_field_section.id, 'allowed-drop-type': 'custom-field'}.merge(drag_and_drop_target_config))) do |component|component.with_header(font_weight: :bold) doflex_layout(justify_content: :space_between, align_items: :center) do |section_header_container|@@ -11,7 +11,7 @@endcontent_container.with_column dorender(Primer::Beta::Text.new(font_weight: :bold)) do- "#{@section.position} #{@section.name}"+ "#{@project_custom_field_section.position} #{@project_custom_field_section.name}"endendend@@ -30,17 +30,17 @@endactions_container.with_column dorender(Primer::Alpha::Dialog.new(- id: "project-custom-field-section-dialog#{@section.id}", title: t('settings.project_attributes.label_new_section'),+ id: "project-custom-field-section-dialog#{@project_custom_field_section.id}", title: t('settings.project_attributes.label_new_section'),size: :medium_portrait)) do |dialog|- render(Settings::ProjectAttributes::Section::DialogBodyFormComponent.new(section: @section))+ render(Settings::ProjectAttributes::Section::DialogBodyFormComponent.new(project_custom_field_section: @project_custom_field_section))endendendendendif @project_custom_fields.empty?- component.with_row do+ component.with_row(data: { 'empty-list-item': true }) dorender(Primer::Beta::Text.new(color: :subtle)) { t("settings.project_attributes.label_no_project_custom_fields") }endelse@@ -48,7 +48,7 @@component.with_row(data: draggable_item_config(project_custom_field)) do- render(Settings::ProjectAttributes::Section::ProjectCustomFieldComponent.new(project_custom_field:))+ render(Settings::ProjectCustomFieldSections::CustomFieldRowComponent.new(project_custom_field:))endendenddiff --git a/app/components/settings/project_attributes/section_component.rb b/app/components/settings/project_custom_field_sections/show_component.rbsimilarity index 76%rename from app/components/settings/project_attributes/section_component.rbrename to app/components/settings/project_custom_field_sections/show_component.rbindex caee30afd3c..8001d6e75b2 100644--- a/app/components/settings/project_attributes/section_component.rb+++ b/app/components/settings/project_custom_field_sections/show_component.rb@@ -27,30 +27,30 @@#++module Settings- module ProjectAttributes- class SectionComponent < ApplicationComponent+ module ProjectCustomFieldSections+ class ShowComponent < ApplicationComponentinclude ApplicationHelperinclude OpPrimer::ComponentHelpersinclude OpTurbo::Streamable- def initialize(section:)+ def initialize(project_custom_field_section:)super- @section = section- @project_custom_fields = section.project_custom_fields_ordered_by_postion_in_section+ @project_custom_field_section = project_custom_field_section+ @project_custom_fields = project_custom_field_section.custom_fields.reorder(position_in_custom_field_section: :asc)endprivatedef wrapper_uniq_by- @section.id+ @project_custom_field_section.idenddef drag_and_drop_target_config{'is-drag-and-drop-target': true,'target-container-accessor': '.Box > ul', # the accessor of the container that contains the drag and drop items- 'target-id': @section.id, # the id of the target+ 'target-id': @project_custom_field_section.id, # the id of the target'target-allowed-drag-type': 'custom-field' # the type of dragged items which are allowed to be dropped in this target}end@@ -64,10 +64,16 @@ module Settingsenddef move_actions(menu)- move_action_item(menu, :highest, t("label_agenda_item_move_to_top"), "move-to-top") unless @section.first?- move_action_item(menu, :higher, t("label_agenda_item_move_up"), "chevron-up") unless @section.first?- move_action_item(menu, :lower, t("label_agenda_item_move_down"), "chevron-down") unless @section.last?- unless @section.last?+ unless @project_custom_field_section.first?+ move_action_item(menu, :highest, t("label_agenda_item_move_to_top"),+ "move-to-top")+ end+ move_action_item(menu, :higher, t("label_agenda_item_move_up"), "chevron-up") unless @project_custom_field_section.first?+ unless @project_custom_field_section.last?+ move_action_item(menu, :lower, t("label_agenda_item_move_down"),+ "chevron-down")+ end+ unless @project_custom_field_section.last?move_action_item(menu, :lowest, t("label_agenda_item_move_to_bottom"),"move-to-bottom")end@@ -75,7 +81,7 @@ module Settingsdef move_action_item(menu, move_to, label_text, icon)menu.with_item(label: label_text,- href: move_admin_settings_project_custom_field_section_path(@section, move_to:),+ href: move_admin_settings_project_custom_field_section_path(@project_custom_field_section, move_to:),form_arguments: {method: :put, data: { 'turbo-stream': true }}) do |item|@@ -93,7 +99,7 @@ module Settingsdef edit_action_item(menu)menu.with_item(label: t("text_edit"),tag: :button,- content_arguments: { 'data-show-dialog-id': "project-custom-field-section-dialog#{@section.id}" },+ content_arguments: { 'data-show-dialog-id': "project-custom-field-section-dialog#{@project_custom_field_section.id}" },value: "") do |item|item.with_leading_visual_icon(icon: :pencil)end@@ -102,7 +108,7 @@ module Settingsdef delete_action_item(menu)menu.with_item(label: t("text_destroy"),scheme: :danger,- href: admin_settings_project_custom_field_section_path(@section),+ href: admin_settings_project_custom_field_section_path(@project_custom_field_section),form_arguments: {method: :delete, data: { confirm: t("text_are_you_sure"), 'turbo-stream': true }}) do |item|diff --git a/app/components/settings/project_custom_fields/edit_form_header_component.html.erb b/app/components/settings/project_custom_fields/edit_form_header_component.html.erbindex c8b61164efa..3c223eb5547 100644--- a/app/components/settings/project_custom_fields/edit_form_header_component.html.erb+++ b/app/components/settings/project_custom_fields/edit_form_header_component.html.erb@@ -40,7 +40,7 @@ See COPYRIGHT and LICENSE files for more details.))endtitle_row_container.with_column(mr: 2) do- render(Primer::Beta::Heading.new(tag: :h1)) { @project_custom_field.name }+ render(Primer::Beta::Heading.new(tag: :h1)) { @custom_field.name }endendheader_container.with_row(mb: 2) dodiff --git a/app/components/settings/project_custom_fields/edit_form_header_component.rb b/app/components/settings/project_custom_fields/edit_form_header_component.rbindex 5dc70d8f8a8..2e6450b489a 100644--- a/app/components/settings/project_custom_fields/edit_form_header_component.rb+++ b/app/components/settings/project_custom_fields/edit_form_header_component.rb@@ -32,10 +32,10 @@ module Settingsinclude ApplicationHelperinclude OpPrimer::ComponentHelpers- def initialize(project_custom_field: ProjectCustomField.new)+ def initialize(custom_field:)super- @project_custom_field = project_custom_field+ @custom_field = custom_fieldendendenddiff --git a/app/components/settings/project_custom_fields/form_component.html.erb b/app/components/settings/project_custom_fields/form_component.html.erbindex 17ed778aa26..700c6c860e3 100644--- a/app/components/settings/project_custom_fields/form_component.html.erb+++ b/app/components/settings/project_custom_fields/form_component.html.erb@@ -26,183 +26,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.See COPYRIGHT and LICENSE files for more details.++#%>-<%= @project_custom_field.errors.inspect %>-<%= primer_form_with(**form_config) do |f| %>- <section class="form--section" id="custom_field_form">- <div class="form--field -required" id="custom_field_name_attributes">- <%= f.text_field :name, required: true, container_class: '-middle' %>- </div>- <div class="form--field">- <%= f.select :project_custom_field_section_id,- ProjectCustomFieldSection.all.collect { |s| [s.name, s.id] },- { container_class: '-slim' }- %>- </div>- <div class="form--field">- <%= f.select :field_format,- custom_field_formats_for_select(@project_custom_field),- { container_class: '-slim' },- disabled: !@project_custom_field.new_record?,- data: {- action: 'admin--custom-fields#formatChanged',- 'admin--custom-fields-target': 'format'- }- %>- </div>- <div class="form--grouping" id="custom_field_length">- <div class="form--grouping-label">- <%= t(:label_min_max_length) %> <br>- <small>(<%= t(:text_min_max_length_info) %>)</small>- </div>- <div class="form--grouping-row">- <div class="form--field -wide-label">- <%= f.number_field :min_length,- container_class: '-xslim',- data: { 'admin--custom-fields-target': 'length' } %>- </div>- <div class="form--field -wide-label">- <%= f.number_field :max_length,- container_class: '-xslim',- data: { 'admin--custom-fields-target': 'length' } %>- </div>- </div>- </div>-- <div class="form--field">- <%= f.text_field :regexp,- size: 50,- container_class: '-wide',- data: { 'admin--custom-fields-target': 'regexp' } %>- <span class="form--field-instructions">- <%= t(:text_regexp_info) %>- </span>- </div>-- <% if @project_custom_field.new_record? || @project_custom_field.list? || @project_custom_field.multi_value_possible? %>- <div class="form--field" hidden>- <%= f.check_box :multi_value,- data: {- 'admin--custom-fields-target': 'multiSelect',- action: 'admin--custom-fields#checkOnlyOne'- } %>- </div>-- <fieldset class="form--fieldset" data-admin--custom-fields-target="possibleValues">- <legend class="form--fieldset-legend"><%= I18n.t("activerecord.attributes.custom_field.possible_values") %></legend>- <% if false %>- <div class="form--toolbar">- <span class="form--toolbar-item">- <%= link_to t('custom_fields.reorder_alphabetical'),- { action: :reorder_alphabetical },- method: :post,- data: { confirm: t('custom_fields.reorder_confirmation') } %>- </span>- </div>- <% end %>- <% if false %>- <div class="form--field">- <%= render partial: "custom_fields/custom_options", locals: { custom_field: @project_custom_field, f: f } %>- <button- type="button"- class="spot-link"- data-test-selector="add-custom-option"- data-action="admin--custom-fields#addOption"- >- <span class="spot-icon spot-icon_1_25 spot-icon_add"></span>- <span><%= t(:button_add) %></span>- </button>- </div>- <% end %>- </fieldset>- <% end %>- <div data-admin--custom-fields-target="defaultValue">- <div class="form--field" id="default_value_text">- <% if @project_custom_field.new_record? || !%w[text bool].include?(@project_custom_field.field_format) %>- <%= f.text_field :default_value,- id: 'custom_fields_default_value_text',- for: 'custom_fields_default_value_text',- data: { 'admin--custom-fields-target': 'defaultText' },- container_class: '-wide' %>- <% end %>- </div>- <div class="form--field" hidden>- <% if @project_custom_field.new_record? || @project_custom_field.field_format == 'bool' %>- <%= f.check_box :default_value,- id: 'custom_fields_default_value_bool',- data: { 'admin--custom-fields-target': 'defaultBool' },- for: 'custom_fields_default_value_bool' %>- <% end %>- </div>- <div class="form--field" hidden>- <% if @project_custom_field.new_record? || @project_custom_field.field_format == 'text' %>- <%= f.text_area :default_value,- id: 'custom_fields_default_value_longtext',- data: { 'admin--custom-fields-target': 'defaultLongText' },- for: 'custom_fields_default_value_longtext',- cols: 100,- rows: 20,- class: 'wiki-edit',- with_text_formatting: true %>- <% end %>- </div>- </div>- <%= call_hook(:view_custom_fields_form_upper_box, custom_field: @project_custom_field, form: f) %>- </section>-- <section class="form--section">- <% case @project_custom_field.class.name- when "WorkPackageCustomField" %>- <div class="form--field">- <%= f.check_box :is_required %>- </div>- <div class="form--field">- <%= f.check_box :is_for_all %>- </div>- <div class="form--field">- <%= f.check_box :is_filter %>- </div>- <div class="form--field">- <%= f.check_box :searchable,- data: { 'admin--custom-fields-target': 'searchable' }%>- </div>- <div class="form--field" hidden>- <%= f.check_box :content_right_to_left,- data: { 'admin--custom-fields-target': 'textOrientation' } %>- </div>- <% when "UserCustomField" %>- <div class="form--field">- <%= f.check_box :is_required %>- </div>- <div class="form--field">- <%= f.check_box :visible %>- </div>- <div class="form--field">- <%= f.check_box :editable %>- </div>- <% when "ProjectCustomField" %>- <div class="form--field">- <%= f.check_box :is_required %>- </div>- <div class="form--field">- <%= f.check_box :visible %>- </div>- <div class="form--field">- <%= f.check_box :searchable,- data: { 'admin--custom-fields-target': 'searchable' }%>- </div>- <% when "TimeEntryCustomField" %>- <div class="form--field">- <%= f.check_box :is_required %>- </div>- <% else %>- <div class="form--field">- <%= f.check_box :is_required %>- </div>- <% end %>- <%= call_hook(:"view_custom_fields_form_#{@project_custom_field.type.to_s.underscore}", custom_field: @project_custom_field, form: f) %>-- <%= render(Primer::ButtonComponent.new(scheme: :primary, type: :submit)) do %>- <%= t("button_save") %>+<%= content_tag(:div, data: {+ controller: 'admin--custom-fields',+ 'application-target': 'dynamic',+ 'admin--custom-fields-format-value': @custom_field.field_format+ }) do %>+ <%= error_messages_for 'custom_field' %>++ <%= labelled_tabular_form_for(@custom_field, **form_config) do |f| %>+ <%= render partial: 'custom_fields/form', locals: { f: f, custom_field: @custom_field } %>+ <% if @custom_field.new_record? %>+ <%= hidden_field_tag 'type', @custom_field.type %><% end %>- </section>+ <%= styled_button_tag t(:button_save), class: '-highlight -with-icon icon-checkmark' %>+ <% end %><% end %>+diff --git a/app/components/settings/project_custom_fields/form_component.rb b/app/components/settings/project_custom_fields/form_component.rbindex bb4b1952dcc..211c8191756 100644--- a/app/components/settings/project_custom_fields/form_component.rb+++ b/app/components/settings/project_custom_fields/form_component.rb@@ -31,23 +31,24 @@ module Settingsclass FormComponent < ApplicationComponentinclude ApplicationHelperinclude CustomFieldsHelper+ include StimulusHelper+ include ErrorMessageHelper+ include OpenProject::FormTagHelperinclude OpPrimer::ComponentHelpers- include OpTurbo::Streamable- def initialize(project_custom_field: ProjectCustomField.new)+ def initialize(custom_field: ProjectCustomField.new)super- @project_custom_field = project_custom_field+ @custom_field = custom_fieldendprivatedef form_config{- model: @project_custom_field,- scope: :custom_field,- method: @project_custom_field.persisted? ? :put : :post,- url: @project_custom_field.persisted? ? admin_settings_project_custom_field_path(@project_custom_field) : admin_settings_project_custom_fields_path+ as: :custom_field,+ url: @custom_field.persisted? ? admin_settings_project_custom_field_path(@custom_field) : admin_settings_project_custom_fields_path,+ html: { method: @custom_field.persisted? ? :put : :post, id: 'custom_field_form' }}endenddiff --git a/app/components/settings/project_attributes/header_component.html.erb b/app/components/settings/project_custom_fields/header_component.html.erbsimilarity index 97%rename from app/components/settings/project_attributes/header_component.html.erbrename to app/components/settings/project_custom_fields/header_component.html.erbindex c126ee29a61..8a590d5411f 100644--- a/app/components/settings/project_attributes/header_component.html.erb+++ b/app/components/settings/project_custom_fields/header_component.html.erb@@ -16,7 +16,7 @@tag: :a,href: new_admin_settings_project_custom_field_path(type: "ProjectCustomField"),scheme: :primary,- data: { 'turbo-frame': 'true' }+ data: { turbo: "false"})) do |button|button.with_leading_visual_icon(icon: :plus)t('settings.project_attributes.label_new_attribute')diff --git a/app/components/settings/project_attributes/header_component.rb b/app/components/settings/project_custom_fields/header_component.rbsimilarity index 98%rename from app/components/settings/project_attributes/header_component.rbrename to app/components/settings/project_custom_fields/header_component.rbindex 5efac759681..74477d49f40 100644--- a/app/components/settings/project_attributes/header_component.rb+++ b/app/components/settings/project_custom_fields/header_component.rb@@ -27,7 +27,7 @@#++module Settings- module ProjectAttributes+ module ProjectCustomFieldsclass HeaderComponent < ApplicationComponentinclude ApplicationHelperinclude OpPrimer::ComponentHelpersdiff --git a/app/contracts/custom_fields/base_contract.rb b/app/contracts/custom_fields/base_contract.rbindex 5721e5c400c..6ebe189a908 100644--- a/app/contracts/custom_fields/base_contract.rb+++ b/app/contracts/custom_fields/base_contract.rb@@ -47,5 +47,6 @@ module CustomFieldsattribute :possible_valuesattribute :multi_valueattribute :content_right_to_left+ attribute :custom_field_section_idendenddiff --git a/app/controllers/admin/settings/project_custom_field_sections_controller.rb b/app/controllers/admin/settings/project_custom_field_sections_controller.rbindex 5f601238e95..041749c9e0a 100644--- a/app/controllers/admin/settings/project_custom_field_sections_controller.rb+++ b/app/controllers/admin/settings/project_custom_field_sections_controller.rb@@ -41,9 +41,9 @@ module Admin::Settingsif @project_custom_field_section.saveupdate_header_via_turbo_stream # required to closed the dialog- update_sections_via_turbo_stream(sections: ProjectCustomFieldSection.all)+ update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)else- update_section_dialog_body_form_via_turbo_stream(section: @project_custom_field_section)+ update_section_dialog_body_form_via_turbo_stream(project_custom_field_section: @project_custom_field_section)endrespond_with_turbo_streams@@ -53,9 +53,9 @@ module Admin::Settings@project_custom_field_section = ProjectCustomFieldSection.find(params[:id])if @project_custom_field_section.update(project_custom_field_section_params)- update_section_via_turbo_stream(section: @project_custom_field_section)+ update_section_via_turbo_stream(project_custom_field_section: @project_custom_field_section)else- update_section_dialog_body_form_via_turbo_stream(section: @project_custom_field_section)+ update_section_dialog_body_form_via_turbo_stream(project_custom_field_section: @project_custom_field_section)endrespond_with_turbo_streams@@ -65,7 +65,7 @@ module Admin::Settings@project_custom_field_section = ProjectCustomFieldSection.find(params[:id])if @project_custom_field_section.destroy- update_sections_via_turbo_stream(sections: ProjectCustomFieldSection.all)+ update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)endrespond_with_turbo_streams@@ -75,20 +75,16 @@ module Admin::Settings@project_custom_field_section.move_to = params[:move_to]&.to_symif @project_custom_field_section.save- update_sections_via_turbo_stream(sections: ProjectCustomFieldSection.all)+ update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)endrespond_with_turbo_streamsenddef drop- if params[:position] == 'lowest'- @project_custom_field_section.move_to = :lowest- else- @project_custom_field_section.insert_at(params[:position].to_i)- end+ @project_custom_field_section.insert_at(params[:position].to_i)- update_sections_via_turbo_stream(sections: ProjectCustomFieldSection.all)+ update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)respond_with_turbo_streamsenddiff --git a/app/controllers/admin/settings/project_custom_fields_controller.rb b/app/controllers/admin/settings/project_custom_fields_controller.rbindex 37c78fc49ae..ded7af72c9d 100644--- a/app/controllers/admin/settings/project_custom_fields_controller.rb+++ b/app/controllers/admin/settings/project_custom_fields_controller.rb@@ -28,13 +28,16 @@module Admin::Settingsclass ProjectCustomFieldsController < ::Admin::SettingsController+ include CustomFields::SharedActionsinclude OpTurbo::ComponentStreaminclude Admin::Settings::ProjectCustomFields::ComponentStreamsmenu_item :project_custom_field_settings- before_action :set_sections, only: %i[index edit update move drop]- before_action :set_project_custom_field, only: %i[edit update move drop]+ before_action :set_sections, only: %i[show index edit update move drop]+ before_action :find_custom_field, only: %i(show edit update destroy delete_option reorder_alphabetical move drop)+ before_action :prepare_custom_option_position, only: %i(update create)+ before_action :find_custom_option, only: :delete_optiondef default_breadcrumbt(:label_project_attributes_plural)@@ -44,95 +47,82 @@ module Admin::Settingsrespond_to :htmlend- def new- @project_custom_field = ProjectCustomField.new-- respond_to :html+ def show+ # quick fixing redirect issue from perform_update+ # perform_update is always redirecting to the show action altough configured otherwise+ render :editend- def edit- respond_to :html- end-- def create- @project_custom_field = ProjectCustomField.new(project_custom_field_params.except(:project_custom_field_section_id))-- if @project_custom_field.save- @project_custom_field.project_custom_field_section = ProjectCustomFieldSection.find(project_custom_field_params[:project_custom_field_section_id])+ def new+ @custom_field = ProjectCustomField.new- redirect_to admin_settings_project_custom_fields_path- else- render action: 'new'- end+ respond_to :htmlend- def update- if project_custom_field_params[:project_custom_field_section_id]&.to_i != @project_custom_field.project_custom_field_section_id- mapping = @project_custom_field.project_custom_field_section_mapping- mapping.remove_from_list- @project_custom_field.project_custom_field_section = ProjectCustomFieldSection.find(project_custom_field_params[:project_custom_field_section_id])- end-- if @project_custom_field.update(project_custom_field_params.except(:project_custom_field_section_id))- redirect_to admin_settings_project_custom_fields_path- else- render action: 'edit'- end- end+ def edit; enddef move- mapping = @project_custom_field.project_custom_field_section_mapping- mapping.move_to = params[:move_to]&.to_sym+ # prototyopical implementation+ # needs refactoring via update service+ @custom_field.move_to = params[:move_to]&.to_sym- update_sections_via_turbo_stream(sections: @sections)+ update_sections_via_turbo_stream(project_custom_field_sections: @custom_field_sections)respond_with_turbo_streamsenddef drop- mapping = @project_custom_field.project_custom_field_section_mapping-- current_section = @project_custom_field.project_custom_field_section+ # prototyopical implementation+ # needs refactoring via update service+ current_section = @custom_field.project_custom_field_sectioncurrent_section_id = current_section.idnew_section_id = params[:target_id].to_iif current_section_id != new_section_idsection_changed = trueold_section = current_section- mapping.remove_from_listcurrent_section = ProjectCustomFieldSection.find(params[:target_id].to_i)- @project_custom_field.project_custom_field_section = current_section+ @custom_field.remove_from_list+ @custom_field.update(project_custom_field_section: current_section)end- mapping = @project_custom_field.reload.project_custom_field_section_mapping-- if params[:position] == 'lowest'- mapping.move_to = :lowest- else- mapping.insert_at(params[:position].to_i)- end+ @custom_field.insert_at(params[:position].to_i)- update_section_via_turbo_stream(section: current_section)+ update_section_via_turbo_stream(project_custom_field_section: current_section)if section_changed- update_section_via_turbo_stream(section: old_section)+ update_section_via_turbo_stream(project_custom_field_section: old_section)endrespond_with_turbo_streamsend+ def destroy+ @custom_field.destroy++ update_section_via_turbo_stream(project_custom_field_section: @custom_field.project_custom_field_section)++ respond_with_turbo_streams+ end+private- def set_sections- @sections = ProjectCustomFieldSection.all+ def edit_path(id:)+ admin_settings_project_custom_field_path(id:)end- def set_project_custom_field- @project_custom_field = ProjectCustomField.find(params[:id])+ def index_path(params = {})+ admin_settings_project_custom_fields_path(**params)+ end++ def set_sections+ @project_custom_field_sections = ProjectCustomFieldSection.allend- def project_custom_field_params- permitted_params.custom_field+ def find_custom_field+ @custom_field = ProjectCustomField.find(params[:id])+ rescue ActiveRecord::RecordNotFound+ render_404endendenddiff --git a/app/controllers/concerns/admin/settings/project_custom_fields/component_streams.rb b/app/controllers/concerns/admin/settings/project_custom_fields/component_streams.rbindex bd85e840446..e393a6a9162 100644--- a/app/controllers/concerns/admin/settings/project_custom_fields/component_streams.rb+++ b/app/controllers/concerns/admin/settings/project_custom_fields/component_streams.rb@@ -35,30 +35,30 @@ module Adminincluded dodef update_header_via_turbo_streamupdate_via_turbo_stream(- component: ::Settings::ProjectAttributes::HeaderComponent.new+ component: ::Settings::ProjectCustomFields::HeaderComponent.new)end- def update_section_via_turbo_stream(section:)+ def update_section_via_turbo_stream(project_custom_field_section:)update_via_turbo_stream(- component: ::Settings::ProjectAttributes::SectionComponent.new(- section:+ component: ::Settings::ProjectCustomFieldSections::ShowComponent.new(+ project_custom_field_section:))end- def update_section_dialog_body_form_via_turbo_stream(section:)+ def update_section_dialog_body_form_via_turbo_stream(project_custom_field_section:)update_via_turbo_stream(component: ::Settings::ProjectAttributes::Section::DialogBodyFormComponent.new(- section:+ project_custom_field_section:))end- def update_sections_via_turbo_stream(sections:)+ def update_sections_via_turbo_stream(project_custom_field_sections:)replace_via_turbo_stream(- component: ::Settings::ProjectAttributes::Section::IndexComponent.new(- sections:+ component: ::Settings::ProjectCustomFieldSections::IndexComponent.new(+ project_custom_field_sections:))enddiff --git a/app/controllers/concerns/custom_fields/shared_actions.rb b/app/controllers/concerns/custom_fields/shared_actions.rbnew file mode 100644index 00000000000..f84db04f366--- /dev/null+++ b/app/controllers/concerns/custom_fields/shared_actions.rb@@ -0,0 +1,143 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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 CustomFields+ module SharedActions+ extend ActiveSupport::Concern++ included do+ def index_path(params = {})+ # default path below, can be overridden in the including controller+ custom_fields_path(**params)+ end++ def edit_path(params = {})+ # default path below, can be overridden in the including controller+ edit_custom_field_path(**params)+ end++ def create+ call = ::CustomFields::CreateService+ .new(user: current_user)+ .call(get_custom_field_params.merge(type: permitted_params.custom_field_type))++ if call.success?+ flash[:notice] = t(:notice_successful_create)+ call_hook(:controller_custom_fields_new_after_save, custom_field: call.result)+ redirect_to index_path(tab: call.result.class.name)+ else+ @custom_field = call.result || new_custom_field+ render action: 'new'+ end+ end++ def update+ perform_update(get_custom_field_params)+ end++ def perform_update(custom_field_params)+ call = ::CustomFields::UpdateService+ .new(user: current_user, model: @custom_field)+ .call(custom_field_params)++ if call.success?+ flash[:notice] = t(:notice_successful_update)+ call_hook(:controller_custom_fields_edit_after_save, custom_field: @custom_field)+ redirect_back_or_default(edit_path(id: @custom_field.id))+ else+ render action: 'edit'+ end+ end++ def reorder_alphabetical+ reordered_options = @custom_field+ .custom_options+ .sort_by(&:value)+ .each_with_index+ .map do |custom_option, index|+ { id: custom_option.id, position: index + 1 }+ end++ perform_update(custom_options_attributes: reordered_options)+ end++ def destroy+ begin+ @custom_field.destroy+ rescue StandardError+ flash[:error] = I18n.t(:error_can_not_delete_custom_field)+ end+ redirect_to index_path(tab: @custom_field.class.name)+ end++ def delete_option+ if @custom_option.destroy+ num_deleted = delete_custom_values! @custom_option++ flash[:notice] = I18n.t(+ :notice_custom_options_deleted, option_value: @custom_option.value, num_deleted:+ )+ else+ flash[:error] = @custom_option.errors.full_messages+ end++ redirect_to edit_path(id: @custom_field.id)+ end++ def new_custom_field+ ::CustomFields::CreateService.careful_new_custom_field(permitted_params.custom_field_type)+ end++ def get_custom_field_params+ permitted_params.custom_field+ end++ def find_custom_option+ @custom_option = CustomOption.find params[:option_id]+ rescue ActiveRecord::RecordNotFound+ render_404+ end++ def delete_custom_values!(custom_option)+ CustomValue+ .where(custom_field_id: custom_option.custom_field_id, value: custom_option.id)+ .delete_all+ end++ def prepare_custom_option_position+ return unless params[:custom_field][:custom_options_attributes]++ index = 0++ params[:custom_field][:custom_options_attributes].each do |_id, attributes|+ attributes[:position] = (index = index + 1)+ end+ end+ end+ end+enddiff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rbindex 7d1e21b8330..a9217b74da2 100644--- a/app/controllers/custom_fields_controller.rb+++ b/app/controllers/custom_fields_controller.rb@@ -27,6 +27,7 @@#++class CustomFieldsController < ApplicationController+ include CustomFields::SharedActionslayout 'admin'before_action :require_admin@@ -53,112 +54,6 @@ class CustomFieldsController < ApplicationControllerdef edit; end- def create- call = ::CustomFields::CreateService- .new(user: current_user)- .call(get_custom_field_params.merge(type: permitted_params.custom_field_type))-- if call.success?- flash[:notice] = t(:notice_successful_create)- call_hook(:controller_custom_fields_new_after_save, custom_field: call.result)- redirect_to custom_fields_path(tab: call.result.class.name)- else- @custom_field = call.result || new_custom_field- render action: 'new'- end- end-- def update- perform_update(get_custom_field_params)- end-- def reorder_alphabetical- reordered_options = @custom_field- .custom_options- .sort_by(&:value)- .each_with_index- .map do |custom_option, index|- { id: custom_option.id, position: index + 1 }- end-- perform_update(custom_options_attributes: reordered_options)- end-- def destroy- begin- @custom_field.destroy- rescue StandardError- flash[:error] = I18n.t(:error_can_not_delete_custom_field)- end- redirect_to custom_fields_path(tab: @custom_field.class.name)- end-- def delete_option- if @custom_option.destroy- num_deleted = delete_custom_values! @custom_option-- flash[:notice] = I18n.t(- :notice_custom_options_deleted, option_value: @custom_option.value, num_deleted:- )- else- flash[:error] = @custom_option.errors.full_messages- end-- redirect_to edit_custom_field_path(id: @custom_field.id)- end-- private-- def perform_update(custom_field_params)- call = ::CustomFields::UpdateService- .new(user: current_user, model: @custom_field)- .call(custom_field_params)-- if call.success?- flash[:notice] = t(:notice_successful_update)- call_hook(:controller_custom_fields_edit_after_save, custom_field: @custom_field)- redirect_back_or_default edit_custom_field_path(id: @custom_field.id)- else- render action: 'edit'- end- end-- def new_custom_field- ::CustomFields::CreateService.careful_new_custom_field(permitted_params.custom_field_type)- end-- def get_custom_field_params- permitted_params.custom_field- end-- def find_custom_option- @custom_option = CustomOption.find params[:option_id]- rescue ActiveRecord::RecordNotFound- render_404- end-- def delete_custom_values!(custom_option)- CustomValue- .where(custom_field_id: custom_option.custom_field_id, value: custom_option.id)- .delete_all- end-- def prepare_custom_option_position- return unless params[:custom_field][:custom_options_attributes]-- index = 0-- params[:custom_field][:custom_options_attributes].each do |_id, attributes|- attributes[:position] = (index = index + 1)- end- end-- def find_custom_field- @custom_field = CustomField.find(params[:id])- rescue ActiveRecord::RecordNotFound- render_404- end-protecteddef default_breadcrumb@@ -172,4 +67,10 @@ class CustomFieldsController < ApplicationControllerdef show_local_breadcrumbtrueend++ def find_custom_field+ @custom_field = CustomField.find(params[:id])+ rescue ActiveRecord::RecordNotFound+ render_404+ endenddiff --git a/app/models/project_custom_field_section_mapping.rb b/app/models/custom_field_section.rbsimilarity index 60%rename from app/models/project_custom_field_section_mapping.rbrename to app/models/custom_field_section.rbindex 33bc324a151..7d1cd811a4a 100644--- a/app/models/project_custom_field_section_mapping.rb+++ b/app/models/custom_field_section.rb@@ -26,22 +26,12 @@# See COPYRIGHT and LICENSE files for more details.#++-class ProjectCustomFieldSectionMapping < ApplicationRecord- belongs_to :project_custom_field_section- belongs_to :project_custom_field, class_name: 'ProjectCustomField', foreign_key: 'custom_field_id',- inverse_of: :project_custom_field_section_mappings+class CustomFieldSection < ApplicationRecord+ has_many :custom_fields, dependent: :destroy- # Additionally to the database-level unique constraint, the application-level validation ensures that a- # custom_field is associated with only one section- validate :project_custom_field_uniqueness+ acts_as_list scope: [:type]- acts_as_list scope: :project_custom_field_section+ validates :name, presence: true- private-- def project_custom_field_uniqueness- if ProjectCustomFieldSectionMapping.where(custom_field_id:).where.not(id:).exists?- errors.add(:project_custom_field, "is already associated with another section")- end- end+ default_scope { order(:position) }enddiff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rbindex 3b8a5f23aa6..f90b7f40814 100644--- a/app/models/permitted_params.rb+++ b/app/models/permitted_params.rb@@ -466,7 +466,7 @@ class PermittedParams:possible_values,:multi_value,:content_right_to_left,- :project_custom_field_section_id,+ :custom_field_section_id,{ custom_options_attributes: %i(id value default_value position) },{ type_ids: [] }],diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rbindex bd27c10091e..69560870d83 100644--- a/app/models/project_custom_field.rb+++ b/app/models/project_custom_field.rb@@ -27,20 +27,11 @@#++class ProjectCustomField < CustomField- # don't pollute the custom_fields table with a section_id column which is only used by ProjectCustomFields- # use a separate mapping table instead- has_many :project_custom_field_section_mappings,- dependent: :destroy,- inverse_of: :project_custom_field,- class_name: 'ProjectCustomFieldSectionMapping',- foreign_key: 'custom_field_id'+ belongs_to :project_custom_field_section, class_name: 'ProjectCustomFieldSection', foreign_key: :custom_field_section_id+ has_many :project_custom_field_project_mappings, class_name: 'ProjectCustomFieldProjectMapping', foreign_key: :custom_field_id,+ dependent: :destroy, inverse_of: :project_custom_field- has_many :project_custom_field_sections,- through: :project_custom_field_section_mappings-- accepts_nested_attributes_for :project_custom_field_sections-- validate :exactly_one_section_mapped+ acts_as_list column: :position_in_custom_field_section, scope: [:custom_field_section_id]def type_name:label_project_plural@@ -53,34 +44,4 @@ class ProjectCustomField < CustomFieldwhere(visible: true)endend-- def exactly_one_section_mapped- return unless persisted?-- unless project_custom_field_sections.count == 1- errors.add(:base, "Exactly one section must be mapped to this custom field.")- end- end-- def project_custom_field_section_mapping- project_custom_field_section_mappings.first- end-- def project_custom_field_section- project_custom_field_sections.first- end-- def project_custom_field_section_id- project_custom_field_section&.id- end-- def project_custom_field_section=(section)- # without this reload, a nil assignment is not recovered properly- project_custom_field_sections.reload-- ActiveRecord::Base.transaction do- project_custom_field_sections.clear- project_custom_field_sections << section- end- endenddiff --git a/app/models/project_custom_field_project_mapping.rb b/app/models/project_custom_field_project_mapping.rbindex e6d5c413875..faff27f27c6 100644--- a/app/models/project_custom_field_project_mapping.rb+++ b/app/models/project_custom_field_project_mapping.rb@@ -29,7 +29,7 @@class ProjectCustomFieldProjectMapping < ApplicationRecordbelongs_to :projectbelongs_to :project_custom_field, class_name: 'ProjectCustomField', foreign_key: 'custom_field_id',- inverse_of: :project_custom_field_section_mappings+ inverse_of: :project_custom_field_project_mappings# # Additionally to the database-level unique constraint, the application-level validation ensures that a# # custom_field is associated with only one sectiondiff --git a/app/models/project_custom_field_section.rb b/app/models/project_custom_field_section.rbindex 5df3ab10531..fcfe4b98ae5 100644--- a/app/models/project_custom_field_section.rb+++ b/app/models/project_custom_field_section.rb@@ -26,17 +26,5 @@# See COPYRIGHT and LICENSE files for more details.#++-class ProjectCustomFieldSection < ApplicationRecord- has_many :project_custom_field_section_mappings, dependent: :destroy- has_many :project_custom_fields, through: :project_custom_field_section_mappings-- acts_as_list-- validates :name, presence: true-- default_scope { order(:position) }-- def project_custom_fields_ordered_by_postion_in_section- project_custom_fields.reorder('project_custom_field_section_mappings.position ASC')- end+class ProjectCustomFieldSection < CustomFieldSectionenddiff --git a/app/views/admin/settings/project_custom_fields/edit.html.erb b/app/views/admin/settings/project_custom_fields/edit.html.erbindex 852e11dcb02..b3f3bc19b06 100644--- a/app/views/admin/settings/project_custom_fields/edit.html.erb+++ b/app/views/admin/settings/project_custom_fields/edit.html.erb@@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.See COPYRIGHT and LICENSE files for more details.++#%>-<turbo-frame id="admin-settings-project-custom-fields">- <%= render(Settings::ProjectCustomFields::EditFormHeaderComponent.new(project_custom_field: @project_custom_field)) %>- <%= render(Settings::ProjectCustomFields::FormComponent.new(project_custom_field: @project_custom_field)) %>-</turbo-frame>+<%= render(Settings::ProjectCustomFields::EditFormHeaderComponent.new(custom_field: @custom_field)) %>+<%= render(Settings::ProjectCustomFields::FormComponent.new(custom_field: @custom_field)) %>+diff --git a/app/views/admin/settings/project_custom_fields/index.html.erb b/app/views/admin/settings/project_custom_fields/index.html.erbindex dd9ec6c0415..14b91d79930 100644--- a/app/views/admin/settings/project_custom_fields/index.html.erb+++ b/app/views/admin/settings/project_custom_fields/index.html.erb@@ -27,8 +27,8 @@ See COPYRIGHT and LICENSE files for more details.++#%><div data-turbo="true">- <turbo-frame id="admin-settings-project-custom-fields" data-turbo-action="advance">- <%= render(Settings::ProjectAttributes::HeaderComponent.new()) %>- <%= render(Settings::ProjectAttributes::Section::IndexComponent.new(sections: @sections)) %>- </turbo-frame>+ <%= render(Settings::ProjectCustomFields::HeaderComponent.new()) %>+ <%= render(Settings::ProjectCustomFieldSections::IndexComponent.new(+ project_custom_field_sections: @project_custom_field_sections+ )) %></div>diff --git a/app/views/admin/settings/project_custom_fields/new.html.erb b/app/views/admin/settings/project_custom_fields/new.html.erbindex cee5c4fc715..bc254c2f1cd 100644--- a/app/views/admin/settings/project_custom_fields/new.html.erb+++ b/app/views/admin/settings/project_custom_fields/new.html.erb@@ -26,7 +26,5 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.See COPYRIGHT and LICENSE files for more details.++#%>-<turbo-frame id="admin-settings-project-custom-fields">- <%= render(Settings::ProjectCustomFields::NewFormHeaderComponent.new()) %>- <%= render(Settings::ProjectCustomFields::FormComponent.new(project_custom_field: @project_custom_field)) %>-</turbo-frame>+<%= render(Settings::ProjectCustomFields::NewFormHeaderComponent.new()) %>+<%= render(Settings::ProjectCustomFields::FormComponent.new(custom_field: @custom_field)) %>diff --git a/app/views/custom_fields/_form.html.erb b/app/views/custom_fields/_form.html.erbindex 53644c6fd6e..a9504bb2edb 100644--- a/app/views/custom_fields/_form.html.erb+++ b/app/views/custom_fields/_form.html.erb@@ -26,10 +26,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.See COPYRIGHT and LICENSE files for more details.++#%>+<!-- Enable reusing this form partial within a view_component which does not pass instance vars directly -->+<!-- custom_field is passed via the local api when rendering the parial from a view_component -->+<% @custom_field = custom_field if @custom_field.nil? %>+<section class="form--section" id="custom_field_form"><div class="form--field -required" id="custom_field_name_attributes"><%= f.text_field :name, required: true, container_class: '-middle' %></div>+ <% if @custom_field.type == 'ProjectCustomField' %>+ <div class="form--field">+ <%= f.select :custom_field_section_id,+ ProjectCustomFieldSection.all.collect { |s| [s.name, s.id] },+ { container_class: '-slim' }+ %>+ </div>+ <% end %><div class="form--field"><%= f.select :field_format,custom_field_formats_for_select(@custom_field),diff --git a/config/routes.rb b/config/routes.rbindex a03141d2934..1f57c092e58 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -418,9 +418,10 @@ OpenProject::Application.routes.draw doresource :api, controller: '/admin/settings/api_settings', only: %i[show update]resource :work_packages, controller: '/admin/settings/work_packages_settings', only: %i[show update]resource :projects, controller: '/admin/settings/projects_settings', only: %i[show update]- resources :project_custom_fields, controller: '/admin/settings/project_custom_fields',- only: %i[index show new create edit update] do+ resources :project_custom_fields, controller: '/admin/settings/project_custom_fields' domember do+ delete "options/:option_id", action: "delete_option", as: :delete_option_of+ post :reorder_alphabeticalput :moveput :dropenddiff --git a/db/migrate/20231030103212_create_project_custom_field_sections.rb b/db/migrate/20231030103212_create_project_custom_field_sections.rbdeleted file mode 100644index f79300b9d67..00000000000--- a/db/migrate/20231030103212_create_project_custom_field_sections.rb+++ /dev/null@@ -1,47 +0,0 @@-class CreateProjectCustomFieldSections < ActiveRecord::Migration[7.0]- def up- create_table :project_custom_field_sections do |t|- t.integer :position- t.string :name-- t.timestamps- end-- # don't pollute the custom_fields table with a section_id column which is only used by ProjectCustomFields- # use a separate mapping table instead- create_table :project_custom_field_section_mappings do |t|- t.references :project_custom_field_section, foreign_key: true, index: {- name: 'index_project_cfs_mappings_on_section_id'- }- t.references :custom_field, foreign_key: true- t.integer :position-- t.timestamps- end-- # Add a unique constraint to ensure that a custom_field can only be added to one section- add_index :project_custom_field_section_mappings, :custom_field_id, unique: true,- name: 'index_project_cfs_mappings_on_custom_field_id'-- create_and_assign_default_section- end-- def down- drop_table :project_custom_field_section_mappings- drop_table :project_custom_field_sections- end-- private-- def create_and_assign_default_section- section = ProjectCustomFieldSection.create!(- name: "Project attributes"- )-- mappings = ProjectCustomField.pluck(:id).map do |id|- { project_custom_field_section_id: section.id, custom_field_id: id }- end-- ProjectCustomFieldSectionMapping.create!(mappings)- end-enddiff --git a/db/migrate/20231123111357_create_custom_field_sections.rb b/db/migrate/20231123111357_create_custom_field_sections.rbnew file mode 100644index 00000000000..5d2d259cc1e--- /dev/null+++ b/db/migrate/20231123111357_create_custom_field_sections.rb@@ -0,0 +1,36 @@+class CreateCustomFieldSections < ActiveRecord::Migration[7.0]+ def up+ create_table :custom_field_sections do |t|+ t.integer :position+ t.string :name+ t.string :type # project or nil (-> work_package)++ t.timestamps+ end++ add_reference :custom_fields, :custom_field_section+ add_column :custom_fields, :position_in_custom_field_section, :integer, null: true++ create_and_assign_default_section+ end++ def down+ remove_reference :custom_fields, :custom_field_section+ remove_column :custom_fields, :position_in_custom_field_section+ drop_table :custom_field_sections+ end++ private++ def create_and_assign_default_section+ # for project custom fields only+ section = ProjectCustomFieldSection.create!(+ name: "Project attributes"+ )++ # trigger acts_as_list callbacks via updating each record instead of bulk update+ ProjectCustomField.find_each do |project_custom_field|+ project_custom_field.update!(custom_field_section_id: section.id)+ end+ end+enddiff --git a/frontend/src/stimulus/controllers/dynamic/generic-drag-and-drop.controller.ts b/frontend/src/stimulus/controllers/dynamic/generic-drag-and-drop.controller.tsindex 2c5a6874370..315ecc6e9eb 100644--- a/frontend/src/stimulus/controllers/dynamic/generic-drag-and-drop.controller.ts+++ b/frontend/src/stimulus/controllers/dynamic/generic-drag-and-drop.controller.ts@@ -32,7 +32,7 @@ import * as Turbo from '@hotwired/turbo';import { Controller } from '@hotwired/stimulus';import { Drake } from 'dragula';import { debugLog } from 'core-app/shared/helpers/debug_output';-import { dropRight } from 'lodash';+import { dropRight, get } from 'lodash';export default class extends Controller {drake:Drake|undefined;@@ -129,7 +129,13 @@ export default class extends Controller {async drop(el:Element, target:Element, _source:Element|null, sibling:Element|null) {const dropUrl = el.getAttribute('data-drop-url');- const targetPosition = Array.from(target.children).indexOf(el);+ let targetPosition = Array.from(target.children).indexOf(el);+ if(target.children.length > 0 && target.children[0].getAttribute('data-empty-list-item') == 'true'){+ // if the target container is empty, a list item showing an empty message might be shown+ // this should not be counted as a list item+ // thus we need to subtract 1 from the target position+ targetPosition--;+ }const targetConfig: any = this.targetConfigs.find((config: any) => config.container == target);const targetId = targetConfig?.targetId as string | undefined;