]> source.dussan.org Git - redmine.git/commitdiff
Accept custom field format added at runtime (#15277).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 9 Nov 2013 09:07:48 +0000 (09:07 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 9 Nov 2013 09:07:48 +0000 (09:07 +0000)
Patch by Boris Bera.

git-svn-id: http://svn.redmine.org/redmine/trunk@12248 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/custom_field.rb
lib/redmine/custom_field_format.rb
test/unit/custom_field_test.rb

index 34b9fb53d3246f2c62212f4cb1196cd34c1efcb1..055516c38ef8af3cf31f9eb667d62e0ba293a54f 100644 (file)
@@ -26,7 +26,7 @@ class CustomField < ActiveRecord::Base
   validates_presence_of :name, :field_format
   validates_uniqueness_of :name, :scope => :type
   validates_length_of :name, :maximum => 30
-  validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
+  validates_inclusion_of :field_format, :in => Proc.new { Redmine::CustomFieldFormat.available_formats }
   validate :validate_custom_field
 
   before_validation :set_searchable
index f7cd5d1c6968d2855b5acd6e316ace15629dfdfd..c539aee2e7e7fea2d697f7d597323ad094b7d863 100644 (file)
@@ -70,6 +70,13 @@ module Redmine
         @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
       end
 
+      def delete(format)
+        if format.is_a?(Redmine::CustomFieldFormat)
+          format = format.name
+        end
+        @@available.delete(format)
+      end
+
       def available_formats
         @@available.keys
       end
index 051853abc3eb3b3d0a8429352adc7ecaf91f8fd1..b99fb435351e95e67507016cbc43c1db6bd6d833 100644 (file)
@@ -57,6 +57,20 @@ class CustomFieldTest < ActiveSupport::TestCase
     assert field.valid?
   end
 
+  def test_field_format_should_be_validated
+    field = CustomField.new(:name => 'Test', :field_format => 'foo')
+    assert !field.valid?
+  end
+
+  def test_field_format_validation_should_accept_formats_added_at_runtime
+    Redmine::CustomFieldFormat.register 'foobar'
+
+    field = CustomField.new(:name => 'Some Custom Field', :field_format => 'foobar')
+    assert field.valid?, 'field should be valid'
+  ensure
+    Redmine::CustomFieldFormat.delete 'foobar'
+  end
+
   def test_should_not_change_field_format_of_existing_custom_field
     field = CustomField.find(1)
     field.field_format = 'int'