comparison lib/redmine/.svn/text-base/custom_field_format.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children cbce1fd3b1b7
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 module Redmine
19 class CustomFieldFormat
20 include Redmine::I18n
21
22 cattr_accessor :available
23 @@available = {}
24
25 attr_accessor :name, :order, :label
26
27 def initialize(name, options={})
28 self.name = name
29 self.label = options[:label]
30 self.order = options[:order]
31 end
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
51 # Allow displaying the edit type of another field_format
52 #
53 # Example: display a custom field as a list
54 def edit_as
55 name
56 end
57
58 class << self
59 def map(&block)
60 yield self
61 end
62
63 # Registers a custom field format
64 def register(custom_field_format, options={})
65 @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
66 end
67
68 def available_formats
69 @@available.keys
70 end
71
72 def find_by_name(name)
73 @@available[name.to_s]
74 end
75
76 def label_for(name)
77 format = @@available[name.to_s]
78 format.label if format
79 end
80
81 # Return an array of custom field formats which can be used in select_tag
82 def as_select
83 @@available.values.sort {|a,b|
84 a.order <=> b.order
85 }.collect {|custom_field_format|
86 [ l(custom_field_format.label), custom_field_format.name ]
87 }
88 end
89
90 def format_value(value, field_format)
91 return "" unless value && !value.empty?
92
93 if format_type = find_by_name(field_format)
94 format_type.format(value)
95 else
96 value
97 end
98 end
99 end
100 end
101 end