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