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