annotate .svn/pristine/b7/b7e57087e2b7452a3c04ea867d9d27d95784c19d.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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