@@ -126,6 +126,22 @@ def possible_values_options(custom_field, object=nil)
126
126
[ ]
127
127
end
128
128
129
+ def value_from_keyword ( custom_field , keyword , object )
130
+ possible_values_options = possible_values_options ( custom_field , object )
131
+ if possible_values_options . present?
132
+ keyword = keyword . to_s
133
+ if v = possible_values_options . detect { |text , id | keyword . casecmp ( text ) == 0 }
134
+ if v . is_a? ( Array )
135
+ v . last
136
+ else
137
+ v
138
+ end
139
+ end
140
+ else
141
+ keyword
142
+ end
143
+ end
144
+
129
145
# Returns the validation errors for custom_field
130
146
# Should return an empty array if custom_field is valid
131
147
def validate_custom_field ( custom_field )
@@ -659,9 +675,13 @@ class UserFormat < RecordList
659
675
field_attributes :user_role
660
676
661
677
def possible_values_options ( custom_field , object = nil )
678
+ possible_values_records ( custom_field , object ) . map { |u | [ u . name , u . id . to_s ] }
679
+ end
680
+
681
+ def possible_values_records ( custom_field , object = nil )
662
682
if object . is_a? ( Array )
663
683
projects = object . map { |o | o . respond_to? ( :project ) ? o . project : nil } . compact . uniq
664
- projects . map { |project | possible_values_options ( custom_field , project ) } . reduce ( :& ) || [ ]
684
+ projects . map { |project | possible_values_records ( custom_field , project ) } . reduce ( :& ) || [ ]
665
685
elsif object . respond_to? ( :project ) && object . project
666
686
scope = object . project . users
667
687
if custom_field . user_role . is_a? ( Array )
@@ -670,12 +690,18 @@ def possible_values_options(custom_field, object=nil)
670
690
scope = scope . where ( "#{ Member . table_name } .id IN (SELECT DISTINCT member_id FROM #{ MemberRole . table_name } WHERE role_id IN (?))" , role_ids )
671
691
end
672
692
end
673
- scope . sorted . collect { | u | [ u . to_s , u . id . to_s ] }
693
+ scope . sorted
674
694
else
675
695
[ ]
676
696
end
677
697
end
678
698
699
+ def value_from_keyword ( custom_field , keyword , object )
700
+ users = possible_values_records ( custom_field , object ) . to_a
701
+ user = Principal . detect_by_keyword ( users , keyword )
702
+ user ? user . id : nil
703
+ end
704
+
679
705
def before_custom_field_save ( custom_field )
680
706
super
681
707
if custom_field . user_role . is_a? ( Array )
0 commit comments