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