##// END OF EJS Templates
Fixes custom_field_tag helper broken by r9682....
Jean-Philippe Lang -
r9503:4f0aa2432c2c
parent child
Show More
@@ -1,156 +1,156
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2012 Jean-Philippe Lang
4 # Copyright (C) 2006-2012 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 module CustomFieldsHelper
20 module CustomFieldsHelper
21
21
22 def custom_fields_tabs
22 def custom_fields_tabs
23 tabs = [{:name => 'IssueCustomField', :partial => 'custom_fields/index', :label => :label_issue_plural},
23 tabs = [{:name => 'IssueCustomField', :partial => 'custom_fields/index', :label => :label_issue_plural},
24 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index', :label => :label_spent_time},
24 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index', :label => :label_spent_time},
25 {:name => 'ProjectCustomField', :partial => 'custom_fields/index', :label => :label_project_plural},
25 {:name => 'ProjectCustomField', :partial => 'custom_fields/index', :label => :label_project_plural},
26 {:name => 'VersionCustomField', :partial => 'custom_fields/index', :label => :label_version_plural},
26 {:name => 'VersionCustomField', :partial => 'custom_fields/index', :label => :label_version_plural},
27 {:name => 'UserCustomField', :partial => 'custom_fields/index', :label => :label_user_plural},
27 {:name => 'UserCustomField', :partial => 'custom_fields/index', :label => :label_user_plural},
28 {:name => 'GroupCustomField', :partial => 'custom_fields/index', :label => :label_group_plural},
28 {:name => 'GroupCustomField', :partial => 'custom_fields/index', :label => :label_group_plural},
29 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index', :label => TimeEntryActivity::OptionName},
29 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index', :label => TimeEntryActivity::OptionName},
30 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index', :label => IssuePriority::OptionName},
30 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index', :label => IssuePriority::OptionName},
31 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index', :label => DocumentCategory::OptionName}
31 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index', :label => DocumentCategory::OptionName}
32 ]
32 ]
33 end
33 end
34
34
35 # Return custom field html tag corresponding to its format
35 # Return custom field html tag corresponding to its format
36 def custom_field_tag(name, custom_value)
36 def custom_field_tag(name, custom_value)
37 custom_field = custom_value.custom_field
37 custom_field = custom_value.custom_field
38 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
38 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
39 field_name << "[]" if custom_field.multiple?
39 field_name << "[]" if custom_field.multiple?
40 field_id = "#{name}_custom_field_values_#{custom_field.id}"
40 field_id = "#{name}_custom_field_values_#{custom_field.id}"
41
41
42 tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
42 tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
43
43
44 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
44 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
45 case field_format.try(:edit_as)
45 case field_format.try(:edit_as)
46 when "date"
46 when "date"
47 text_field_tag(field_name, custom_value.value, tag_options.merge(:size => 10)) +
47 text_field_tag(field_name, custom_value.value, tag_options.merge(:size => 10)) +
48 calendar_for(field_id)
48 calendar_for(field_id)
49 when "text"
49 when "text"
50 text_area_tag(field_name, custom_value.value, tag_options.merge(:rows => 3))
50 text_area_tag(field_name, custom_value.value, tag_options.merge(:rows => 3))
51 when "bool"
51 when "bool"
52 hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, tag_options)
52 hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, tag_options)
53 when "list"
53 when "list"
54 blank_option = ''
54 blank_option = ''.html_safe
55 unless custom_field.multiple?
55 unless custom_field.multiple?
56 if custom_field.is_required?
56 if custom_field.is_required?
57 unless custom_field.default_value.present?
57 unless custom_field.default_value.present?
58 blank_option = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
58 blank_option = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
59 end
59 end
60 else
60 else
61 blank_option = content_tag('option')
61 blank_option = content_tag('option')
62 end
62 end
63 end
63 end
64 s = select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value),
64 s = select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value),
65 tag_options.merge(:multiple => custom_field.multiple?))
65 tag_options.merge(:multiple => custom_field.multiple?))
66 if custom_field.multiple?
66 if custom_field.multiple?
67 s << hidden_field_tag(field_name, '')
67 s << hidden_field_tag(field_name, '')
68 end
68 end
69 s
69 s
70 else
70 else
71 text_field_tag(field_name, custom_value.value, tag_options)
71 text_field_tag(field_name, custom_value.value, tag_options)
72 end
72 end
73 end
73 end
74
74
75 # Return custom field label tag
75 # Return custom field label tag
76 def custom_field_label_tag(name, custom_value)
76 def custom_field_label_tag(name, custom_value)
77 content_tag "label", h(custom_value.custom_field.name) +
77 content_tag "label", h(custom_value.custom_field.name) +
78 (custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>".html_safe : ""),
78 (custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>".html_safe : ""),
79 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}"
79 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}"
80 end
80 end
81
81
82 # Return custom field tag with its label tag
82 # Return custom field tag with its label tag
83 def custom_field_tag_with_label(name, custom_value)
83 def custom_field_tag_with_label(name, custom_value)
84 custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value)
84 custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value)
85 end
85 end
86
86
87 def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil)
87 def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil)
88 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
88 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
89 field_name << "[]" if custom_field.multiple?
89 field_name << "[]" if custom_field.multiple?
90 field_id = "#{name}_custom_field_values_#{custom_field.id}"
90 field_id = "#{name}_custom_field_values_#{custom_field.id}"
91
91
92 tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
92 tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
93
93
94 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
94 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
95 case field_format.try(:edit_as)
95 case field_format.try(:edit_as)
96 when "date"
96 when "date"
97 text_field_tag(field_name, '', tag_options.merge(:size => 10)) +
97 text_field_tag(field_name, '', tag_options.merge(:size => 10)) +
98 calendar_for(field_id)
98 calendar_for(field_id)
99 when "text"
99 when "text"
100 text_area_tag(field_name, '', tag_options.merge(:rows => 3))
100 text_area_tag(field_name, '', tag_options.merge(:rows => 3))
101 when "bool"
101 when "bool"
102 select_tag(field_name, options_for_select([[l(:label_no_change_option), ''],
102 select_tag(field_name, options_for_select([[l(:label_no_change_option), ''],
103 [l(:general_text_yes), '1'],
103 [l(:general_text_yes), '1'],
104 [l(:general_text_no), '0']]), tag_options)
104 [l(:general_text_no), '0']]), tag_options)
105 when "list"
105 when "list"
106 options = []
106 options = []
107 options << [l(:label_no_change_option), ''] unless custom_field.multiple?
107 options << [l(:label_no_change_option), ''] unless custom_field.multiple?
108 options << [l(:label_none), '__none__'] unless custom_field.is_required?
108 options << [l(:label_none), '__none__'] unless custom_field.is_required?
109 options += custom_field.possible_values_options(projects)
109 options += custom_field.possible_values_options(projects)
110 select_tag(field_name, options_for_select(options), tag_options.merge(:multiple => custom_field.multiple?))
110 select_tag(field_name, options_for_select(options), tag_options.merge(:multiple => custom_field.multiple?))
111 else
111 else
112 text_field_tag(field_name, '', tag_options)
112 text_field_tag(field_name, '', tag_options)
113 end
113 end
114 end
114 end
115
115
116 # Return a string used to display a custom value
116 # Return a string used to display a custom value
117 def show_value(custom_value)
117 def show_value(custom_value)
118 return "" unless custom_value
118 return "" unless custom_value
119 format_value(custom_value.value, custom_value.custom_field.field_format)
119 format_value(custom_value.value, custom_value.custom_field.field_format)
120 end
120 end
121
121
122 # Return a string used to display a custom value
122 # Return a string used to display a custom value
123 def format_value(value, field_format)
123 def format_value(value, field_format)
124 if value.is_a?(Array)
124 if value.is_a?(Array)
125 value.collect {|v| format_value(v, field_format)}.compact.sort.join(', ')
125 value.collect {|v| format_value(v, field_format)}.compact.sort.join(', ')
126 else
126 else
127 Redmine::CustomFieldFormat.format_value(value, field_format)
127 Redmine::CustomFieldFormat.format_value(value, field_format)
128 end
128 end
129 end
129 end
130
130
131 # Return an array of custom field formats which can be used in select_tag
131 # Return an array of custom field formats which can be used in select_tag
132 def custom_field_formats_for_select(custom_field)
132 def custom_field_formats_for_select(custom_field)
133 Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name)
133 Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name)
134 end
134 end
135
135
136 # Renders the custom_values in api views
136 # Renders the custom_values in api views
137 def render_api_custom_values(custom_values, api)
137 def render_api_custom_values(custom_values, api)
138 api.array :custom_fields do
138 api.array :custom_fields do
139 custom_values.each do |custom_value|
139 custom_values.each do |custom_value|
140 attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
140 attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
141 attrs.merge!(:multiple => true) if custom_value.custom_field.multiple?
141 attrs.merge!(:multiple => true) if custom_value.custom_field.multiple?
142 api.custom_field attrs do
142 api.custom_field attrs do
143 if custom_value.value.is_a?(Array)
143 if custom_value.value.is_a?(Array)
144 api.array :value do
144 api.array :value do
145 custom_value.value.each do |value|
145 custom_value.value.each do |value|
146 api.value value unless value.blank?
146 api.value value unless value.blank?
147 end
147 end
148 end
148 end
149 else
149 else
150 api.value custom_value.value
150 api.value custom_value.value
151 end
151 end
152 end
152 end
153 end
153 end
154 end unless custom_values.empty?
154 end unless custom_values.empty?
155 end
155 end
156 end
156 end
General Comments 0
You need to be logged in to leave comments. Login now