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