Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: module Redmine Chris@909: class CustomFieldFormat Chris@909: include Redmine::I18n Chris@909: Chris@909: cattr_accessor :available Chris@909: @@available = {} Chris@909: Chris@909: attr_accessor :name, :order, :label, :edit_as, :class_names Chris@909: Chris@909: def initialize(name, options={}) Chris@909: self.name = name Chris@909: self.label = options[:label] Chris@909: self.order = options[:order] Chris@909: self.edit_as = options[:edit_as] || name Chris@909: self.class_names = options[:only] Chris@909: end Chris@909: Chris@909: def format(value) Chris@909: send "format_as_#{name}", value Chris@909: end Chris@909: Chris@909: def format_as_date(value) Chris@909: begin; format_date(value.to_date); rescue; value end Chris@909: end Chris@909: Chris@909: def format_as_bool(value) Chris@909: l(value == "1" ? :general_text_Yes : :general_text_No) Chris@909: end Chris@909: Chris@909: ['string','text','int','float','list'].each do |name| Chris@909: define_method("format_as_#{name}") {|value| Chris@909: return value Chris@909: } Chris@909: end Chris@909: Chris@909: ['user', 'version'].each do |name| Chris@909: define_method("format_as_#{name}") {|value| Chris@909: return value.blank? ? "" : name.classify.constantize.find_by_id(value.to_i).to_s Chris@909: } Chris@909: end Chris@909: Chris@909: class << self Chris@909: def map(&block) Chris@909: yield self Chris@909: end Chris@909: Chris@909: # Registers a custom field format Chris@909: def register(custom_field_format, options={}) Chris@909: @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name) Chris@909: end Chris@909: Chris@909: def available_formats Chris@909: @@available.keys Chris@909: end Chris@909: Chris@909: def find_by_name(name) Chris@909: @@available[name.to_s] Chris@909: end Chris@909: Chris@909: def label_for(name) Chris@909: format = @@available[name.to_s] Chris@909: format.label if format Chris@909: end Chris@909: Chris@909: # Return an array of custom field formats which can be used in select_tag Chris@909: def as_select(class_name=nil) Chris@909: fields = @@available.values Chris@909: fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)} Chris@909: fields.sort {|a,b| Chris@909: a.order <=> b.order Chris@909: }.collect {|custom_field_format| Chris@909: [ l(custom_field_format.label), custom_field_format.name ] Chris@909: } Chris@909: end Chris@909: Chris@909: def format_value(value, field_format) Chris@909: return "" unless value && !value.empty? Chris@909: Chris@909: if format_type = find_by_name(field_format) Chris@909: format_type.format(value) Chris@909: else Chris@909: value Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end