annotate .svn/pristine/f2/f21e899ada67cb1a2a7766f40a5bf4e791cace76.svn-base @ 1464:261b3d9a4903 redmine-2.4

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