##// END OF EJS Templates
Change the case statement into a method call based on the name...
Eric Davis -
r3560:4cb943571abc
parent child
Show More
@@ -1,78 +1,94
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
2 # Copyright (C) 2006-2009 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 Redmine
18 module Redmine
19 class CustomFieldFormat
19 class CustomFieldFormat
20 include Redmine::I18n
20 include Redmine::I18n
21
21
22 cattr_accessor :available
22 cattr_accessor :available
23 @@available = {}
23 @@available = {}
24
24
25 attr_accessor :name, :order, :label
25 attr_accessor :name, :order, :label
26
26
27 def initialize(name, options={})
27 def initialize(name, options={})
28 self.name = name
28 self.name = name
29 self.label = options[:label]
29 self.label = options[:label]
30 self.order = options[:order]
30 self.order = options[:order]
31 end
31 end
32
32
33 def format(value)
34 send "format_as_#{name}", value
35 end
36
37 def format_as_date(value)
38 begin; format_date(value.to_date); rescue; value end
39 end
40
41 def format_as_bool(value)
42 l(value == "1" ? :general_text_Yes : :general_text_No)
43 end
44
45 ['string','text','int','float','list'].each do |name|
46 define_method("format_as_#{name}") {|value|
47 return value
48 }
49 end
50
33 class << self
51 class << self
34 def map(&block)
52 def map(&block)
35 yield self
53 yield self
36 end
54 end
37
55
38 # Registers a custom field format
56 # Registers a custom field format
39 def register(custom_field_format, options={})
57 def register(custom_field_format, options={})
40 @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
58 @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
41 end
59 end
42
60
43 def available_formats
61 def available_formats
44 @@available.keys
62 @@available.keys
45 end
63 end
46
64
47 def find_by_name(name)
65 def find_by_name(name)
48 @@available[name.to_s]
66 @@available[name.to_s]
49 end
67 end
50
68
51 def label_for(name)
69 def label_for(name)
52 format = @@available[name.to_s]
70 format = @@available[name.to_s]
53 format.label if format
71 format.label if format
54 end
72 end
55
73
56 # Return an array of custom field formats which can be used in select_tag
74 # Return an array of custom field formats which can be used in select_tag
57 def as_select
75 def as_select
58 @@available.values.sort {|a,b|
76 @@available.values.sort {|a,b|
59 a.order <=> b.order
77 a.order <=> b.order
60 }.collect {|custom_field_format|
78 }.collect {|custom_field_format|
61 [ l(custom_field_format.label), custom_field_format.name ]
79 [ l(custom_field_format.label), custom_field_format.name ]
62 }
80 }
63 end
81 end
64
82
65 def format_value(value, field_format)
83 def format_value(value, field_format)
66 return "" unless value && !value.empty?
84 return "" unless value && !value.empty?
67 case field_format
85
68 when "date"
86 if format_type = find_by_name(field_format)
69 begin; format_date(value.to_date); rescue; value end
87 format_type.format(value)
70 when "bool"
71 l(value == "1" ? :general_text_Yes : :general_text_No)
72 else
88 else
73 value
89 value
74 end
90 end
75 end
91 end
76 end
92 end
77 end
93 end
78 end
94 end
General Comments 0
You need to be logged in to leave comments. Login now