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
Fix custom fields reordering
Oliver Günther9a68bc6c0671
diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rbindex 722f641c97a..86c998256c5 100644--- a/app/controllers/custom_fields_controller.rb+++ b/app/controllers/custom_fields_controller.rb@@ -34,7 +34,7 @@ class CustomFieldsController < ApplicationControllerhelper_method :gonbefore_action :require_admin- before_action :find_custom_field, only: %i(edit update destroy move delete_option)+ before_action :find_custom_field, only: %i(edit update destroy delete_option reorder_alphabetical)before_action :prepare_custom_option_position, only: %i(update create)before_action :find_custom_option, only: :delete_option@@ -73,17 +73,19 @@ class CustomFieldsController < ApplicationControllerdef edit; enddef update- call = ::CustomFields::UpdateService- .new(user: current_user, model: @custom_field)- .call(get_custom_field_params)+ perform_update(get_custom_field_params)+ end- 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'+ 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)enddef destroy@@ -111,6 +113,20 @@ class CustomFieldsController < ApplicationControllerprivate+ 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)enddiff --git a/app/views/custom_fields/_form.html.erb b/app/views/custom_fields/_form.html.erbindex 4f902105850..f5c5efc94cb 100644--- a/app/views/custom_fields/_form.html.erb+++ b/app/views/custom_fields/_form.html.erb@@ -77,6 +77,16 @@ See docs/COPYRIGHT.rdoc for more details.<fieldset class="form--fieldset" id="custom_field_possible_values_attributes"><legend class="form--fieldset-legend"><%= I18n.t("activerecord.attributes.custom_field.possible_values") %></legend>+ <% if @custom_field.persisted? %>+ <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 %><div class="form--field"><%= render partial: "custom_fields/custom_options", locals: { custom_field: @custom_field, f: f } %><a id="add-custom-option" href="#" class="icon icon-add"><%= t(:button_add) %></a>diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 0d93707c284..c25a412dc89 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -191,6 +191,8 @@ en:enabled_in_project: 'Enabled in project'contained_in_type: 'Contained in type'confirm_destroy_option: "Deleting an option will delete all of its occurrences (e.g. in work packages). Are you sure you want to delete it?"+ reorder_alphabetical: "Reorder values alphabetically"+ reorder_confirmation: "Warning: The current order of available values will be lost. Continue?"tab:no_results_title_text: There are currently no custom fields.no_results_content_text: Create a new custom fielddiff --git a/config/routes.rb b/config/routes.rbindex 8abbf9b34dd..a526cca9255 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -142,6 +142,8 @@ OpenProject::Application.routes.draw doto: "custom_fields#delete_option",via: :delete,as: :delete_option_of++ post :reorder_alphabeticalendenddiff --git a/frontend/src/app/modules/augmenting/dynamic-scripts/custom_fields.js b/frontend/src/app/modules/augmenting/dynamic-scripts/custom_fields.jsindex 9da86c4688a..b0a11955f86 100644--- a/frontend/src/app/modules/augmenting/dynamic-scripts/custom_fields.js+++ b/frontend/src/app/modules/augmenting/dynamic-scripts/custom_fields.js@@ -263,8 +263,9 @@$('#custom_field_multi_value').change(checkOnlyOne);// Make custom fields draggable- var container = document.getElementById('custom-field-dragula-container');- dragula([container], {+ const container = document.getElementById('custom-field-dragula-container');+ // eslint-disable-next-line no-undef+ const drake = dragula([container], {isContainer: function (el) {return false;},@@ -285,5 +286,21 @@mirrorContainer: container,ignoreInputTextSelection: true});++ // Setup autoscroll+ window.OpenProject.getPluginContext().then((pluginContext) => {+ new pluginContext.classes.DomAutoscrollService(+ [+ document.getElementById('content-wrapper'),+ ],+ {+ margin: 25,+ maxSpeed: 10,+ scrollWhenOutside: true,+ autoScroll: function () {+ return drake.dragging;+ }+ });+ });});}(window, jQuery));diff --git a/frontend/src/app/modules/plugins/plugin-context.ts b/frontend/src/app/modules/plugins/plugin-context.tsindex e622ab222b0..4cc3b58bb73 100644--- a/frontend/src/app/modules/plugins/plugin-context.ts+++ b/frontend/src/app/modules/plugins/plugin-context.ts@@ -23,6 +23,7 @@ import { ExternalRelationQueryConfigurationService } from "core-components/wp-taimport { InjectField } from "core-app/helpers/angular/inject-field.decorator";import { APIV3Service } from "core-app/modules/apiv3/api-v3.service";import { ConfigurationService } from "core-app/modules/common/config/configuration.service";+import { DomAutoscrollService } from "core-app/modules/common/drag-and-drop/dom-autoscroll.service";/*** Plugin context bridge for plugins outside the CLI compiler context@@ -65,7 +66,8 @@ export class OpenProjectPluginContext {dynamicContent: DynamicContentModal,},HalResource: HalResource,- DisplayField: DisplayField+ DisplayField: DisplayField,+ DomAutoscrollService,};// Hooksdiff --git a/spec/features/custom_fields/reorder_options_spec.rb b/spec/features/custom_fields/reorder_options_spec.rbnew file mode 100644index 00000000000..73b7df990b5--- /dev/null+++ b/spec/features/custom_fields/reorder_options_spec.rb@@ -0,0 +1,33 @@+require 'spec_helper'+require 'support/pages/custom_fields'++describe 'Reordering custom options of a list custom field', js: true do+ let(:user) { FactoryBot.create :admin }+ let(:cf_page) { Pages::CustomFields.new }++ let!(:custom_field) do+ FactoryBot.create(+ :list_wp_custom_field,+ name: "Platform",+ possible_values: %w[Playstation Xbox Nintendo PC Switch Mobile Dreamcast]+ )+ end++ before do+ login_as(user)+ end++ it 'reorders the items alphabetically when pressed' do+ expect(custom_field.custom_options.order(:position).pluck(:value))+ .to eq %w[Playstation Xbox Nintendo PC Switch Mobile Dreamcast]++ cf_page.visit!+ click_link custom_field.name++ click_link 'Reorder values alphabetically'+ cf_page.accept_alert_dialog!+ expect(page).to have_selector('.flash.notice', text: I18n.t(:notice_successful_update))+ expect(custom_field.custom_options.order(:position).pluck(:value))+ .to eq %w[Dreamcast Mobile Nintendo PC Playstation Switch Xbox]+ end+end