annotate .svn/pristine/90/909ec0fd795dbd3e64eadc42e898b993cb6d9e6d.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 class CustomField < ActiveRecord::Base
Chris@1494 19 include Redmine::SubclassFactory
Chris@1494 20
Chris@1494 21 has_many :custom_values, :dependent => :delete_all
Chris@1494 22 has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "custom_field_id"
Chris@1494 23 acts_as_list :scope => 'type = \'#{self.class}\''
Chris@1494 24 serialize :possible_values
Chris@1494 25
Chris@1494 26 validates_presence_of :name, :field_format
Chris@1494 27 validates_uniqueness_of :name, :scope => :type
Chris@1494 28 validates_length_of :name, :maximum => 30
Chris@1494 29 validates_inclusion_of :field_format, :in => Proc.new { Redmine::CustomFieldFormat.available_formats }
Chris@1494 30 validate :validate_custom_field
Chris@1494 31
Chris@1494 32 before_validation :set_searchable
Chris@1494 33 after_save :handle_multiplicity_change
Chris@1494 34 after_save do |field|
Chris@1494 35 if field.visible_changed? && field.visible
Chris@1494 36 field.roles.clear
Chris@1494 37 end
Chris@1494 38 end
Chris@1494 39
Chris@1494 40 scope :sorted, lambda { order("#{table_name}.position ASC") }
Chris@1494 41 scope :visible, lambda {|*args|
Chris@1494 42 user = args.shift || User.current
Chris@1494 43 if user.admin?
Chris@1494 44 # nop
Chris@1494 45 elsif user.memberships.any?
Chris@1494 46 where("#{table_name}.visible = ? OR #{table_name}.id IN (SELECT DISTINCT cfr.custom_field_id FROM #{Member.table_name} m" +
Chris@1494 47 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
Chris@1494 48 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
Chris@1494 49 " WHERE m.user_id = ?)",
Chris@1494 50 true, user.id)
Chris@1494 51 else
Chris@1494 52 where(:visible => true)
Chris@1494 53 end
Chris@1494 54 }
Chris@1494 55
Chris@1494 56 def visible_by?(project, user=User.current)
Chris@1494 57 visible? || user.admin?
Chris@1494 58 end
Chris@1494 59
Chris@1494 60 def field_format=(arg)
Chris@1494 61 # cannot change format of a saved custom field
Chris@1494 62 super if new_record?
Chris@1494 63 end
Chris@1494 64
Chris@1494 65 def set_searchable
Chris@1494 66 # make sure these fields are not searchable
Chris@1494 67 self.searchable = false if %w(int float date bool).include?(field_format)
Chris@1494 68 # make sure only these fields can have multiple values
Chris@1494 69 self.multiple = false unless %w(list user version).include?(field_format)
Chris@1494 70 true
Chris@1494 71 end
Chris@1494 72
Chris@1494 73 def validate_custom_field
Chris@1494 74 if self.field_format == "list"
Chris@1494 75 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
Chris@1494 76 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
Chris@1494 77 end
Chris@1494 78
Chris@1494 79 if regexp.present?
Chris@1494 80 begin
Chris@1494 81 Regexp.new(regexp)
Chris@1494 82 rescue
Chris@1494 83 errors.add(:regexp, :invalid)
Chris@1494 84 end
Chris@1494 85 end
Chris@1494 86
Chris@1494 87 if default_value.present? && !valid_field_value?(default_value)
Chris@1494 88 errors.add(:default_value, :invalid)
Chris@1494 89 end
Chris@1494 90 end
Chris@1494 91
Chris@1494 92 def possible_values_options(obj=nil)
Chris@1494 93 case field_format
Chris@1494 94 when 'user', 'version'
Chris@1494 95 if obj.respond_to?(:project) && obj.project
Chris@1494 96 case field_format
Chris@1494 97 when 'user'
Chris@1494 98 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
Chris@1494 99 when 'version'
Chris@1494 100 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]}
Chris@1494 101 end
Chris@1494 102 elsif obj.is_a?(Array)
Chris@1494 103 obj.collect {|o| possible_values_options(o)}.reduce(:&)
Chris@1494 104 else
Chris@1494 105 []
Chris@1494 106 end
Chris@1494 107 when 'bool'
Chris@1494 108 [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']]
Chris@1494 109 else
Chris@1494 110 possible_values || []
Chris@1494 111 end
Chris@1494 112 end
Chris@1494 113
Chris@1494 114 def possible_values(obj=nil)
Chris@1494 115 case field_format
Chris@1494 116 when 'user', 'version'
Chris@1494 117 possible_values_options(obj).collect(&:last)
Chris@1494 118 when 'bool'
Chris@1494 119 ['1', '0']
Chris@1494 120 else
Chris@1494 121 values = super()
Chris@1494 122 if values.is_a?(Array)
Chris@1494 123 values.each do |value|
Chris@1494 124 value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
Chris@1494 125 end
Chris@1494 126 values
Chris@1494 127 else
Chris@1494 128 []
Chris@1494 129 end
Chris@1494 130 end
Chris@1494 131 end
Chris@1494 132
Chris@1494 133 # Makes possible_values accept a multiline string
Chris@1494 134 def possible_values=(arg)
Chris@1494 135 if arg.is_a?(Array)
Chris@1494 136 super(arg.compact.collect(&:strip).select {|v| !v.blank?})
Chris@1494 137 else
Chris@1494 138 self.possible_values = arg.to_s.split(/[\n\r]+/)
Chris@1494 139 end
Chris@1494 140 end
Chris@1494 141
Chris@1494 142 def cast_value(value)
Chris@1494 143 casted = nil
Chris@1494 144 unless value.blank?
Chris@1494 145 case field_format
Chris@1494 146 when 'string', 'text', 'list'
Chris@1494 147 casted = value
Chris@1494 148 when 'date'
Chris@1494 149 casted = begin; value.to_date; rescue; nil end
Chris@1494 150 when 'bool'
Chris@1494 151 casted = (value == '1' ? true : false)
Chris@1494 152 when 'int'
Chris@1494 153 casted = value.to_i
Chris@1494 154 when 'float'
Chris@1494 155 casted = value.to_f
Chris@1494 156 when 'user', 'version'
Chris@1494 157 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
Chris@1494 158 end
Chris@1494 159 end
Chris@1494 160 casted
Chris@1494 161 end
Chris@1494 162
Chris@1494 163 def value_from_keyword(keyword, customized)
Chris@1494 164 possible_values_options = possible_values_options(customized)
Chris@1494 165 if possible_values_options.present?
Chris@1494 166 keyword = keyword.to_s.downcase
Chris@1494 167 if v = possible_values_options.detect {|text, id| text.downcase == keyword}
Chris@1494 168 if v.is_a?(Array)
Chris@1494 169 v.last
Chris@1494 170 else
Chris@1494 171 v
Chris@1494 172 end
Chris@1494 173 end
Chris@1494 174 else
Chris@1494 175 keyword
Chris@1494 176 end
Chris@1494 177 end
Chris@1494 178
Chris@1494 179 # Returns a ORDER BY clause that can used to sort customized
Chris@1494 180 # objects by their value of the custom field.
Chris@1494 181 # Returns nil if the custom field can not be used for sorting.
Chris@1494 182 def order_statement
Chris@1494 183 return nil if multiple?
Chris@1494 184 case field_format
Chris@1494 185 when 'string', 'text', 'list', 'date', 'bool'
Chris@1494 186 # COALESCE is here to make sure that blank and NULL values are sorted equally
Chris@1494 187 "COALESCE(#{join_alias}.value, '')"
Chris@1494 188 when 'int', 'float'
Chris@1494 189 # Make the database cast values into numeric
Chris@1494 190 # Postgresql will raise an error if a value can not be casted!
Chris@1494 191 # CustomValue validations should ensure that it doesn't occur
Chris@1494 192 "CAST(CASE #{join_alias}.value WHEN '' THEN '0' ELSE #{join_alias}.value END AS decimal(30,3))"
Chris@1494 193 when 'user', 'version'
Chris@1494 194 value_class.fields_for_order_statement(value_join_alias)
Chris@1494 195 else
Chris@1494 196 nil
Chris@1494 197 end
Chris@1494 198 end
Chris@1494 199
Chris@1494 200 # Returns a GROUP BY clause that can used to group by custom value
Chris@1494 201 # Returns nil if the custom field can not be used for grouping.
Chris@1494 202 def group_statement
Chris@1494 203 return nil if multiple?
Chris@1494 204 case field_format
Chris@1494 205 when 'list', 'date', 'bool', 'int'
Chris@1494 206 order_statement
Chris@1494 207 when 'user', 'version'
Chris@1494 208 "COALESCE(#{join_alias}.value, '')"
Chris@1494 209 else
Chris@1494 210 nil
Chris@1494 211 end
Chris@1494 212 end
Chris@1494 213
Chris@1494 214 def join_for_order_statement
Chris@1494 215 case field_format
Chris@1494 216 when 'user', 'version'
Chris@1494 217 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
Chris@1494 218 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
Chris@1494 219 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
Chris@1494 220 " AND #{join_alias}.custom_field_id = #{id}" +
Chris@1494 221 " AND (#{visibility_by_project_condition})" +
Chris@1494 222 " AND #{join_alias}.value <> ''" +
Chris@1494 223 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
Chris@1494 224 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
Chris@1494 225 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
Chris@1494 226 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" +
Chris@1494 227 " LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" +
Chris@1494 228 " ON CAST(CASE #{join_alias}.value WHEN '' THEN '0' ELSE #{join_alias}.value END AS decimal(30,0)) = #{value_join_alias}.id"
Chris@1494 229 when 'int', 'float'
Chris@1494 230 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
Chris@1494 231 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
Chris@1494 232 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
Chris@1494 233 " AND #{join_alias}.custom_field_id = #{id}" +
Chris@1494 234 " AND (#{visibility_by_project_condition})" +
Chris@1494 235 " AND #{join_alias}.value <> ''" +
Chris@1494 236 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
Chris@1494 237 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
Chris@1494 238 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
Chris@1494 239 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)"
Chris@1494 240 when 'string', 'text', 'list', 'date', 'bool'
Chris@1494 241 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
Chris@1494 242 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
Chris@1494 243 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
Chris@1494 244 " AND #{join_alias}.custom_field_id = #{id}" +
Chris@1494 245 " AND (#{visibility_by_project_condition})" +
Chris@1494 246 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
Chris@1494 247 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
Chris@1494 248 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
Chris@1494 249 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)"
Chris@1494 250 else
Chris@1494 251 nil
Chris@1494 252 end
Chris@1494 253 end
Chris@1494 254
Chris@1494 255 def join_alias
Chris@1494 256 "cf_#{id}"
Chris@1494 257 end
Chris@1494 258
Chris@1494 259 def value_join_alias
Chris@1494 260 join_alias + "_" + field_format
Chris@1494 261 end
Chris@1494 262
Chris@1494 263 def visibility_by_project_condition(project_key=nil, user=User.current)
Chris@1494 264 if visible? || user.admin?
Chris@1494 265 "1=1"
Chris@1494 266 elsif user.anonymous?
Chris@1494 267 "1=0"
Chris@1494 268 else
Chris@1494 269 project_key ||= "#{self.class.customized_class.table_name}.project_id"
Chris@1494 270 "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
Chris@1494 271 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
Chris@1494 272 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
Chris@1494 273 " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})"
Chris@1494 274 end
Chris@1494 275 end
Chris@1494 276
Chris@1494 277 def self.visibility_condition
Chris@1494 278 if user.admin?
Chris@1494 279 "1=1"
Chris@1494 280 elsif user.anonymous?
Chris@1494 281 "#{table_name}.visible"
Chris@1494 282 else
Chris@1494 283 "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
Chris@1494 284 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
Chris@1494 285 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
Chris@1494 286 " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})"
Chris@1494 287 end
Chris@1494 288 end
Chris@1494 289
Chris@1494 290 def <=>(field)
Chris@1494 291 position <=> field.position
Chris@1494 292 end
Chris@1494 293
Chris@1494 294 # Returns the class that values represent
Chris@1494 295 def value_class
Chris@1494 296 case field_format
Chris@1494 297 when 'user', 'version'
Chris@1494 298 field_format.classify.constantize
Chris@1494 299 else
Chris@1494 300 nil
Chris@1494 301 end
Chris@1494 302 end
Chris@1494 303
Chris@1494 304 def self.customized_class
Chris@1494 305 self.name =~ /^(.+)CustomField$/
Chris@1494 306 $1.constantize rescue nil
Chris@1494 307 end
Chris@1494 308
Chris@1494 309 # to move in project_custom_field
Chris@1494 310 def self.for_all
Chris@1494 311 where(:is_for_all => true).order('position').all
Chris@1494 312 end
Chris@1494 313
Chris@1494 314 def type_name
Chris@1494 315 nil
Chris@1494 316 end
Chris@1494 317
Chris@1494 318 # Returns the error messages for the given value
Chris@1494 319 # or an empty array if value is a valid value for the custom field
Chris@1494 320 def validate_field_value(value)
Chris@1494 321 errs = []
Chris@1494 322 if value.is_a?(Array)
Chris@1494 323 if !multiple?
Chris@1494 324 errs << ::I18n.t('activerecord.errors.messages.invalid')
Chris@1494 325 end
Chris@1494 326 if is_required? && value.detect(&:present?).nil?
Chris@1494 327 errs << ::I18n.t('activerecord.errors.messages.blank')
Chris@1494 328 end
Chris@1494 329 value.each {|v| errs += validate_field_value_format(v)}
Chris@1494 330 else
Chris@1494 331 if is_required? && value.blank?
Chris@1494 332 errs << ::I18n.t('activerecord.errors.messages.blank')
Chris@1494 333 end
Chris@1494 334 errs += validate_field_value_format(value)
Chris@1494 335 end
Chris@1494 336 errs
Chris@1494 337 end
Chris@1494 338
Chris@1494 339 # Returns true if value is a valid value for the custom field
Chris@1494 340 def valid_field_value?(value)
Chris@1494 341 validate_field_value(value).empty?
Chris@1494 342 end
Chris@1494 343
Chris@1494 344 def format_in?(*args)
Chris@1494 345 args.include?(field_format)
Chris@1494 346 end
Chris@1494 347
Chris@1494 348 protected
Chris@1494 349
Chris@1494 350 # Returns the error message for the given value regarding its format
Chris@1494 351 def validate_field_value_format(value)
Chris@1494 352 errs = []
Chris@1494 353 unless value.to_s == ''
Chris@1494 354 errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp)
Chris@1494 355 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length
Chris@1494 356 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length
Chris@1494 357
Chris@1494 358 # Format specific validations
Chris@1494 359 case field_format
Chris@1494 360 when 'int'
Chris@1494 361 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
Chris@1494 362 when 'float'
Chris@1494 363 begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end
Chris@1494 364 when 'date'
Chris@1494 365 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@1494 366 when 'list'
Chris@1494 367 errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value)
Chris@1494 368 end
Chris@1494 369 end
Chris@1494 370 errs
Chris@1494 371 end
Chris@1494 372
Chris@1494 373 # Removes multiple values for the custom field after setting the multiple attribute to false
Chris@1494 374 # We kepp the value with the highest id for each customized object
Chris@1494 375 def handle_multiplicity_change
Chris@1494 376 if !new_record? && multiple_was && !multiple
Chris@1494 377 ids = custom_values.
Chris@1494 378 where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" +
Chris@1494 379 " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" +
Chris@1494 380 " AND cve.id > #{CustomValue.table_name}.id)").
Chris@1494 381 pluck(:id)
Chris@1494 382
Chris@1494 383 if ids.any?
Chris@1494 384 custom_values.where(:id => ids).delete_all
Chris@1494 385 end
Chris@1494 386 end
Chris@1494 387 end
Chris@1494 388 end