Mercurial > hg > soundsoftware-site
comparison .svn/pristine/f2/f21e899ada67cb1a2a7766f40a5bf4e791cace76.svn-base @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
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 available_formats | |
74 @@available.keys | |
75 end | |
76 | |
77 def find_by_name(name) | |
78 @@available[name.to_s] | |
79 end | |
80 | |
81 def label_for(name) | |
82 format = @@available[name.to_s] | |
83 format.label if format | |
84 end | |
85 | |
86 # Return an array of custom field formats which can be used in select_tag | |
87 def as_select(class_name=nil) | |
88 fields = @@available.values | |
89 fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)} | |
90 fields.sort {|a,b| | |
91 a.order <=> b.order | |
92 }.collect {|custom_field_format| | |
93 [ l(custom_field_format.label), custom_field_format.name ] | |
94 } | |
95 end | |
96 | |
97 def format_value(value, field_format) | |
98 return "" unless value && !value.empty? | |
99 | |
100 if format_type = find_by_name(field_format) | |
101 format_type.format(value) | |
102 else | |
103 value | |
104 end | |
105 end | |
106 end | |
107 end | |
108 end |