]> source.dussan.org Git - redmine.git/commitdiff
CustomField#possible_values may raise undefined method `force_encoding' error (#19316).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 14 Mar 2015 08:25:20 +0000 (08:25 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 14 Mar 2015 08:25:20 +0000 (08:25 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@14079 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/custom_field.rb
test/unit/custom_field_test.rb

index 02d1833973fbdc3387a733d716e3c8aa99f36512..3fbb1edc23b5942b7dc9bff163394437e7256c60 100644 (file)
@@ -118,7 +118,7 @@ class CustomField < ActiveRecord::Base
     values = read_attribute(:possible_values)
     if values.is_a?(Array)
       values.each do |value|
-        value.force_encoding('UTF-8')
+        value.to_s.force_encoding('UTF-8')
       end
       values
     else
@@ -129,7 +129,7 @@ class CustomField < ActiveRecord::Base
   # Makes possible_values accept a multiline string
   def possible_values=(arg)
     if arg.is_a?(Array)
-      values = arg.compact.collect(&:strip).select {|v| !v.blank?}
+      values = arg.compact.map {|a| a.to_s.strip}.reject(&:blank?)
       write_attribute(:possible_values, values)
     else
       self.possible_values = arg.to_s.split(/[\n\r]+/)
index 815939d33853257bcc5a765d77e1f7bd52e6301b..f40875137401cc08824b83f8f230df317ea2edc3 100644 (file)
@@ -85,12 +85,26 @@ class CustomFieldTest < ActiveSupport::TestCase
     assert_equal ["One value"], field.possible_values
   end
 
+  def test_possible_values_should_stringify_values
+    field = CustomField.new
+    field.possible_values = [1, 2]
+    assert_equal ["1", "2"], field.possible_values
+  end
+
   def test_possible_values_should_accept_a_string
     field = CustomField.new
     field.possible_values = "One value"
     assert_equal ["One value"], field.possible_values
   end
 
+  def test_possible_values_should_return_utf8_encoded_strings
+    field = CustomField.new
+    s = "Value".force_encoding('BINARY')
+    field.possible_values = s
+    assert_equal [s], field.possible_values
+    assert_equal 'UTF-8', field.possible_values.first.encoding.name
+  end
+
   def test_possible_values_should_accept_a_multiline_string
     field = CustomField.new
     field.possible_values = "One value\nAnd another one  \r\n \n"