]> source.dussan.org Git - redmine.git/commitdiff
New custom fields of existing issues are not initialized with their default value...
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 31 Oct 2015 09:17:29 +0000 (09:17 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 31 Oct 2015 09:17:29 +0000 (09:17 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@14773 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/custom_value.rb
lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb
test/integration/api_test/issues_test.rb
test/unit/custom_value_test.rb

index ab6cc8fbc88d5f193393326340a31d20bf0beac5..d89bfbb13f4ee77157930d61cac758031b90357a 100644 (file)
@@ -22,7 +22,7 @@ class CustomValue < ActiveRecord::Base
 
   def initialize(attributes=nil, *args)
     super
-    if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?))
+    if new_record? && custom_field && !attributes.key?(:value)
       self.value ||= custom_field.default_value
     end
   end
index 9c4af77d48bc02cca9f64ae1b88d0f773ffd1ee6..6360e4e906c3ebe03071e7e23953e1962776df9b 100644 (file)
@@ -92,12 +92,12 @@ module Redmine
             if field.multiple?
               values = custom_values.select { |v| v.custom_field == field }
               if values.empty?
-                values << custom_values.build(:customized => self, :custom_field => field, :value => nil)
+                values << custom_values.build(:customized => self, :custom_field => field)
               end
               x.value = values.map(&:value)
             else
               cv = custom_values.detect { |v| v.custom_field == field }
-              cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil)
+              cv ||= custom_values.build(:customized => self, :custom_field => field)
               x.value = cv.value
             end
             x.value_was = x.value.dup if x.value
index 18dbfaad9bdd0b40ac5592bd885db9daa723c741..5a7d64d47fe70a72af4ec9649b5752cb93986959 100644 (file)
@@ -562,7 +562,7 @@ JSON
     assert_equal "", issue.reload.custom_field_value(field)
   end
 
-  test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker does not set default value" do
+  test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker should set default value" do
     field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
     issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
 
@@ -573,7 +573,7 @@ JSON
     end
 
     assert_equal 2, issue.reload.tracker_id
-    assert_nil issue.reload.custom_field_value(field)
+    assert_equal "Default", issue.reload.custom_field_value(field)
   end
 
   test "PUT /issues/:id.json with tracker change and custom field specific to that tracker set to blank should not set default value" do
index b344994a40b4be38bf41ad6c8f698f31c003def7..954c3aee9b8a48822c13b8bcc82e0a06f7463f19 100644 (file)
@@ -20,15 +20,25 @@ require File.expand_path('../../test_helper', __FILE__)
 class CustomValueTest < ActiveSupport::TestCase
   fixtures :custom_fields, :custom_values, :users
 
-  def test_default_value
-    field = CustomField.find_by_default_value('Default string')
-    assert_not_nil field
+  def test_new_without_value_should_set_default_value
+    field = CustomField.generate!(:default_value => 'Default string')
 
     v = CustomValue.new(:custom_field => field)
     assert_equal 'Default string', v.value
+  end
+
+  def test_new_with_value_should_not_set_default_value
+    field = CustomField.generate!(:default_value => 'Default string')
+
+    v = CustomValue.new(:custom_field => field, :value => 'String')
+    assert_equal 'String', v.value
+  end
+
+  def test_new_with_nil_value_should_not_set_default_value
+    field = CustomField.generate!(:default_value => 'Default string')
 
-    v = CustomValue.new(:custom_field => field, :value => 'Not empty')
-    assert_equal 'Not empty', v.value
+    v = CustomValue.new(:custom_field => field, :value => nil)
+    assert_nil v.value
   end
 
   def test_sti_polymorphic_association