From 9d5e2c14f899ec3107020b66038e45045e46ab41 Mon Sep 17 00:00:00 2001 From: Yiannis Tsiouris Date: Wed, 10 Aug 2016 21:48:37 +0300 Subject: Set multiple values via emails for list custom fields --- lib/redmine/field_format.rb | 24 ++++++++++++++++++---- test/fixtures/custom_fields.yml | 11 ++++++++++ test/fixtures/custom_fields_projects.yml | 3 +++ test/fixtures/custom_fields_trackers.yml | 3 +++ .../mail_handler/ticket_with_custom_fields.eml | 1 + test/unit/mail_handler_test.rb | 3 ++- 6 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/redmine/field_format.rb b/lib/redmine/field_format.rb index f6b120d..6a9f821 100644 --- a/lib/redmine/field_format.rb +++ b/lib/redmine/field_format.rb @@ -144,16 +144,32 @@ module Redmine 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 custom_field.multiple? + possible_options = possible_values_options.collect {|option, value| option} + keywords_re = + / + (?:\A|,\s*) # Start of the line or separator (,) and some whitespace + (#{Regexp.union(possible_options).source}) # one of the possible options + \s* # some whitespace before either the next separator (,) or the end + /xi + result = [] + keyword.scan(keywords_re) do |match| + v = possible_values_options.detect {|text, id| match.first.casecmp(text) == 0} + v = v.last if v.is_a?(Array) + result << v + end + result = result.uniq + elsif v = possible_values_options.detect {|text, id| keyword.casecmp(text) == 0} if v.is_a?(Array) - v.last + result = v.last else - v + result = v end end else - keyword + result = keyword end + result end # Returns the validation errors for custom_field diff --git a/test/fixtures/custom_fields.yml b/test/fixtures/custom_fields.yml index 98dccdf..1937bdc 100644 --- a/test/fixtures/custom_fields.yml +++ b/test/fixtures/custom_fields.yml @@ -146,3 +146,14 @@ custom_fields_011: - Other value field_format: list position: 1 +custom_fields_012: + id: 12 + name: OS + type: IssueCustomField + possible_values: + - Linux + - Windows + - Mac OS X + field_format: list + multiple: true + position: 1 diff --git a/test/fixtures/custom_fields_projects.yml b/test/fixtures/custom_fields_projects.yml index bb9788b..265a360 100644 --- a/test/fixtures/custom_fields_projects.yml +++ b/test/fixtures/custom_fields_projects.yml @@ -2,3 +2,6 @@ custom_fields_projects_001: custom_field_id: 9 project_id: 1 +custom_fields_projects_002: + custom_field_id: 12 + project_id: 2 diff --git a/test/fixtures/custom_fields_trackers.yml b/test/fixtures/custom_fields_trackers.yml index fc01b11..8f81915 100644 --- a/test/fixtures/custom_fields_trackers.yml +++ b/test/fixtures/custom_fields_trackers.yml @@ -29,3 +29,6 @@ custom_fields_trackers_009: custom_fields_trackers_010: custom_field_id: 9 tracker_id: 1 +custom_fields_trackers_011: + custom_field_id: 12 + tracker_id: 1 diff --git a/test/fixtures/mail_handler/ticket_with_custom_fields.eml b/test/fixtures/mail_handler/ticket_with_custom_fields.eml index 58dde7e..177ec5f 100644 --- a/test/fixtures/mail_handler/ticket_with_custom_fields.eml +++ b/test/fixtures/mail_handler/ticket_with_custom_fields.eml @@ -40,3 +40,4 @@ pulvinar dui, a gravida orci mi eget odio. Nunc a lacus. category: Stock management searchable field: Value for a custom field Database: postgresql +OS: Mac OS X ,windows diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index c1afdae..baa89ca 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -233,7 +233,7 @@ class MailHandlerTest < ActiveSupport::TestCase def test_add_issue_with_custom_fields 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? @@ -241,6 +241,7 @@ class MailHandlerTest < ActiveSupport::TestCase 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(12).sort assert !issue.description.match(/^searchable field:/i) end -- 2.1.4