@@ -0,0 +1,65 | |||
|
1 | # Redmine - project management software | |
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
|
3 | # | |
|
4 | # This program is free software; you can redistribute it and/or | |
|
5 | # modify it under the terms of the GNU General Public License | |
|
6 | # as published by the Free Software Foundation; either version 2 | |
|
7 | # of the License, or (at your option) any later version. | |
|
8 | # | |
|
9 | # This program is distributed in the hope that it will be useful, | |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
12 | # GNU General Public License for more details. | |
|
13 | # | |
|
14 | # You should have received a copy of the GNU General Public License | |
|
15 | # along with this program; if not, write to the Free Software | |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
|
17 | ||
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
|
19 | ||
|
20 | class CustomFieldUserFormatTest < ActiveSupport::TestCase | |
|
21 | fixtures :custom_fields, :projects, :members, :users, :member_roles, :trackers, :issues | |
|
22 | ||
|
23 | def setup | |
|
24 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user') | |
|
25 | end | |
|
26 | ||
|
27 | def test_possible_values_with_no_arguments | |
|
28 | assert_equal [], @field.possible_values | |
|
29 | assert_equal [], @field.possible_values(nil) | |
|
30 | end | |
|
31 | ||
|
32 | def test_possible_values_with_project_resource | |
|
33 | project = Project.find(1) | |
|
34 | possible_values = @field.possible_values(project.issues.first) | |
|
35 | assert possible_values.any? | |
|
36 | assert_equal project.users.sort.collect(&:id).map(&:to_s), possible_values | |
|
37 | end | |
|
38 | ||
|
39 | def test_possible_values_options_with_no_arguments | |
|
40 | assert_equal [], @field.possible_values_options | |
|
41 | assert_equal [], @field.possible_values_options(nil) | |
|
42 | end | |
|
43 | ||
|
44 | def test_possible_values_options_with_project_resource | |
|
45 | project = Project.find(1) | |
|
46 | possible_values_options = @field.possible_values_options(project.issues.first) | |
|
47 | assert possible_values_options.any? | |
|
48 | assert_equal project.users.sort.map {|u| [u.name, u.id.to_s]}, possible_values_options | |
|
49 | end | |
|
50 | ||
|
51 | def test_cast_blank_value | |
|
52 | assert_equal nil, @field.cast_value(nil) | |
|
53 | assert_equal nil, @field.cast_value("") | |
|
54 | end | |
|
55 | ||
|
56 | def test_cast_valid_value | |
|
57 | user = @field.cast_value("2") | |
|
58 | assert_kind_of User, user | |
|
59 | assert_equal User.find(2), user | |
|
60 | end | |
|
61 | ||
|
62 | def test_cast_invalid_value | |
|
63 | assert_equal nil, @field.cast_value("187") | |
|
64 | end | |
|
65 | end |
@@ -49,7 +49,7 module CustomFieldsHelper | |||
|
49 | 49 | blank_option = custom_field.is_required? ? |
|
50 | 50 | (custom_field.default_value.blank? ? "<option value=\"\">--- #{l(:actionview_instancetag_blank_option)} ---</option>" : '') : |
|
51 | 51 | '<option></option>' |
|
52 | select_tag(field_name, blank_option + options_for_select(custom_field.possible_values, custom_value.value), :id => field_id) | |
|
52 | select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value), :id => field_id) | |
|
53 | 53 | else |
|
54 | 54 | text_field_tag(field_name, custom_value.value, :id => field_id) |
|
55 | 55 | end |
@@ -83,7 +83,7 module CustomFieldsHelper | |||
|
83 | 83 | [l(:general_text_yes), '1'], |
|
84 | 84 | [l(:general_text_no), '0']]), :id => field_id) |
|
85 | 85 | when "list" |
|
86 | select_tag(field_name, options_for_select([[l(:label_no_change_option), '']] + custom_field.possible_values), :id => field_id) | |
|
86 | select_tag(field_name, options_for_select([[l(:label_no_change_option), '']] + custom_field.possible_values_options), :id => field_id) | |
|
87 | 87 | else |
|
88 | 88 | text_field_tag(field_name, '', :id => field_id) |
|
89 | 89 | end |
@@ -101,8 +101,8 module CustomFieldsHelper | |||
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | # Return an array of custom field formats which can be used in select_tag |
|
104 | def custom_field_formats_for_select | |
|
105 | Redmine::CustomFieldFormat.as_select | |
|
104 | def custom_field_formats_for_select(custom_field) | |
|
105 | Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name) | |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | # Renders the custom_values in api views |
@@ -1,5 +1,5 | |||
|
1 |
# |
|
|
2 | # Copyright (C) 2006 Jean-Philippe Lang | |
|
1 | # Redmine - project management software | |
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
@@ -48,6 +48,33 class CustomField < ActiveRecord::Base | |||
|
48 | 48 | errors.add(:default_value, :invalid) unless v.valid? |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | def possible_values_options(obj=nil) | |
|
52 | case field_format | |
|
53 | when 'user', 'version' | |
|
54 | if obj.respond_to?(:project) | |
|
55 | case field_format | |
|
56 | when 'user' | |
|
57 | obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} | |
|
58 | when 'version' | |
|
59 | obj.project.versions.sort.collect {|u| [u.to_s, u.id.to_s]} | |
|
60 | end | |
|
61 | else | |
|
62 | [] | |
|
63 | end | |
|
64 | else | |
|
65 | read_attribute :possible_values | |
|
66 | end | |
|
67 | end | |
|
68 | ||
|
69 | def possible_values(obj=nil) | |
|
70 | case field_format | |
|
71 | when 'user' | |
|
72 | possible_values_options(obj).collect(&:last) | |
|
73 | else | |
|
74 | read_attribute :possible_values | |
|
75 | end | |
|
76 | end | |
|
77 | ||
|
51 | 78 | # Makes possible_values accept a multiline string |
|
52 | 79 | def possible_values=(arg) |
|
53 | 80 | if arg.is_a?(Array) |
@@ -71,6 +98,8 class CustomField < ActiveRecord::Base | |||
|
71 | 98 | casted = value.to_i |
|
72 | 99 | when 'float' |
|
73 | 100 | casted = value.to_f |
|
101 | when 'user', 'version' | |
|
102 | casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) | |
|
74 | 103 | end |
|
75 | 104 | end |
|
76 | 105 | casted |
@@ -1,5 +1,5 | |||
|
1 | 1 | # Redmine - project management software |
|
2 |
# Copyright (C) 2006-20 |
|
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
@@ -636,6 +636,9 class Query < ActiveRecord::Base | |||
|
636 | 636 | options = { :type => :date, :order => 20 } |
|
637 | 637 | when "bool" |
|
638 | 638 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
639 | when "user", "version" | |
|
640 | next unless project | |
|
641 | options = { :type => :list_optional, :values => field.possible_values_options(project), :order => 20} | |
|
639 | 642 | else |
|
640 | 643 | options = { :type => :string, :order => 20 } |
|
641 | 644 | end |
@@ -40,6 +40,14 function toggle_custom_field_format() { | |||
|
40 | 40 | if (p_searchable) Element.hide(p_searchable.parentNode); |
|
41 | 41 | Element.hide(p_values.parentNode); |
|
42 | 42 | break; |
|
43 | case "user": | |
|
44 | case "version": | |
|
45 | Element.hide(p_length.parentNode); | |
|
46 | Element.hide(p_regexp.parentNode); | |
|
47 | if (p_searchable) Element.hide(p_searchable.parentNode); | |
|
48 | Element.hide(p_values.parentNode); | |
|
49 | Element.hide(p_default.parentNode); | |
|
50 | break; | |
|
43 | 51 | default: |
|
44 | 52 | Element.show(p_length.parentNode); |
|
45 | 53 | Element.show(p_regexp.parentNode); |
@@ -54,7 +62,7 function toggle_custom_field_format() { | |||
|
54 | 62 | |
|
55 | 63 | <div class="box"> |
|
56 | 64 | <p><%= f.text_field :name, :required => true %></p> |
|
57 | <p><%= f.select :field_format, custom_field_formats_for_select, {}, :onchange => "toggle_custom_field_format();", | |
|
65 | <p><%= f.select :field_format, custom_field_formats_for_select(@custom_field), {}, :onchange => "toggle_custom_field_format();", | |
|
58 | 66 | :disabled => !@custom_field.new_record? %></p> |
|
59 | 67 | <p><label for="custom_field_min_length"><%=l(:label_min_max_length)%></label> |
|
60 | 68 | <%= f.text_field :min_length, :size => 5, :no_label => true %> - |
@@ -41,6 +41,8 Redmine::CustomFieldFormat.map do |fields| | |||
|
41 | 41 | fields.register Redmine::CustomFieldFormat.new('list', :label => :label_list, :order => 5) |
|
42 | 42 | fields.register Redmine::CustomFieldFormat.new('date', :label => :label_date, :order => 6) |
|
43 | 43 | fields.register Redmine::CustomFieldFormat.new('bool', :label => :label_boolean, :order => 7) |
|
44 | fields.register Redmine::CustomFieldFormat.new('user', :label => :label_user, :only => %w(Issue TimeEntry Version Project), :edit_as => 'list', :order => 8) | |
|
45 | fields.register Redmine::CustomFieldFormat.new('version', :label => :label_version, :only => %w(Issue TimeEntry Version Project), :edit_as => 'list', :order => 9) | |
|
44 | 46 | end |
|
45 | 47 | |
|
46 | 48 | # Permissions |
@@ -22,12 +22,14 module Redmine | |||
|
22 | 22 | cattr_accessor :available |
|
23 | 23 | @@available = {} |
|
24 | 24 | |
|
25 | attr_accessor :name, :order, :label | |
|
25 | attr_accessor :name, :order, :label, :edit_as, :class_names | |
|
26 | 26 | |
|
27 | 27 | def initialize(name, options={}) |
|
28 | 28 | self.name = name |
|
29 | 29 | self.label = options[:label] |
|
30 | 30 | self.order = options[:order] |
|
31 | self.edit_as = options[:edit_as] || name | |
|
32 | self.class_names = options[:only] | |
|
31 | 33 | end |
|
32 | 34 | |
|
33 | 35 | def format(value) |
@@ -47,12 +49,11 module Redmine | |||
|
47 | 49 | return value |
|
48 | 50 | } |
|
49 | 51 | end |
|
50 | ||
|
51 | # Allow displaying the edit type of another field_format | |
|
52 | # | |
|
53 | # Example: display a custom field as a list | |
|
54 | def edit_as | |
|
55 | name | |
|
52 | ||
|
53 | ['user', 'version'].each do |name| | |
|
54 | define_method("format_as_#{name}") {|value| | |
|
55 | return value.blank? ? "" : name.classify.constantize.find_by_id(value.to_i).to_s | |
|
56 | } | |
|
56 | 57 | end |
|
57 | 58 | |
|
58 | 59 | class << self |
@@ -79,8 +80,10 module Redmine | |||
|
79 | 80 | end |
|
80 | 81 | |
|
81 | 82 | # Return an array of custom field formats which can be used in select_tag |
|
82 | def as_select | |
|
83 |
@@available.values |
|
|
83 | def as_select(class_name=nil) | |
|
84 | fields = @@available.values | |
|
85 | fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)} | |
|
86 | fields.sort {|a,b| | |
|
84 | 87 | a.order <=> b.order |
|
85 | 88 | }.collect {|custom_field_format| |
|
86 | 89 | [ l(custom_field_format.label), custom_field_format.name ] |
@@ -1,5 +1,5 | |||
|
1 | 1 | # Redmine - project management software |
|
2 |
# Copyright (C) 2006-20 |
|
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
@@ -31,6 +31,31 class CustomFieldsControllerTest < ActionController::TestCase | |||
|
31 | 31 | @request.session[:user_id] = 1 |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | def test_get_new_issue_custom_field | |
|
35 | get :new, :type => 'IssueCustomField' | |
|
36 | assert_response :success | |
|
37 | assert_template 'new' | |
|
38 | assert_tag :select, | |
|
39 | :attributes => {:name => 'custom_field[field_format]'}, | |
|
40 | :child => { | |
|
41 | :tag => 'option', | |
|
42 | :attributes => {:value => 'user'}, | |
|
43 | :content => 'User' | |
|
44 | } | |
|
45 | assert_tag :select, | |
|
46 | :attributes => {:name => 'custom_field[field_format]'}, | |
|
47 | :child => { | |
|
48 | :tag => 'option', | |
|
49 | :attributes => {:value => 'version'}, | |
|
50 | :content => 'Version' | |
|
51 | } | |
|
52 | end | |
|
53 | ||
|
54 | def test_get_new_with_invalid_custom_field_class_should_redirect_to_list | |
|
55 | get :new, :type => 'UnknownCustomField' | |
|
56 | assert_redirected_to '/custom_fields' | |
|
57 | end | |
|
58 | ||
|
34 | 59 | def test_post_new_list_custom_field |
|
35 | 60 | assert_difference 'CustomField.count' do |
|
36 | 61 | post :new, :type => "IssueCustomField", |
@@ -53,9 +78,4 class CustomFieldsControllerTest < ActionController::TestCase | |||
|
53 | 78 | assert_equal ["0.1", "0.2"], field.possible_values |
|
54 | 79 | assert_equal 1, field.trackers.size |
|
55 | 80 | end |
|
56 | ||
|
57 | def test_invalid_custom_field_class_should_redirect_to_list | |
|
58 | get :new, :type => 'UnknownCustomField' | |
|
59 | assert_redirected_to '/custom_fields' | |
|
60 | end | |
|
61 | 81 | end |
@@ -1,5 +1,5 | |||
|
1 |
# |
|
|
2 |
# Copyright (C) 2006-20 |
|
|
1 | # Redmine - project management software | |
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
@@ -126,4 +126,75 class IssuesTest < ActionController::IntegrationTest | |||
|
126 | 126 | :attributes => { :href => '/projects/ecookbook/issues?page=2' } |
|
127 | 127 | |
|
128 | 128 | end |
|
129 | ||
|
130 | def test_issue_with_user_custom_field | |
|
131 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :trackers => Tracker.all) | |
|
132 | Role.anonymous.add_permission! :add_issues, :edit_issues | |
|
133 | users = Project.find(1).users | |
|
134 | tester = users.first | |
|
135 | ||
|
136 | # Issue form | |
|
137 | get '/projects/ecookbook/issues/new' | |
|
138 | assert_response :success | |
|
139 | assert_tag :select, | |
|
140 | :attributes => {:name => "issue[custom_field_values][#{@field.id}]"}, | |
|
141 | :children => {:count => (users.size + 1)}, # +1 for blank value | |
|
142 | :child => { | |
|
143 | :tag => 'option', | |
|
144 | :attributes => {:value => tester.id.to_s}, | |
|
145 | :content => tester.name | |
|
146 | } | |
|
147 | ||
|
148 | # Create issue | |
|
149 | assert_difference 'Issue.count' do | |
|
150 | post '/projects/ecookbook/issues', | |
|
151 | :issue => { | |
|
152 | :tracker_id => '1', | |
|
153 | :priority_id => '4', | |
|
154 | :subject => 'Issue with user custom field', | |
|
155 | :custom_field_values => {@field.id.to_s => users.first.id.to_s} | |
|
156 | } | |
|
157 | end | |
|
158 | issue = Issue.first(:order => 'id DESC') | |
|
159 | assert_response 302 | |
|
160 | ||
|
161 | # Issue view | |
|
162 | follow_redirect! | |
|
163 | assert_tag :th, | |
|
164 | :content => /Tester/, | |
|
165 | :sibling => { | |
|
166 | :tag => 'td', | |
|
167 | :content => tester.name | |
|
168 | } | |
|
169 | assert_tag :select, | |
|
170 | :attributes => {:name => "issue[custom_field_values][#{@field.id}]"}, | |
|
171 | :children => {:count => (users.size + 1)}, # +1 for blank value | |
|
172 | :child => { | |
|
173 | :tag => 'option', | |
|
174 | :attributes => {:value => tester.id.to_s, :selected => 'selected'}, | |
|
175 | :content => tester.name | |
|
176 | } | |
|
177 | ||
|
178 | # Update issue | |
|
179 | new_tester = users[1] | |
|
180 | assert_difference 'Journal.count' do | |
|
181 | put "/issues/#{issue.id}", | |
|
182 | :notes => 'Updating custom field', | |
|
183 | :issue => { | |
|
184 | :custom_field_values => {@field.id.to_s => new_tester.id.to_s} | |
|
185 | } | |
|
186 | end | |
|
187 | assert_response 302 | |
|
188 | ||
|
189 | # Issue view | |
|
190 | follow_redirect! | |
|
191 | assert_tag :content => 'Tester', | |
|
192 | :ancestor => {:tag => 'ul', :attributes => {:class => /details/}}, | |
|
193 | :sibling => { | |
|
194 | :content => tester.name, | |
|
195 | :sibling => { | |
|
196 | :content => new_tester.name | |
|
197 | } | |
|
198 | } | |
|
199 | end | |
|
129 | 200 | end |
@@ -1,5 +1,5 | |||
|
1 |
# |
|
|
2 |
# Copyright (C) 2006-20 |
|
|
1 | # Redmine - project management software | |
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
@@ -75,7 +75,7 module Redmine | |||
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def custom_field_values |
|
78 | @custom_field_values ||= available_custom_fields.collect { |x| custom_values.detect { |v| v.custom_field == x } || custom_values.build(:custom_field => x, :value => nil) } | |
|
78 | @custom_field_values ||= available_custom_fields.collect { |x| custom_values.detect { |v| v.custom_field == x } || custom_values.build(:customized => self, :custom_field => x, :value => nil) } | |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def visible_custom_field_values |
General Comments 0
You need to be logged in to leave comments.
Login now