annotate .svn/pristine/55/554119d7707f8ea022be31bc48239ea95d10b4bf.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 class CustomField < ActiveRecord::Base
Chris@909 19 has_many :custom_values, :dependent => :delete_all
Chris@909 20 acts_as_list :scope => 'type = \'#{self.class}\''
Chris@909 21 serialize :possible_values
Chris@909 22
Chris@909 23 validates_presence_of :name, :field_format
Chris@909 24 validates_uniqueness_of :name, :scope => :type
Chris@909 25 validates_length_of :name, :maximum => 30
Chris@909 26 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
Chris@909 27
Chris@909 28 validate :validate_values
Chris@909 29
Chris@909 30 def initialize(attributes = nil)
Chris@909 31 super
Chris@909 32 self.possible_values ||= []
Chris@909 33 end
Chris@909 34
Chris@909 35 def before_validation
Chris@909 36 # make sure these fields are not searchable
Chris@909 37 self.searchable = false if %w(int float date bool).include?(field_format)
Chris@909 38 true
Chris@909 39 end
Chris@909 40
Chris@909 41 def validate_values
Chris@909 42 if self.field_format == "list"
Chris@909 43 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
Chris@909 44 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
Chris@909 45 end
Chris@909 46
Chris@909 47 if regexp.present?
Chris@909 48 begin
Chris@909 49 Regexp.new(regexp)
Chris@909 50 rescue
Chris@909 51 errors.add(:regexp, :invalid)
Chris@909 52 end
Chris@909 53 end
Chris@909 54
Chris@909 55 # validate default value
Chris@909 56 v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil)
Chris@909 57 v.custom_field.is_required = false
Chris@909 58 errors.add(:default_value, :invalid) unless v.valid?
Chris@909 59 end
Chris@909 60
Chris@909 61 def possible_values_options(obj=nil)
Chris@909 62 case field_format
Chris@909 63 when 'user', 'version'
Chris@909 64 if obj.respond_to?(:project) && obj.project
Chris@909 65 case field_format
Chris@909 66 when 'user'
Chris@909 67 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
Chris@909 68 when 'version'
Chris@909 69 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]}
Chris@909 70 end
Chris@909 71 elsif obj.is_a?(Array)
Chris@909 72 obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v}
Chris@909 73 else
Chris@909 74 []
Chris@909 75 end
Chris@909 76 else
Chris@909 77 read_attribute :possible_values
Chris@909 78 end
Chris@909 79 end
Chris@909 80
Chris@909 81 def possible_values(obj=nil)
Chris@909 82 case field_format
Chris@909 83 when 'user', 'version'
Chris@909 84 possible_values_options(obj).collect(&:last)
Chris@909 85 else
Chris@909 86 read_attribute :possible_values
Chris@909 87 end
Chris@909 88 end
Chris@909 89
Chris@909 90 # Makes possible_values accept a multiline string
Chris@909 91 def possible_values=(arg)
Chris@909 92 if arg.is_a?(Array)
Chris@909 93 write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?})
Chris@909 94 else
Chris@909 95 self.possible_values = arg.to_s.split(/[\n\r]+/)
Chris@909 96 end
Chris@909 97 end
Chris@909 98
Chris@909 99 def cast_value(value)
Chris@909 100 casted = nil
Chris@909 101 unless value.blank?
Chris@909 102 case field_format
Chris@909 103 when 'string', 'text', 'list'
Chris@909 104 casted = value
Chris@909 105 when 'date'
Chris@909 106 casted = begin; value.to_date; rescue; nil end
Chris@909 107 when 'bool'
Chris@909 108 casted = (value == '1' ? true : false)
Chris@909 109 when 'int'
Chris@909 110 casted = value.to_i
Chris@909 111 when 'float'
Chris@909 112 casted = value.to_f
Chris@909 113 when 'user', 'version'
Chris@909 114 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
Chris@909 115 end
Chris@909 116 end
Chris@909 117 casted
Chris@909 118 end
Chris@909 119
Chris@909 120 # Returns a ORDER BY clause that can used to sort customized
Chris@909 121 # objects by their value of the custom field.
Chris@909 122 # Returns false, if the custom field can not be used for sorting.
Chris@909 123 def order_statement
Chris@909 124 case field_format
Chris@909 125 when 'string', 'text', 'list', 'date', 'bool'
Chris@909 126 # COALESCE is here to make sure that blank and NULL values are sorted equally
Chris@909 127 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
Chris@909 128 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
Chris@909 129 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
Chris@909 130 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
Chris@909 131 when 'int', 'float'
Chris@909 132 # Make the database cast values into numeric
Chris@909 133 # Postgresql will raise an error if a value can not be casted!
Chris@909 134 # CustomValue validations should ensure that it doesn't occur
Chris@909 135 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
Chris@909 136 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
Chris@909 137 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
Chris@909 138 " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
Chris@909 139 else
Chris@909 140 nil
Chris@909 141 end
Chris@909 142 end
Chris@909 143
Chris@909 144 def <=>(field)
Chris@909 145 position <=> field.position
Chris@909 146 end
Chris@909 147
Chris@909 148 def self.customized_class
Chris@909 149 self.name =~ /^(.+)CustomField$/
Chris@909 150 begin; $1.constantize; rescue nil; end
Chris@909 151 end
Chris@909 152
Chris@909 153 # to move in project_custom_field
Chris@909 154 def self.for_all
Chris@909 155 find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
Chris@909 156 end
Chris@909 157
Chris@909 158 def type_name
Chris@909 159 nil
Chris@909 160 end
Chris@909 161 end