annotate lib/redmine/custom_field_format.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

Close obsolete branch bibplugin_db_migrations
author Chris Cannam
date Fri, 30 Nov 2012 14:40:50 +0000
parents cbb26bc654de
children 433d4f72a19b
rev   line source
Chris@0 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 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@0 29 self.label = options[:label]
Chris@0 30 self.order = options[:order]
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@0 65 def register(custom_field_format, options={})
Chris@0 66 @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
Chris@0 67 end
Chris@0 68
Chris@0 69 def available_formats
Chris@0 70 @@available.keys
Chris@0 71 end
Chris@0 72
Chris@0 73 def find_by_name(name)
Chris@0 74 @@available[name.to_s]
Chris@0 75 end
Chris@0 76
Chris@0 77 def label_for(name)
Chris@0 78 format = @@available[name.to_s]
Chris@0 79 format.label if format
Chris@0 80 end
Chris@0 81
Chris@0 82 # Return an array of custom field formats which can be used in select_tag
Chris@441 83 def as_select(class_name=nil)
Chris@441 84 fields = @@available.values
Chris@441 85 fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)}
Chris@441 86 fields.sort {|a,b|
Chris@0 87 a.order <=> b.order
Chris@0 88 }.collect {|custom_field_format|
Chris@0 89 [ l(custom_field_format.label), custom_field_format.name ]
Chris@0 90 }
Chris@0 91 end
Chris@0 92
Chris@0 93 def format_value(value, field_format)
Chris@0 94 return "" unless value && !value.empty?
Chris@0 95
Chris@0 96 if format_type = find_by_name(field_format)
Chris@0 97 format_type.format(value)
Chris@0 98 else
Chris@0 99 value
Chris@0 100 end
Chris@0 101 end
Chris@0 102 end
Chris@909 103 end
Chris@0 104 end