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