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