1 |
|
# Redmine - project management software
|
2 |
|
# Copyright (C) 2006-2013 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, :edit_as, :class_names
|
26 |
|
|
27 |
|
def initialize(name, options={})
|
28 |
|
self.name = name
|
29 |
|
self.label = options[:label] || "label_#{name}".to_sym
|
30 |
|
self.order = options[:order] || self.class.available_formats.size
|
31 |
|
self.edit_as = options[:edit_as] || name
|
32 |
|
self.class_names = options[:only]
|
33 |
|
end
|
34 |
|
|
35 |
|
def format(value)
|
36 |
|
send "format_as_#{name}", value
|
37 |
|
end
|
38 |
|
|
39 |
|
def format_as_date(value)
|
40 |
|
begin; format_date(value.to_date); rescue; value end
|
41 |
|
end
|
42 |
|
|
43 |
|
def format_as_bool(value)
|
44 |
|
l(value == "1" ? :general_text_Yes : :general_text_No)
|
45 |
|
end
|
46 |
|
|
47 |
|
['string','text','int','float','list'].each do |name|
|
48 |
|
define_method("format_as_#{name}") {|value|
|
49 |
|
return value
|
50 |
|
}
|
51 |
|
end
|
52 |
|
|
53 |
|
['user', 'version'].each do |name|
|
54 |
|
define_method("format_as_#{name}") {|value|
|
55 |
|
return value.blank? ? "" : name.classify.constantize.find_by_id(value.to_i).to_s
|
56 |
|
}
|
57 |
|
end
|
58 |
|
|
59 |
|
class << self
|
60 |
|
def map(&block)
|
61 |
|
yield self
|
62 |
|
end
|
63 |
|
|
64 |
|
# Registers a custom field format
|
65 |
|
def register(*args)
|
66 |
|
custom_field_format = args.first
|
67 |
|
unless custom_field_format.is_a?(Redmine::CustomFieldFormat)
|
68 |
|
custom_field_format = Redmine::CustomFieldFormat.new(*args)
|
69 |
|
end
|
70 |
|
@@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
|
71 |
|
end
|
72 |
|
|
73 |
|
def delete(format)
|
74 |
|
if format.is_a?(Redmine::CustomFieldFormat)
|
75 |
|
format = format.name
|
76 |
|
end
|
77 |
|
@@available.delete(format)
|
78 |
|
end
|
79 |
|
|
80 |
|
def available_formats
|
81 |
|
@@available.keys
|
82 |
|
end
|
83 |
|
|
84 |
|
def find_by_name(name)
|
85 |
|
@@available[name.to_s]
|
86 |
|
end
|
87 |
|
|
88 |
|
def label_for(name)
|
89 |
|
format = @@available[name.to_s]
|
90 |
|
format.label if format
|
91 |
|
end
|
92 |
|
|
93 |
|
# Return an array of custom field formats which can be used in select_tag
|
94 |
|
def as_select(class_name=nil)
|
95 |
|
fields = @@available.values
|
96 |
|
fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)}
|
97 |
|
fields.sort {|a,b|
|
98 |
|
a.order <=> b.order
|
99 |
|
}.collect {|custom_field_format|
|
100 |
|
[ l(custom_field_format.label), custom_field_format.name ]
|
101 |
|
}
|
102 |
|
end
|
103 |
|
|
104 |
|
def format_value(value, field_format)
|
105 |
|
return "" unless value && !value.empty?
|
106 |
|
|
107 |
|
if format_type = find_by_name(field_format)
|
108 |
|
format_type.format(value)
|
109 |
|
else
|
110 |
|
value
|
111 |
|
end
|
112 |
|
end
|
113 |
|
end
|
114 |
|
end
|
115 |
|
end
|
116 |
0 |
|
Removed Redmine::CustomFieldFormat.