def value_from_keyword(custom_field, keyword, object)
possible_values_options = possible_values_options(custom_field, object)
if possible_values_options.present?
- keyword = keyword.to_s
- if v = possible_values_options.detect {|text, id| keyword.casecmp(text) == 0}
- if v.is_a?(Array)
- v.last
- else
- v
+ parse_keyword(custom_field, keyword) do |k|
+ if v = possible_values_options.detect {|text, id| k.casecmp(text) == 0}
+ if v.is_a?(Array)
+ v.last
+ else
+ v
+ end
end
end
else
end
end
+ def parse_keyword(custom_field, keyword, &block)
+ separator = Regexp.escape ","
+ keyword = keyword.to_s
+
+ if custom_field.multiple?
+ values = []
+ while keyword.length > 0
+ k = keyword.dup
+ loop do
+ if value = yield(k.strip)
+ values << value
+ break
+ elsif k.slice!(/#{separator}([^#{separator}]*)\Z/).nil?
+ break
+ end
+ end
+ keyword.slice!(/\A#{Regexp.escape k}#{separator}?/)
+ end
+ values
+ else
+ yield keyword.strip
+ end
+ end
+ protected :parse_keyword
+
# Returns the validation errors for custom_field
# Should return an empty array if custom_field is valid
def validate_custom_field(custom_field)
end
def value_from_keyword(custom_field, keyword, object)
- value = custom_field.enumerations.where("LOWER(name) LIKE LOWER(?)", keyword).first
- value ? value.id : nil
+ parse_keyword(custom_field, keyword) do |k|
+ custom_field.enumerations.where("LOWER(name) LIKE LOWER(?)", k).first.try(:id)
+ end
end
end
def value_from_keyword(custom_field, keyword, object)
users = possible_values_records(custom_field, object).to_a
- user = Principal.detect_by_keyword(users, keyword)
- user ? user.id : nil
+ parse_keyword(custom_field, keyword) do |k|
+ Principal.detect_by_keyword(users, k).try(:id)
+ end
end
def before_custom_field_save(custom_field)
category: Stock management
searchable field: Value for a custom field
Database: postgresql
+OS: Mac OS X ,windows
def test_value_from_keyword_should_return_enumeration_id
assert_equal @foo.id, @field.value_from_keyword('foo', nil)
+ assert_nil @field.value_from_keyword('baz', nil)
+ end
+
+ def test_value_from_keyword_for_multiple_custom_field_should_return_enumeration_ids
+ @field.multiple = true
+ @field.save!
+ assert_equal [@foo.id, @bar.id], @field.value_from_keyword('foo, bar', nil)
+ assert_equal [@foo.id], @field.value_from_keyword('foo, baz', nil)
+ assert_equal [], @field.value_from_keyword('baz', nil)
end
end
end
end
end
+
+ def test_value_from_keyword_should_return_value
+ field = GroupCustomField.create!(:name => 'List', :field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz,qux'])
+
+ assert_equal 'Foo', field.value_from_keyword('foo', nil)
+ assert_equal 'Baz,qux', field.value_from_keyword('baz,qux', nil)
+ assert_nil field.value_from_keyword('invalid', nil)
+ end
+
+ def test_value_from_keyword_for_multiple_custom_field_should_return_values
+ field = GroupCustomField.create!(:name => 'List', :field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz,qux'], :multiple => true)
+
+ assert_equal ['Foo','Bar'], field.value_from_keyword('foo,bar', nil)
+ assert_equal ['Baz,qux'], field.value_from_keyword('baz,qux', nil)
+ assert_equal ['Baz,qux', 'Foo'], field.value_from_keyword('baz,qux,foo', nil)
+ assert_equal ['Foo'], field.value_from_keyword('foo,invalid', nil)
+ assert_equal ['Foo'], field.value_from_keyword(',foo,', nil)
+ assert_equal ['Foo'], field.value_from_keyword(',foo, ,,', nil)
+ assert_equal [], field.value_from_keyword('invalid', nil)
+ end
end
assert_equal ['Dave Lopper'], field.possible_values_options(project).map(&:first)
end
+
+ def test_value_from_keyword_should_return_user_id
+ field = IssueCustomField.new(:field_format => 'user')
+ project = Project.find(1)
+
+ assert_equal 2, field.value_from_keyword('jsmith', project)
+ assert_equal 3, field.value_from_keyword('Dave Lopper', project)
+ assert_nil field.value_from_keyword('Unknown User', project)
+ end
+
+ def test_value_from_keyword_for_multiple_custom_field_should_return_enumeration_ids
+ field = IssueCustomField.new(:field_format => 'user', :multiple => true)
+ project = Project.find(1)
+
+ assert_equal [2, 3], field.value_from_keyword('jsmith, Dave Lopper', project)
+ assert_equal [2], field.value_from_keyword('jsmith', project)
+ assert_equal [], field.value_from_keyword('Unknown User', project)
+ end
end
end
def test_add_issue_with_custom_fields
+ mutiple = IssueCustomField.generate!(:field_format => 'list', :name => 'OS', :multiple => true, :possible_values => ['Linux', 'Windows', 'Mac OS X'])
+
issue = submit_email('ticket_with_custom_fields.eml',
- :issue => {:project => 'onlinestore'}, :allow_override => ['database', 'Searchable_field']
+ :issue => {:project => 'onlinestore'}, :allow_override => ['database', 'Searchable_field', 'OS']
)
assert issue.is_a?(Issue)
assert !issue.new_record?
assert_equal 'New ticket with custom field values', issue.subject
assert_equal 'PostgreSQL', issue.custom_field_value(1)
assert_equal 'Value for a custom field', issue.custom_field_value(2)
+ assert_equal ['Mac OS X', 'Windows'], issue.custom_field_value(mutiple.id).sort
assert !issue.description.match(/^searchable field:/i)
end