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