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
# 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]+/)
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"