Chris@0
|
1 # Redmine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 module Redmine
|
Chris@0
|
19 class CustomFieldFormat
|
Chris@0
|
20 include Redmine::I18n
|
Chris@0
|
21
|
Chris@0
|
22 cattr_accessor :available
|
Chris@0
|
23 @@available = {}
|
Chris@0
|
24
|
Chris@0
|
25 attr_accessor :name, :order, :label
|
Chris@0
|
26
|
Chris@0
|
27 def initialize(name, options={})
|
Chris@0
|
28 self.name = name
|
Chris@0
|
29 self.label = options[:label]
|
Chris@0
|
30 self.order = options[:order]
|
Chris@0
|
31 end
|
Chris@0
|
32
|
Chris@0
|
33 def format(value)
|
Chris@0
|
34 send "format_as_#{name}", value
|
Chris@0
|
35 end
|
Chris@0
|
36
|
Chris@0
|
37 def format_as_date(value)
|
Chris@0
|
38 begin; format_date(value.to_date); rescue; value end
|
Chris@0
|
39 end
|
Chris@0
|
40
|
Chris@0
|
41 def format_as_bool(value)
|
Chris@0
|
42 l(value == "1" ? :general_text_Yes : :general_text_No)
|
Chris@0
|
43 end
|
Chris@0
|
44
|
Chris@0
|
45 ['string','text','int','float','list'].each do |name|
|
Chris@0
|
46 define_method("format_as_#{name}") {|value|
|
Chris@0
|
47 return value
|
Chris@0
|
48 }
|
Chris@0
|
49 end
|
Chris@0
|
50
|
Chris@0
|
51 # Allow displaying the edit type of another field_format
|
Chris@0
|
52 #
|
Chris@0
|
53 # Example: display a custom field as a list
|
Chris@0
|
54 def edit_as
|
Chris@0
|
55 name
|
Chris@0
|
56 end
|
Chris@0
|
57
|
Chris@0
|
58 class << self
|
Chris@0
|
59 def map(&block)
|
Chris@0
|
60 yield self
|
Chris@0
|
61 end
|
Chris@0
|
62
|
Chris@0
|
63 # Registers a custom field format
|
Chris@0
|
64 def register(custom_field_format, options={})
|
Chris@0
|
65 @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
|
Chris@0
|
66 end
|
Chris@0
|
67
|
Chris@0
|
68 def available_formats
|
Chris@0
|
69 @@available.keys
|
Chris@0
|
70 end
|
Chris@0
|
71
|
Chris@0
|
72 def find_by_name(name)
|
Chris@0
|
73 @@available[name.to_s]
|
Chris@0
|
74 end
|
Chris@0
|
75
|
Chris@0
|
76 def label_for(name)
|
Chris@0
|
77 format = @@available[name.to_s]
|
Chris@0
|
78 format.label if format
|
Chris@0
|
79 end
|
Chris@0
|
80
|
Chris@0
|
81 # Return an array of custom field formats which can be used in select_tag
|
Chris@0
|
82 def as_select
|
Chris@0
|
83 @@available.values.sort {|a,b|
|
Chris@0
|
84 a.order <=> b.order
|
Chris@0
|
85 }.collect {|custom_field_format|
|
Chris@0
|
86 [ l(custom_field_format.label), custom_field_format.name ]
|
Chris@0
|
87 }
|
Chris@0
|
88 end
|
Chris@0
|
89
|
Chris@0
|
90 def format_value(value, field_format)
|
Chris@0
|
91 return "" unless value && !value.empty?
|
Chris@0
|
92
|
Chris@0
|
93 if format_type = find_by_name(field_format)
|
Chris@0
|
94 format_type.format(value)
|
Chris@0
|
95 else
|
Chris@0
|
96 value
|
Chris@0
|
97 end
|
Chris@0
|
98 end
|
Chris@0
|
99 end
|
Chris@0
|
100 end
|
Chris@0
|
101 end
|