Commit 0cb7e296 authored by jplang's avatar jplang

Use same logic for finding user as a custom field (#950).

git-svn-id: https://svn.redmine.org/redmine/trunk@14503 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent bab5fac9
......@@ -141,19 +141,7 @@ class CustomField < ActiveRecord::Base
end
def value_from_keyword(keyword, customized)
possible_values_options = possible_values_options(customized)
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
end
end
else
keyword
end
format.value_from_keyword(self, keyword, customized)
end
# Returns a ORDER BY clause that can used to sort customized
......
......@@ -126,6 +126,22 @@ module Redmine
[]
end
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
end
end
else
keyword
end
end
# Returns the validation errors for custom_field
# Should return an empty array if custom_field is valid
def validate_custom_field(custom_field)
......@@ -659,9 +675,13 @@ module Redmine
field_attributes :user_role
def possible_values_options(custom_field, object=nil)
possible_values_records(custom_field, object).map {|u| [u.name, u.id.to_s]}
end
def possible_values_records(custom_field, object=nil)
if object.is_a?(Array)
projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
projects.map {|project| possible_values_records(custom_field, project)}.reduce(:&) || []
elsif object.respond_to?(:project) && object.project
scope = object.project.users
if custom_field.user_role.is_a?(Array)
......@@ -670,12 +690,18 @@ module Redmine
scope = scope.where("#{Member.table_name}.id IN (SELECT DISTINCT member_id FROM #{MemberRole.table_name} WHERE role_id IN (?))", role_ids)
end
end
scope.sorted.collect {|u| [u.to_s, u.id.to_s]}
scope.sorted
else
[]
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
end
def before_custom_field_save(custom_field)
super
if custom_field.user_role.is_a?(Array)
......
......@@ -78,6 +78,16 @@ class IssueImportTest < ActiveSupport::TestCase
assert_equal [User.find(3), nil, nil], issues.map(&:assigned_to)
end
def test_user_custom_field_should_be_set
field = IssueCustomField.generate!(:field_format => 'user', :is_for_all => true, :trackers => Tracker.all)
import = generate_import_with_mapping
import.mapping.merge!("cf_#{field.id}" => '11')
import.save!
issues = new_records(Issue, 3) { import.run }
assert_equal '3', issues.first.custom_field_value(field)
end
def test_is_private_should_be_set_based_on_user_locale
import = generate_import_with_mapping
import.mapping.merge!('is_private' => '6')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment