annotate lib/redmine/custom_field_format.rb @ 1295:622f24f53b42 redmine-2.3

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