annotate .svn/pristine/89/89f5504f7426bd8b866f9fb941e83b8b3b055e7a.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 module Redmine
Chris@909 19 class CustomFieldFormat
Chris@909 20 include Redmine::I18n
Chris@909 21
Chris@909 22 cattr_accessor :available
Chris@909 23 @@available = {}
Chris@909 24
Chris@909 25 attr_accessor :name, :order, :label, :edit_as, :class_names
Chris@909 26
Chris@909 27 def initialize(name, options={})
Chris@909 28 self.name = name
Chris@909 29 self.label = options[:label]
Chris@909 30 self.order = options[:order]
Chris@909 31 self.edit_as = options[:edit_as] || name
Chris@909 32 self.class_names = options[:only]
Chris@909 33 end
Chris@909 34
Chris@909 35 def format(value)
Chris@909 36 send "format_as_#{name}", value
Chris@909 37 end
Chris@909 38
Chris@909 39 def format_as_date(value)
Chris@909 40 begin; format_date(value.to_date); rescue; value end
Chris@909 41 end
Chris@909 42
Chris@909 43 def format_as_bool(value)
Chris@909 44 l(value == "1" ? :general_text_Yes : :general_text_No)
Chris@909 45 end
Chris@909 46
Chris@909 47 ['string','text','int','float','list'].each do |name|
Chris@909 48 define_method("format_as_#{name}") {|value|
Chris@909 49 return value
Chris@909 50 }
Chris@909 51 end
Chris@909 52
Chris@909 53 ['user', 'version'].each do |name|
Chris@909 54 define_method("format_as_#{name}") {|value|
Chris@909 55 return value.blank? ? "" : name.classify.constantize.find_by_id(value.to_i).to_s
Chris@909 56 }
Chris@909 57 end
Chris@909 58
Chris@909 59 class << self
Chris@909 60 def map(&block)
Chris@909 61 yield self
Chris@909 62 end
Chris@909 63
Chris@909 64 # Registers a custom field format
Chris@909 65 def register(custom_field_format, options={})
Chris@909 66 @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
Chris@909 67 end
Chris@909 68
Chris@909 69 def available_formats
Chris@909 70 @@available.keys
Chris@909 71 end
Chris@909 72
Chris@909 73 def find_by_name(name)
Chris@909 74 @@available[name.to_s]
Chris@909 75 end
Chris@909 76
Chris@909 77 def label_for(name)
Chris@909 78 format = @@available[name.to_s]
Chris@909 79 format.label if format
Chris@909 80 end
Chris@909 81
Chris@909 82 # Return an array of custom field formats which can be used in select_tag
Chris@909 83 def as_select(class_name=nil)
Chris@909 84 fields = @@available.values
Chris@909 85 fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)}
Chris@909 86 fields.sort {|a,b|
Chris@909 87 a.order <=> b.order
Chris@909 88 }.collect {|custom_field_format|
Chris@909 89 [ l(custom_field_format.label), custom_field_format.name ]
Chris@909 90 }
Chris@909 91 end
Chris@909 92
Chris@909 93 def format_value(value, field_format)
Chris@909 94 return "" unless value && !value.empty?
Chris@909 95
Chris@909 96 if format_type = find_by_name(field_format)
Chris@909 97 format_type.format(value)
Chris@909 98 else
Chris@909 99 value
Chris@909 100 end
Chris@909 101 end
Chris@909 102 end
Chris@909 103 end
Chris@909 104 end