Mercurial > hg > soundsoftware-site
comparison app/helpers/custom_fields_helper.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | 622f24f53b42 261b3d9a4903 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # encoding: utf-8 | 1 # encoding: utf-8 |
2 # | 2 # |
3 # Redmine - project management software | 3 # Redmine - project management software |
4 # Copyright (C) 2006-2011 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. |
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 CustomField::CUSTOM_FIELDS_TABS |
24 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index', :label => :label_spent_time}, | |
25 {:name => 'ProjectCustomField', :partial => 'custom_fields/index', :label => :label_project_plural}, | |
26 {:name => 'VersionCustomField', :partial => 'custom_fields/index', :label => :label_version_plural}, | |
27 {:name => 'UserCustomField', :partial => 'custom_fields/index', :label => :label_user_plural}, | |
28 {:name => 'GroupCustomField', :partial => 'custom_fields/index', :label => :label_group_plural}, | |
29 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index', :label => TimeEntryActivity::OptionName}, | |
30 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index', :label => IssuePriority::OptionName}, | |
31 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index', :label => DocumentCategory::OptionName} | |
32 ] | |
33 end | 24 end |
34 | 25 |
35 # Return custom field html tag corresponding to its format | 26 # Return custom field html tag corresponding to its format |
36 def custom_field_tag(name, custom_value) | 27 def custom_field_tag(name, custom_value) |
37 custom_field = custom_value.custom_field | 28 custom_field = custom_value.custom_field |
38 field_name = "#{name}[custom_field_values][#{custom_field.id}]" | 29 field_name = "#{name}[custom_field_values][#{custom_field.id}]" |
30 field_name << "[]" if custom_field.multiple? | |
39 field_id = "#{name}_custom_field_values_#{custom_field.id}" | 31 field_id = "#{name}_custom_field_values_#{custom_field.id}" |
32 | |
33 tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"} | |
40 | 34 |
41 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format) | 35 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format) |
42 case field_format.try(:edit_as) | 36 case field_format.try(:edit_as) |
43 when "date" | 37 when "date" |
44 text_field_tag(field_name, custom_value.value, :id => field_id, :size => 10) + | 38 text_field_tag(field_name, custom_value.value, tag_options.merge(:size => 10)) + |
45 calendar_for(field_id) | 39 calendar_for(field_id) |
46 when "text" | 40 when "text" |
47 text_area_tag(field_name, custom_value.value, :id => field_id, :rows => 3, :style => 'width:90%') | 41 text_area_tag(field_name, custom_value.value, tag_options.merge(:rows => 3)) |
48 when "bool" | 42 when "bool" |
49 hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, :id => field_id) | 43 hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, tag_options) |
50 when "list" | 44 when "list" |
51 blank_option = custom_field.is_required? ? | 45 blank_option = ''.html_safe |
52 (custom_field.default_value.blank? ? "<option value=\"\">--- #{l(:actionview_instancetag_blank_option)} ---</option>" : '') : | 46 unless custom_field.multiple? |
53 '<option></option>' | 47 if custom_field.is_required? |
54 select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value), :id => field_id) | 48 unless custom_field.default_value.present? |
49 blank_option = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '') | |
50 end | |
51 else | |
52 blank_option = content_tag('option') | |
53 end | |
54 end | |
55 s = select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value), | |
56 tag_options.merge(:multiple => custom_field.multiple?)) | |
57 if custom_field.multiple? | |
58 s << hidden_field_tag(field_name, '') | |
59 end | |
60 s | |
55 else | 61 else |
56 text_field_tag(field_name, custom_value.value, :id => field_id) | 62 text_field_tag(field_name, custom_value.value, tag_options) |
57 end | 63 end |
58 end | 64 end |
59 | 65 |
60 # Return custom field label tag | 66 # Return custom field label tag |
61 def custom_field_label_tag(name, custom_value) | 67 def custom_field_label_tag(name, custom_value, options={}) |
68 required = options[:required] || custom_value.custom_field.is_required? | |
69 | |
62 content_tag "label", h(custom_value.custom_field.name) + | 70 content_tag "label", h(custom_value.custom_field.name) + |
63 (custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>".html_safe : ""), | 71 (required ? " <span class=\"required\">*</span>".html_safe : ""), |
64 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}", | 72 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}" |
65 :class => (custom_value.errors.empty? ? nil : "error" ) | |
66 end | 73 end |
67 | 74 |
68 # Return custom field tag with its label tag | 75 # Return custom field tag with its label tag |
69 def custom_field_tag_with_label(name, custom_value) | 76 def custom_field_tag_with_label(name, custom_value, options={}) |
70 custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value) | 77 custom_field_label_tag(name, custom_value, options) + custom_field_tag(name, custom_value) |
71 end | 78 end |
72 | 79 |
73 def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil) | 80 def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil) |
74 field_name = "#{name}[custom_field_values][#{custom_field.id}]" | 81 field_name = "#{name}[custom_field_values][#{custom_field.id}]" |
82 field_name << "[]" if custom_field.multiple? | |
75 field_id = "#{name}_custom_field_values_#{custom_field.id}" | 83 field_id = "#{name}_custom_field_values_#{custom_field.id}" |
84 | |
85 tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"} | |
86 | |
76 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format) | 87 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format) |
77 case field_format.try(:edit_as) | 88 case field_format.try(:edit_as) |
78 when "date" | 89 when "date" |
79 text_field_tag(field_name, '', :id => field_id, :size => 10) + | 90 text_field_tag(field_name, '', tag_options.merge(:size => 10)) + |
80 calendar_for(field_id) | 91 calendar_for(field_id) |
81 when "text" | 92 when "text" |
82 text_area_tag(field_name, '', :id => field_id, :rows => 3, :style => 'width:90%') | 93 text_area_tag(field_name, '', tag_options.merge(:rows => 3)) |
83 when "bool" | 94 when "bool" |
84 select_tag(field_name, options_for_select([[l(:label_no_change_option), ''], | 95 select_tag(field_name, options_for_select([[l(:label_no_change_option), ''], |
85 [l(:general_text_yes), '1'], | 96 [l(:general_text_yes), '1'], |
86 [l(:general_text_no), '0']]), :id => field_id) | 97 [l(:general_text_no), '0']]), tag_options) |
87 when "list" | 98 when "list" |
88 select_tag(field_name, options_for_select([[l(:label_no_change_option), '']] + custom_field.possible_values_options(projects)), :id => field_id) | 99 options = [] |
100 options << [l(:label_no_change_option), ''] unless custom_field.multiple? | |
101 options << [l(:label_none), '__none__'] unless custom_field.is_required? | |
102 options += custom_field.possible_values_options(projects) | |
103 select_tag(field_name, options_for_select(options), tag_options.merge(:multiple => custom_field.multiple?)) | |
89 else | 104 else |
90 text_field_tag(field_name, '', :id => field_id) | 105 text_field_tag(field_name, '', tag_options) |
91 end | 106 end |
92 end | 107 end |
93 | 108 |
94 # Return a string used to display a custom value | 109 # Return a string used to display a custom value |
95 def show_value(custom_value) | 110 def show_value(custom_value) |
97 format_value(custom_value.value, custom_value.custom_field.field_format) | 112 format_value(custom_value.value, custom_value.custom_field.field_format) |
98 end | 113 end |
99 | 114 |
100 # Return a string used to display a custom value | 115 # Return a string used to display a custom value |
101 def format_value(value, field_format) | 116 def format_value(value, field_format) |
102 Redmine::CustomFieldFormat.format_value(value, field_format) # Proxy | 117 if value.is_a?(Array) |
118 value.collect {|v| format_value(v, field_format)}.compact.sort.join(', ') | |
119 else | |
120 Redmine::CustomFieldFormat.format_value(value, field_format) | |
121 end | |
103 end | 122 end |
104 | 123 |
105 # Return an array of custom field formats which can be used in select_tag | 124 # Return an array of custom field formats which can be used in select_tag |
106 def custom_field_formats_for_select(custom_field) | 125 def custom_field_formats_for_select(custom_field) |
107 Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name) | 126 Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name) |
109 | 128 |
110 # Renders the custom_values in api views | 129 # Renders the custom_values in api views |
111 def render_api_custom_values(custom_values, api) | 130 def render_api_custom_values(custom_values, api) |
112 api.array :custom_fields do | 131 api.array :custom_fields do |
113 custom_values.each do |custom_value| | 132 custom_values.each do |custom_value| |
114 api.custom_field :id => custom_value.custom_field_id, :name => custom_value.custom_field.name do | 133 attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name} |
115 api.value custom_value.value | 134 attrs.merge!(:multiple => true) if custom_value.custom_field.multiple? |
135 api.custom_field attrs do | |
136 if custom_value.value.is_a?(Array) | |
137 api.array :value do | |
138 custom_value.value.each do |value| | |
139 api.value value unless value.blank? | |
140 end | |
141 end | |
142 else | |
143 api.value custom_value.value | |
144 end | |
116 end | 145 end |
117 end | 146 end |
118 end unless custom_values.empty? | 147 end unless custom_values.empty? |
119 end | 148 end |
120 end | 149 end |