From 6fabc106964a6e33eb5f0cf779856a6b24451692 2017-01-18 14:49:57 From: Jean-Philippe Lang Date: 2017-01-18 14:49:57 Subject: [PATCH] Add warning when loosing data from custom fields when bulk editing issues (#22600). git-svn-id: http://svn.redmine.org/redmine/trunk@16224 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 1b6a82a..0f66f27 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -213,6 +213,16 @@ class IssuesController < ApplicationController edited_issues = Issue.where(:id => @issues.map(&:id)).to_a + @values_by_custom_field = {} + edited_issues.each do |issue| + issue.custom_field_values.each do |c| + if c.value_present? + @values_by_custom_field[c.custom_field] ||= [] + @values_by_custom_field[c.custom_field] << issue.id + end + end + end + @allowed_projects = Issue.allowed_target_projects if params[:issue] @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} @@ -244,6 +254,15 @@ class IssuesController < ApplicationController end end + edited_issues.each do |issue| + issue.custom_field_values.each do |c| + if c.value_present? && @values_by_custom_field[c.custom_field] + @values_by_custom_field[c.custom_field].delete(issue.id) + end + end + end + @values_by_custom_field.delete_if {|k,v| v.blank?} + @custom_fields = edited_issues.map{|i|i.editable_custom_fields}.reduce(:&).select {|field| field.format.bulk_edit_supported} @assignables = target_projects.map(&:assignable_users).reduce(:&) @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) diff --git a/app/models/custom_field_value.rb b/app/models/custom_field_value.rb index 38cffc0..eea09b2 100644 --- a/app/models/custom_field_value.rb +++ b/app/models/custom_field_value.rb @@ -52,6 +52,14 @@ class CustomFieldValue @value = custom_field.set_custom_field_value(self, v) end + def value_present? + if value.is_a?(Array) + value.any?(&:present?) + else + value.present? + end + end + def validate_value custom_field.validate_custom_value(self).each do |message| customized.errors.add(:base, custom_field.name + ' ' + message) diff --git a/app/views/issues/bulk_edit.html.erb b/app/views/issues/bulk_edit.html.erb index 29b4881..23817f8 100644 --- a/app/views/issues/bulk_edit.html.erb +++ b/app/views/issues/bulk_edit.html.erb @@ -186,6 +186,13 @@ +<% if @values_by_custom_field.present? %> +
+ <%= l(:warning_fields_cleared_on_bulk_edit) %>:
+ <%= safe_join(@values_by_custom_field.map {|field, ids| content_tag "span", "#{field.name} (#{ids.size})"}, ', ') %> +
+<% end %> +

<% if @copy %> <%= hidden_field_tag 'copy', '1' %> diff --git a/config/locales/en.yml b/config/locales/en.yml index b643ae5..7ba0599 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -219,6 +219,7 @@ en: error_no_projects_with_tracker_allowed_for_new_issue: "There are no projects with trackers for which you can create an issue" error_move_of_child_not_possible: "Subtask %{child} could not be moved to the new project: %{errors}" error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted: "Spent time cannot be reassigned to an issue that is about to be deleted" + warning_fields_cleared_on_bulk_edit: "Changes will result in the automatic deletion of values from one or more fields on the selected objects" mail_subject_lost_password: "Your %{value} password" mail_body_lost_password: 'To change your password, click on the following link:' diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 031ecc4..73aabc0 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -239,6 +239,7 @@ fr: error_no_projects_with_tracker_allowed_for_new_issue: "Aucun projet ne dispose d'un tracker sur lequel vous pouvez créer une demande" error_move_of_child_not_possible: "La sous-tâche %{child} n'a pas pu être déplacée dans le nouveau projet : %{errors}" error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted: "Le temps passé ne peut pas être réaffecté à une demande qui va être supprimée" + warning_fields_cleared_on_bulk_edit: "Les changements apportés entraîneront la suppression automatique des valeurs d'un ou plusieurs champs sur les objets sélectionnés" mail_subject_lost_password: "Votre mot de passe %{value}" mail_body_lost_password: 'Pour changer votre mot de passe, cliquez sur le lien suivant :' diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index 2e9a11c..4e8d886 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -4067,6 +4067,23 @@ class IssuesControllerTest < Redmine::ControllerTest assert_select 'input[name=?]', "issue[custom_field_values][#{field2.id}]" end + def test_bulk_edit_should_warn_about_custom_field_values_about_to_be_cleared + CustomField.delete_all + + cleared = IssueCustomField.generate!(:name => 'Cleared', :tracker_ids => [2], :is_for_all => true) + CustomValue.create!(:customized => Issue.find(2), :custom_field => cleared, :value => 'foo') + + not_cleared = IssueCustomField.generate!(:name => 'Not cleared', :tracker_ids => [2, 3], :is_for_all => true) + CustomValue.create!(:customized => Issue.find(2), :custom_field => not_cleared, :value => 'bar') + @request.session[:user_id] = 2 + + get :bulk_edit, :ids => [1, 2], :issue => {:tracker_id => 3} + assert_response :success + assert_select '.warning', :text => /automatic deletion of values/ + assert_select '.warning span', :text => 'Cleared (1)' + assert_select '.warning span', :text => /Not cleared/, :count => 0 + end + def test_bulk_update @request.session[:user_id] = 2 # update issues priority