annotate app/helpers/custom_fields_helper.rb @ 1465:ab8bd24eeb65 bug_635

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