##// END OF EJS Templates
Fixes boolean custom fields tags (broken by r1592) (#1640)....
Jean-Philippe Lang -
r1653:025581bb284a
parent child
Show More
@@ -1,87 +1,87
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module CustomFieldsHelper
19 19
20 20 def custom_fields_tabs
21 21 tabs = [{:name => 'IssueCustomField', :label => :label_issue_plural},
22 22 {:name => 'ProjectCustomField', :label => :label_project_plural},
23 23 {:name => 'UserCustomField', :label => :label_user_plural}
24 24 ]
25 25 end
26 26
27 27 # Return custom field html tag corresponding to its format
28 28 def custom_field_tag(name, custom_value)
29 29 custom_field = custom_value.custom_field
30 30 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
31 31 field_id = "#{name}_custom_field_values_#{custom_field.id}"
32 32
33 33 case custom_field.field_format
34 34 when "date"
35 35 text_field_tag(field_name, custom_value.value, :id => field_id, :size => 10) +
36 36 calendar_for(field_id)
37 37 when "text"
38 38 text_area_tag(field_name, custom_value.value, :id => field_id, :rows => 3, :style => 'width:90%')
39 39 when "bool"
40 check_box_tag(field_name, custom_value.value, :id => field_id)
40 check_box_tag(field_name, '1', custom_value.true?, :id => field_id) + hidden_field_tag(field_name, '0')
41 41 when "list"
42 42 blank_option = custom_field.is_required? ?
43 43 (custom_field.default_value.blank? ? "<option value=\"\">--- #{l(:actionview_instancetag_blank_option)} ---</option>" : '') :
44 44 '<option></option>'
45 45 select_tag(field_name, blank_option + options_for_select(custom_field.possible_values, custom_value.value), :id => field_id)
46 46 else
47 47 text_field_tag(field_name, custom_value.value, :id => field_id)
48 48 end
49 49 end
50 50
51 51 # Return custom field label tag
52 52 def custom_field_label_tag(name, custom_value)
53 53 content_tag "label", custom_value.custom_field.name +
54 54 (custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>" : ""),
55 55 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}",
56 56 :class => (custom_value.errors.empty? ? nil : "error" )
57 57 end
58 58
59 59 # Return custom field tag with its label tag
60 60 def custom_field_tag_with_label(name, custom_value)
61 61 custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value)
62 62 end
63 63
64 64 # Return a string used to display a custom value
65 65 def show_value(custom_value)
66 66 return "" unless custom_value
67 67 format_value(custom_value.value, custom_value.custom_field.field_format)
68 68 end
69 69
70 70 # Return a string used to display a custom value
71 71 def format_value(value, field_format)
72 72 return "" unless value && !value.empty?
73 73 case field_format
74 74 when "date"
75 75 begin; format_date(value.to_date); rescue; value end
76 76 when "bool"
77 77 l_YesNo(value == "1")
78 78 else
79 79 value
80 80 end
81 81 end
82 82
83 83 # Return an array of custom field formats which can be used in select_tag
84 84 def custom_field_formats_for_select
85 85 CustomField::FIELD_FORMATS.sort {|a,b| a[1][:order]<=>b[1][:order]}.collect { |k| [ l(k[1][:name]), k[0] ] }
86 86 end
87 87 end
@@ -1,50 +1,55
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class CustomValue < ActiveRecord::Base
19 19 belongs_to :custom_field
20 20 belongs_to :customized, :polymorphic => true
21 21
22 22 def after_initialize
23 23 if custom_field && new_record? && (customized_type.blank? || (customized && customized.new_record?))
24 24 self.value ||= custom_field.default_value
25 25 end
26 26 end
27 27
28 # Returns true if the boolean custom value is true
29 def true?
30 self.value == '1'
31 end
32
28 33 protected
29 34 def validate
30 35 if value.blank?
31 36 errors.add(:value, :activerecord_error_blank) if custom_field.is_required? and value.blank?
32 37 else
33 38 errors.add(:value, :activerecord_error_invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
34 39 errors.add(:value, :activerecord_error_too_short) if custom_field.min_length > 0 and value.length < custom_field.min_length
35 40 errors.add(:value, :activerecord_error_too_long) if custom_field.max_length > 0 and value.length > custom_field.max_length
36 41
37 42 # Format specific validations
38 43 case custom_field.field_format
39 44 when 'int'
40 45 errors.add(:value, :activerecord_error_not_a_number) unless value =~ /^[+-]?\d+$/
41 46 when 'float'
42 47 begin; Kernel.Float(value); rescue; errors.add(:value, :activerecord_error_invalid) end
43 48 when 'date'
44 49 errors.add(:value, :activerecord_error_not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/
45 50 when 'list'
46 51 errors.add(:value, :activerecord_error_inclusion) unless custom_field.possible_values.include?(value)
47 52 end
48 53 end
49 54 end
50 55 end
General Comments 0
You need to be logged in to leave comments. Login now