annotate .svn/pristine/14/14309d64e537e5e954feb549559e6200d128036a.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 class CustomField < ActiveRecord::Base
Chris@1296 19 include Redmine::SubclassFactory
Chris@1296 20
Chris@1296 21 has_many :custom_values, :dependent => :delete_all
Chris@1296 22 acts_as_list :scope => 'type = \'#{self.class}\''
Chris@1296 23 serialize :possible_values
Chris@1296 24
Chris@1296 25 validates_presence_of :name, :field_format
Chris@1296 26 validates_uniqueness_of :name, :scope => :type
Chris@1296 27 validates_length_of :name, :maximum => 30
Chris@1296 28 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
Chris@1296 29
Chris@1296 30 validate :validate_custom_field
Chris@1296 31 before_validation :set_searchable
Chris@1296 32
Chris@1296 33 CUSTOM_FIELDS_TABS = [
Chris@1296 34 {:name => 'IssueCustomField', :partial => 'custom_fields/index',
Chris@1296 35 :label => :label_issue_plural},
Chris@1296 36 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
Chris@1296 37 :label => :label_spent_time},
Chris@1296 38 {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
Chris@1296 39 :label => :label_project_plural},
Chris@1296 40 {:name => 'VersionCustomField', :partial => 'custom_fields/index',
Chris@1296 41 :label => :label_version_plural},
Chris@1296 42 {:name => 'UserCustomField', :partial => 'custom_fields/index',
Chris@1296 43 :label => :label_user_plural},
Chris@1296 44 {:name => 'GroupCustomField', :partial => 'custom_fields/index',
Chris@1296 45 :label => :label_group_plural},
Chris@1296 46 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
Chris@1296 47 :label => TimeEntryActivity::OptionName},
Chris@1296 48 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
Chris@1296 49 :label => IssuePriority::OptionName},
Chris@1296 50 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
Chris@1296 51 :label => DocumentCategory::OptionName}
Chris@1296 52 ]
Chris@1296 53
Chris@1296 54 CUSTOM_FIELDS_NAMES = CUSTOM_FIELDS_TABS.collect{|v| v[:name]}
Chris@1296 55
Chris@1296 56 def field_format=(arg)
Chris@1296 57 # cannot change format of a saved custom field
Chris@1296 58 super if new_record?
Chris@1296 59 end
Chris@1296 60
Chris@1296 61 def set_searchable
Chris@1296 62 # make sure these fields are not searchable
Chris@1296 63 self.searchable = false if %w(int float date bool).include?(field_format)
Chris@1296 64 # make sure only these fields can have multiple values
Chris@1296 65 self.multiple = false unless %w(list user version).include?(field_format)
Chris@1296 66 true
Chris@1296 67 end
Chris@1296 68
Chris@1296 69 def validate_custom_field
Chris@1296 70 if self.field_format == "list"
Chris@1296 71 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
Chris@1296 72 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
Chris@1296 73 end
Chris@1296 74
Chris@1296 75 if regexp.present?
Chris@1296 76 begin
Chris@1296 77 Regexp.new(regexp)
Chris@1296 78 rescue
Chris@1296 79 errors.add(:regexp, :invalid)
Chris@1296 80 end
Chris@1296 81 end
Chris@1296 82
Chris@1296 83 if default_value.present? && !valid_field_value?(default_value)
Chris@1296 84 errors.add(:default_value, :invalid)
Chris@1296 85 end
Chris@1296 86 end
Chris@1296 87
Chris@1296 88 def possible_values_options(obj=nil)
Chris@1296 89 case field_format
Chris@1296 90 when 'user', 'version'
Chris@1296 91 if obj.respond_to?(:project) && obj.project
Chris@1296 92 case field_format
Chris@1296 93 when 'user'
Chris@1296 94 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
Chris@1296 95 when 'version'
Chris@1296 96 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]}
Chris@1296 97 end
Chris@1296 98 elsif obj.is_a?(Array)
Chris@1296 99 obj.collect {|o| possible_values_options(o)}.reduce(:&)
Chris@1296 100 else
Chris@1296 101 []
Chris@1296 102 end
Chris@1296 103 when 'bool'
Chris@1296 104 [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']]
Chris@1296 105 else
Chris@1296 106 possible_values || []
Chris@1296 107 end
Chris@1296 108 end
Chris@1296 109
Chris@1296 110 def possible_values(obj=nil)
Chris@1296 111 case field_format
Chris@1296 112 when 'user', 'version'
Chris@1296 113 possible_values_options(obj).collect(&:last)
Chris@1296 114 when 'bool'
Chris@1296 115 ['1', '0']
Chris@1296 116 else
Chris@1296 117 values = super()
Chris@1296 118 if values.is_a?(Array)
Chris@1296 119 values.each do |value|
Chris@1296 120 value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
Chris@1296 121 end
Chris@1296 122 end
Chris@1296 123 values || []
Chris@1296 124 end
Chris@1296 125 end
Chris@1296 126
Chris@1296 127 # Makes possible_values accept a multiline string
Chris@1296 128 def possible_values=(arg)
Chris@1296 129 if arg.is_a?(Array)
Chris@1296 130 super(arg.compact.collect(&:strip).select {|v| !v.blank?})
Chris@1296 131 else
Chris@1296 132 self.possible_values = arg.to_s.split(/[\n\r]+/)
Chris@1296 133 end
Chris@1296 134 end
Chris@1296 135
Chris@1296 136 def cast_value(value)
Chris@1296 137 casted = nil
Chris@1296 138 unless value.blank?
Chris@1296 139 case field_format
Chris@1296 140 when 'string', 'text', 'list'
Chris@1296 141 casted = value
Chris@1296 142 when 'date'
Chris@1296 143 casted = begin; value.to_date; rescue; nil end
Chris@1296 144 when 'bool'
Chris@1296 145 casted = (value == '1' ? true : false)
Chris@1296 146 when 'int'
Chris@1296 147 casted = value.to_i
Chris@1296 148 when 'float'
Chris@1296 149 casted = value.to_f
Chris@1296 150 when 'user', 'version'
Chris@1296 151 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
Chris@1296 152 end
Chris@1296 153 end
Chris@1296 154 casted
Chris@1296 155 end
Chris@1296 156
Chris@1296 157 def value_from_keyword(keyword, customized)
Chris@1296 158 possible_values_options = possible_values_options(customized)
Chris@1296 159 if possible_values_options.present?
Chris@1296 160 keyword = keyword.to_s.downcase
Chris@1296 161 if v = possible_values_options.detect {|text, id| text.downcase == keyword}
Chris@1296 162 if v.is_a?(Array)
Chris@1296 163 v.last
Chris@1296 164 else
Chris@1296 165 v
Chris@1296 166 end
Chris@1296 167 end
Chris@1296 168 else
Chris@1296 169 keyword
Chris@1296 170 end
Chris@1296 171 end
Chris@1296 172
Chris@1296 173 # Returns a ORDER BY clause that can used to sort customized
Chris@1296 174 # objects by their value of the custom field.
Chris@1296 175 # Returns nil if the custom field can not be used for sorting.
Chris@1296 176 def order_statement
Chris@1296 177 return nil if multiple?
Chris@1296 178 case field_format
Chris@1296 179 when 'string', 'text', 'list', 'date', 'bool'
Chris@1296 180 # COALESCE is here to make sure that blank and NULL values are sorted equally
Chris@1296 181 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
Chris@1296 182 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
Chris@1296 183 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
Chris@1296 184 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
Chris@1296 185 when 'int', 'float'
Chris@1296 186 # Make the database cast values into numeric
Chris@1296 187 # Postgresql will raise an error if a value can not be casted!
Chris@1296 188 # CustomValue validations should ensure that it doesn't occur
Chris@1296 189 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
Chris@1296 190 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
Chris@1296 191 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
Chris@1296 192 " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
Chris@1296 193 when 'user', 'version'
Chris@1296 194 value_class.fields_for_order_statement(value_join_alias)
Chris@1296 195 else
Chris@1296 196 nil
Chris@1296 197 end
Chris@1296 198 end
Chris@1296 199
Chris@1296 200 # Returns a GROUP BY clause that can used to group by custom value
Chris@1296 201 # Returns nil if the custom field can not be used for grouping.
Chris@1296 202 def group_statement
Chris@1296 203 return nil if multiple?
Chris@1296 204 case field_format
Chris@1296 205 when 'list', 'date', 'bool', 'int'
Chris@1296 206 order_statement
Chris@1296 207 when 'user', 'version'
Chris@1296 208 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
Chris@1296 209 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
Chris@1296 210 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
Chris@1296 211 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
Chris@1296 212 else
Chris@1296 213 nil
Chris@1296 214 end
Chris@1296 215 end
Chris@1296 216
Chris@1296 217 def join_for_order_statement
Chris@1296 218 case field_format
Chris@1296 219 when 'user', 'version'
Chris@1296 220 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
Chris@1296 221 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
Chris@1296 222 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
Chris@1296 223 " AND #{join_alias}.custom_field_id = #{id}" +
Chris@1296 224 " AND #{join_alias}.value <> ''" +
Chris@1296 225 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
Chris@1296 226 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
Chris@1296 227 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
Chris@1296 228 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" +
Chris@1296 229 " LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" +
Chris@1296 230 " ON CAST(#{join_alias}.value as decimal(60,0)) = #{value_join_alias}.id"
Chris@1296 231 else
Chris@1296 232 nil
Chris@1296 233 end
Chris@1296 234 end
Chris@1296 235
Chris@1296 236 def join_alias
Chris@1296 237 "cf_#{id}"
Chris@1296 238 end
Chris@1296 239
Chris@1296 240 def value_join_alias
Chris@1296 241 join_alias + "_" + field_format
Chris@1296 242 end
Chris@1296 243
Chris@1296 244 def <=>(field)
Chris@1296 245 position <=> field.position
Chris@1296 246 end
Chris@1296 247
Chris@1296 248 # Returns the class that values represent
Chris@1296 249 def value_class
Chris@1296 250 case field_format
Chris@1296 251 when 'user', 'version'
Chris@1296 252 field_format.classify.constantize
Chris@1296 253 else
Chris@1296 254 nil
Chris@1296 255 end
Chris@1296 256 end
Chris@1296 257
Chris@1296 258 def self.customized_class
Chris@1296 259 self.name =~ /^(.+)CustomField$/
Chris@1296 260 begin; $1.constantize; rescue nil; end
Chris@1296 261 end
Chris@1296 262
Chris@1296 263 # to move in project_custom_field
Chris@1296 264 def self.for_all
Chris@1296 265 find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
Chris@1296 266 end
Chris@1296 267
Chris@1296 268 def type_name
Chris@1296 269 nil
Chris@1296 270 end
Chris@1296 271
Chris@1296 272 # Returns the error messages for the given value
Chris@1296 273 # or an empty array if value is a valid value for the custom field
Chris@1296 274 def validate_field_value(value)
Chris@1296 275 errs = []
Chris@1296 276 if value.is_a?(Array)
Chris@1296 277 if !multiple?
Chris@1296 278 errs << ::I18n.t('activerecord.errors.messages.invalid')
Chris@1296 279 end
Chris@1296 280 if is_required? && value.detect(&:present?).nil?
Chris@1296 281 errs << ::I18n.t('activerecord.errors.messages.blank')
Chris@1296 282 end
Chris@1296 283 value.each {|v| errs += validate_field_value_format(v)}
Chris@1296 284 else
Chris@1296 285 if is_required? && value.blank?
Chris@1296 286 errs << ::I18n.t('activerecord.errors.messages.blank')
Chris@1296 287 end
Chris@1296 288 errs += validate_field_value_format(value)
Chris@1296 289 end
Chris@1296 290 errs
Chris@1296 291 end
Chris@1296 292
Chris@1296 293 # Returns true if value is a valid value for the custom field
Chris@1296 294 def valid_field_value?(value)
Chris@1296 295 validate_field_value(value).empty?
Chris@1296 296 end
Chris@1296 297
Chris@1296 298 def format_in?(*args)
Chris@1296 299 args.include?(field_format)
Chris@1296 300 end
Chris@1296 301
Chris@1296 302 protected
Chris@1296 303
Chris@1296 304 # Returns the error message for the given value regarding its format
Chris@1296 305 def validate_field_value_format(value)
Chris@1296 306 errs = []
Chris@1296 307 if value.present?
Chris@1296 308 errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp)
Chris@1296 309 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length
Chris@1296 310 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length
Chris@1296 311
Chris@1296 312 # Format specific validations
Chris@1296 313 case field_format
Chris@1296 314 when 'int'
Chris@1296 315 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
Chris@1296 316 when 'float'
Chris@1296 317 begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end
Chris@1296 318 when 'date'
Chris@1296 319 errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
Chris@1296 320 when 'list'
Chris@1296 321 errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value)
Chris@1296 322 end
Chris@1296 323 end
Chris@1296 324 errs
Chris@1296 325 end
Chris@1296 326 end