From e856c9f879f15f31317ddfcf35042e799b90f783 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 23 Sep 2018 08:25:25 +0000 Subject: [PATCH] Fixed that issue details page shows default values for custom fields that aren't actually set (#25726). git-svn-id: http://svn.redmine.org/redmine/trunk@17499 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/custom_value.rb | 2 +- app/models/issue.rb | 5 +++++ .../lib/acts_as_customizable.rb | 6 ++++++ test/functional/issues_controller_test.rb | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb index dc511c0d7..734c76b22 100644 --- a/app/models/custom_value.rb +++ b/app/models/custom_value.rb @@ -23,7 +23,7 @@ class CustomValue < ActiveRecord::Base def initialize(attributes=nil, *args) super - if new_record? && custom_field && !attributes.key?(:value) + if new_record? && custom_field && !attributes.key?(:value) && (customized.nil? || customized.set_custom_field_default?(self)) self.value ||= custom_field.default_value end end diff --git a/app/models/issue.rb b/app/models/issue.rb index 090677153..b9d8355ec 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -259,6 +259,11 @@ class Issue < ActiveRecord::Base end end + # Overrides Redmine::Acts::Customizable::InstanceMethods#set_custom_field_default? + def set_custom_field_default?(custom_value) + new_record? || project_id_changed?|| tracker_id_changed? + end + # Copies attributes from another issue, arg can be an id or an Issue def copy_from(arg, options={}) issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg) diff --git a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb index e7805cbcf..72cc30335 100644 --- a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb +++ b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb @@ -104,6 +104,12 @@ module Redmine @custom_field_values_changed == true end + # Should the default custom field value be set for the given custom_value? + # By default, default custom field value is set for new objects only + def set_custom_field_default?(custom_value) + new_record? + end + def custom_value_for(c) field_id = (c.is_a?(CustomField) ? c.id : c.to_i) custom_values.detect {|v| v.custom_field_id == field_id } diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index d01b990f9..0e2505eef 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -2134,6 +2134,20 @@ class IssuesControllerTest < Redmine::ControllerTest end end + def test_show_should_not_display_default_value_for_new_custom_field + prior = Issue.generate! + field = IssueCustomField.generate!(:name => 'WithDefault', :field_format => 'string', :default_value => 'DEFAULT') + after = Issue.generate! + + get :show, :params => {:id => prior.id} + assert_response :success + assert_select ".cf_#{field.id} .value", :text => '' + + get :show, :params => {:id => after.id} + assert_response :success + assert_select ".cf_#{field.id} .value", :text => 'DEFAULT' + end + def test_show_should_display_private_notes_with_permission_only journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1) @request.session[:user_id] = 2