annotate .svn/pristine/c5/c5a5dd0715c90581373d0e0b7b8ea344f1c040db.svn-base @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents 261b3d9a4903
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 delete(format)
Chris@1464 74 if format.is_a?(Redmine::CustomFieldFormat)
Chris@1464 75 format = format.name
Chris@1464 76 end
Chris@1464 77 @@available.delete(format)
Chris@1464 78 end
Chris@1464 79
Chris@1464 80 def available_formats
Chris@1464 81 @@available.keys
Chris@1464 82 end
Chris@1464 83
Chris@1464 84 def find_by_name(name)
Chris@1464 85 @@available[name.to_s]
Chris@1464 86 end
Chris@1464 87
Chris@1464 88 def label_for(name)
Chris@1464 89 format = @@available[name.to_s]
Chris@1464 90 format.label if format
Chris@1464 91 end
Chris@1464 92
Chris@1464 93 # Return an array of custom field formats which can be used in select_tag
Chris@1464 94 def as_select(class_name=nil)
Chris@1464 95 fields = @@available.values
Chris@1464 96 fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)}
Chris@1464 97 fields.sort {|a,b|
Chris@1464 98 a.order <=> b.order
Chris@1464 99 }.collect {|custom_field_format|
Chris@1464 100 [ l(custom_field_format.label), custom_field_format.name ]
Chris@1464 101 }
Chris@1464 102 end
Chris@1464 103
Chris@1464 104 def format_value(value, field_format)
Chris@1464 105 return "" unless value && !value.empty?
Chris@1464 106
Chris@1464 107 if format_type = find_by_name(field_format)
Chris@1464 108 format_type.format(value)
Chris@1464 109 else
Chris@1464 110 value
Chris@1464 111 end
Chris@1464 112 end
Chris@1464 113 end
Chris@1464 114 end
Chris@1464 115 end