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