annotate app/models/custom_field.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class CustomField < ActiveRecord::Base
Chris@1115 19 include Redmine::SubclassFactory
Chris@1115 20
Chris@0 21 has_many :custom_values, :dependent => :delete_all
Chris@1464 22 has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "custom_field_id"
Chris@0 23 acts_as_list :scope => 'type = \'#{self.class}\''
Chris@0 24 serialize :possible_values
Chris@1517 25 store :format_store
Chris@909 26
Chris@0 27 validates_presence_of :name, :field_format
Chris@0 28 validates_uniqueness_of :name, :scope => :type
Chris@0 29 validates_length_of :name, :maximum => 30
Chris@1517 30 validates_inclusion_of :field_format, :in => Proc.new { Redmine::FieldFormat.available_formats }
Chris@1464 31 validate :validate_custom_field
Chris@0 32
Chris@1115 33 before_validation :set_searchable
Chris@1517 34 before_save do |field|
Chris@1517 35 field.format.before_custom_field_save(field)
Chris@1517 36 end
Chris@1464 37 after_save :handle_multiplicity_change
Chris@1464 38 after_save do |field|
Chris@1464 39 if field.visible_changed? && field.visible
Chris@1464 40 field.roles.clear
Chris@1464 41 end
Chris@1464 42 end
Chris@909 43
Chris@1464 44 scope :sorted, lambda { order("#{table_name}.position ASC") }
Chris@1464 45 scope :visible, lambda {|*args|
Chris@1464 46 user = args.shift || User.current
Chris@1464 47 if user.admin?
Chris@1464 48 # nop
Chris@1464 49 elsif user.memberships.any?
Chris@1464 50 where("#{table_name}.visible = ? OR #{table_name}.id IN (SELECT DISTINCT cfr.custom_field_id FROM #{Member.table_name} m" +
Chris@1464 51 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
Chris@1464 52 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
Chris@1464 53 " WHERE m.user_id = ?)",
Chris@1464 54 true, user.id)
Chris@1464 55 else
Chris@1464 56 where(:visible => true)
Chris@1464 57 end
Chris@1464 58 }
Chris@1115 59
Chris@1464 60 def visible_by?(project, user=User.current)
Chris@1464 61 visible? || user.admin?
Chris@1464 62 end
Chris@1115 63
Chris@1517 64 def format
Chris@1517 65 @format ||= Redmine::FieldFormat.find(field_format)
Chris@1517 66 end
Chris@1517 67
Chris@1115 68 def field_format=(arg)
Chris@1115 69 # cannot change format of a saved custom field
Chris@1517 70 if new_record?
Chris@1517 71 @format = nil
Chris@1517 72 super
Chris@1517 73 end
Chris@0 74 end
Chris@909 75
Chris@1115 76 def set_searchable
Chris@0 77 # make sure these fields are not searchable
Chris@1517 78 self.searchable = false unless format.class.searchable_supported
Chris@1115 79 # make sure only these fields can have multiple values
Chris@1517 80 self.multiple = false unless format.class.multiple_supported
Chris@0 81 true
Chris@0 82 end
Chris@909 83
Chris@1115 84 def validate_custom_field
Chris@1517 85 format.validate_custom_field(self).each do |attribute, message|
Chris@1517 86 errors.add attribute, message
Chris@0 87 end
Chris@909 88
Chris@909 89 if regexp.present?
Chris@909 90 begin
Chris@909 91 Regexp.new(regexp)
Chris@909 92 rescue
Chris@909 93 errors.add(:regexp, :invalid)
Chris@909 94 end
Chris@909 95 end
Chris@909 96
Chris@1517 97 if default_value.present?
Chris@1517 98 validate_field_value(default_value).each do |message|
Chris@1517 99 errors.add :default_value, message
Chris@1517 100 end
Chris@1115 101 end
Chris@0 102 end
Chris@909 103
Chris@1517 104 def possible_custom_value_options(custom_value)
Chris@1517 105 format.possible_custom_value_options(custom_value)
Chris@1517 106 end
Chris@1517 107
Chris@1517 108 def possible_values_options(object=nil)
Chris@1517 109 if object.is_a?(Array)
Chris@1517 110 object.map {|o| format.possible_values_options(self, o)}.reduce(:&) || []
Chris@441 111 else
Chris@1517 112 format.possible_values_options(self, object) || []
Chris@441 113 end
Chris@441 114 end
Chris@909 115
Chris@1517 116 def possible_values
Chris@1517 117 values = read_attribute(:possible_values)
Chris@1517 118 if values.is_a?(Array)
Chris@1517 119 values.each do |value|
Chris@1517 120 value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
Chris@1517 121 end
Chris@1517 122 values
Chris@441 123 else
Chris@1517 124 []
Chris@441 125 end
Chris@441 126 end
Chris@909 127
Chris@0 128 # Makes possible_values accept a multiline string
Chris@0 129 def possible_values=(arg)
Chris@0 130 if arg.is_a?(Array)
Chris@1517 131 values = arg.compact.collect(&:strip).select {|v| !v.blank?}
Chris@1517 132 write_attribute(:possible_values, values)
Chris@0 133 else
Chris@0 134 self.possible_values = arg.to_s.split(/[\n\r]+/)
Chris@0 135 end
Chris@0 136 end
Chris@909 137
Chris@0 138 def cast_value(value)
Chris@1517 139 format.cast_value(self, value)
Chris@0 140 end
Chris@909 141
Chris@1115 142 def value_from_keyword(keyword, customized)
Chris@1115 143 possible_values_options = possible_values_options(customized)
Chris@1115 144 if possible_values_options.present?
Chris@1115 145 keyword = keyword.to_s.downcase
Chris@1115 146 if v = possible_values_options.detect {|text, id| text.downcase == keyword}
Chris@1115 147 if v.is_a?(Array)
Chris@1115 148 v.last
Chris@1115 149 else
Chris@1115 150 v
Chris@1115 151 end
Chris@1115 152 end
Chris@1115 153 else
Chris@1115 154 keyword
Chris@1115 155 end
Chris@1115 156 end
Chris@1464 157
Chris@0 158 # Returns a ORDER BY clause that can used to sort customized
Chris@0 159 # objects by their value of the custom field.
Chris@1115 160 # Returns nil if the custom field can not be used for sorting.
Chris@0 161 def order_statement
Chris@1115 162 return nil if multiple?
Chris@1517 163 format.order_statement(self)
Chris@0 164 end
Chris@0 165
Chris@1115 166 # Returns a GROUP BY clause that can used to group by custom value
Chris@1115 167 # Returns nil if the custom field can not be used for grouping.
Chris@1464 168 def group_statement
Chris@1115 169 return nil if multiple?
Chris@1517 170 format.group_statement(self)
Chris@1115 171 end
Chris@1115 172
Chris@1115 173 def join_for_order_statement
Chris@1517 174 format.join_for_order_statement(self)
Chris@1115 175 end
Chris@1115 176
Chris@1517 177 def visibility_by_project_condition(project_key=nil, user=User.current, id_column=nil)
Chris@1464 178 if visible? || user.admin?
Chris@1464 179 "1=1"
Chris@1464 180 elsif user.anonymous?
Chris@1464 181 "1=0"
Chris@1464 182 else
Chris@1464 183 project_key ||= "#{self.class.customized_class.table_name}.project_id"
Chris@1517 184 id_column ||= id
Chris@1464 185 "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
Chris@1464 186 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
Chris@1464 187 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
Chris@1517 188 " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id_column})"
Chris@1464 189 end
Chris@1464 190 end
Chris@1464 191
Chris@1464 192 def self.visibility_condition
Chris@1464 193 if user.admin?
Chris@1464 194 "1=1"
Chris@1464 195 elsif user.anonymous?
Chris@1464 196 "#{table_name}.visible"
Chris@1464 197 else
Chris@1464 198 "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
Chris@1464 199 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
Chris@1464 200 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
Chris@1464 201 " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})"
Chris@1464 202 end
Chris@1464 203 end
Chris@1464 204
Chris@0 205 def <=>(field)
Chris@0 206 position <=> field.position
Chris@0 207 end
Chris@909 208
Chris@1115 209 # Returns the class that values represent
Chris@1115 210 def value_class
Chris@1517 211 format.target_class if format.respond_to?(:target_class)
Chris@1115 212 end
Chris@1115 213
Chris@0 214 def self.customized_class
Chris@0 215 self.name =~ /^(.+)CustomField$/
Chris@1464 216 $1.constantize rescue nil
Chris@0 217 end
Chris@909 218
Chris@0 219 # to move in project_custom_field
Chris@0 220 def self.for_all
Chris@1464 221 where(:is_for_all => true).order('position').all
Chris@0 222 end
Chris@909 223
Chris@0 224 def type_name
Chris@0 225 nil
Chris@0 226 end
Chris@1115 227
Chris@1115 228 # Returns the error messages for the given value
Chris@1115 229 # or an empty array if value is a valid value for the custom field
Chris@1517 230 def validate_custom_value(custom_value)
Chris@1517 231 value = custom_value.value
Chris@1115 232 errs = []
Chris@1115 233 if value.is_a?(Array)
Chris@1115 234 if !multiple?
Chris@1115 235 errs << ::I18n.t('activerecord.errors.messages.invalid')
Chris@1115 236 end
Chris@1115 237 if is_required? && value.detect(&:present?).nil?
Chris@1115 238 errs << ::I18n.t('activerecord.errors.messages.blank')
Chris@1115 239 end
Chris@1115 240 else
Chris@1115 241 if is_required? && value.blank?
Chris@1115 242 errs << ::I18n.t('activerecord.errors.messages.blank')
Chris@1115 243 end
Chris@1115 244 end
Chris@1517 245 errs += format.validate_custom_value(custom_value)
Chris@1115 246 errs
Chris@1115 247 end
Chris@1115 248
Chris@1517 249 # Returns the error messages for the default custom field value
Chris@1517 250 def validate_field_value(value)
Chris@1517 251 validate_custom_value(CustomValue.new(:custom_field => self, :value => value))
Chris@1517 252 end
Chris@1517 253
Chris@1115 254 # Returns true if value is a valid value for the custom field
Chris@1115 255 def valid_field_value?(value)
Chris@1115 256 validate_field_value(value).empty?
Chris@1115 257 end
Chris@1115 258
Chris@1115 259 def format_in?(*args)
Chris@1115 260 args.include?(field_format)
Chris@1115 261 end
Chris@1115 262
Chris@1115 263 protected
Chris@1115 264
Chris@1464 265 # Removes multiple values for the custom field after setting the multiple attribute to false
Chris@1464 266 # We kepp the value with the highest id for each customized object
Chris@1464 267 def handle_multiplicity_change
Chris@1464 268 if !new_record? && multiple_was && !multiple
Chris@1464 269 ids = custom_values.
Chris@1464 270 where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" +
Chris@1464 271 " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" +
Chris@1464 272 " AND cve.id > #{CustomValue.table_name}.id)").
Chris@1464 273 pluck(:id)
Chris@1464 274
Chris@1464 275 if ids.any?
Chris@1464 276 custom_values.where(:id => ids).delete_all
Chris@1464 277 end
Chris@1464 278 end
Chris@1464 279 end
Chris@0 280 end
Chris@1517 281
Chris@1517 282 require_dependency 'redmine/field_format'