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