You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom_field.rb 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 Jean-Philippe Lang
  3. #
  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. #
  9. # 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. #
  14. # 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. class CustomField < ActiveRecord::Base
  18. include Redmine::SubclassFactory
  19. has_many :custom_values, :dependent => :delete_all
  20. acts_as_list :scope => 'type = \'#{self.class}\''
  21. serialize :possible_values
  22. validates_presence_of :name, :field_format
  23. validates_uniqueness_of :name, :scope => :type
  24. validates_length_of :name, :maximum => 30
  25. validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
  26. validate :validate_custom_field
  27. before_validation :set_searchable
  28. def initialize(attributes=nil, *args)
  29. super
  30. self.possible_values ||= []
  31. end
  32. def set_searchable
  33. # make sure these fields are not searchable
  34. self.searchable = false if %w(int float date bool).include?(field_format)
  35. # make sure only these fields can have multiple values
  36. self.multiple = false unless %w(list user version).include?(field_format)
  37. true
  38. end
  39. def validate_custom_field
  40. if self.field_format == "list"
  41. errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
  42. errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
  43. end
  44. if regexp.present?
  45. begin
  46. Regexp.new(regexp)
  47. rescue
  48. errors.add(:regexp, :invalid)
  49. end
  50. end
  51. if default_value.present? && !valid_field_value?(default_value)
  52. errors.add(:default_value, :invalid)
  53. end
  54. end
  55. def possible_values_options(obj=nil)
  56. case field_format
  57. when 'user', 'version'
  58. if obj.respond_to?(:project) && obj.project
  59. case field_format
  60. when 'user'
  61. obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
  62. when 'version'
  63. obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]}
  64. end
  65. elsif obj.is_a?(Array)
  66. obj.collect {|o| possible_values_options(o)}.reduce(:&)
  67. else
  68. []
  69. end
  70. when 'bool'
  71. [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']]
  72. else
  73. possible_values || []
  74. end
  75. end
  76. def possible_values(obj=nil)
  77. case field_format
  78. when 'user', 'version'
  79. possible_values_options(obj).collect(&:last)
  80. when 'bool'
  81. ['1', '0']
  82. else
  83. values = super()
  84. if values.is_a?(Array)
  85. values.each do |value|
  86. value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
  87. end
  88. end
  89. values
  90. end
  91. end
  92. # Makes possible_values accept a multiline string
  93. def possible_values=(arg)
  94. if arg.is_a?(Array)
  95. super(arg.compact.collect(&:strip).select {|v| !v.blank?})
  96. else
  97. self.possible_values = arg.to_s.split(/[\n\r]+/)
  98. end
  99. end
  100. def cast_value(value)
  101. casted = nil
  102. unless value.blank?
  103. case field_format
  104. when 'string', 'text', 'list'
  105. casted = value
  106. when 'date'
  107. casted = begin; value.to_date; rescue; nil end
  108. when 'bool'
  109. casted = (value == '1' ? true : false)
  110. when 'int'
  111. casted = value.to_i
  112. when 'float'
  113. casted = value.to_f
  114. when 'user', 'version'
  115. casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
  116. end
  117. end
  118. casted
  119. end
  120. # Returns a ORDER BY clause that can used to sort customized
  121. # objects by their value of the custom field.
  122. # Returns false, if the custom field can not be used for sorting.
  123. def order_statement
  124. return nil if multiple?
  125. case field_format
  126. when 'string', 'text', 'list', 'date', 'bool'
  127. # COALESCE is here to make sure that blank and NULL values are sorted equally
  128. "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
  129. " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
  130. " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
  131. " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
  132. when 'int', 'float'
  133. # Make the database cast values into numeric
  134. # Postgresql will raise an error if a value can not be casted!
  135. # CustomValue validations should ensure that it doesn't occur
  136. "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
  137. " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
  138. " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
  139. " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
  140. else
  141. nil
  142. end
  143. end
  144. def <=>(field)
  145. position <=> field.position
  146. end
  147. def self.customized_class
  148. self.name =~ /^(.+)CustomField$/
  149. begin; $1.constantize; rescue nil; end
  150. end
  151. # to move in project_custom_field
  152. def self.for_all
  153. find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
  154. end
  155. def type_name
  156. nil
  157. end
  158. # Returns the error messages for the given value
  159. # or an empty array if value is a valid value for the custom field
  160. def validate_field_value(value)
  161. errs = []
  162. if value.is_a?(Array)
  163. if !multiple?
  164. errs << ::I18n.t('activerecord.errors.messages.invalid')
  165. end
  166. if is_required? && value.detect(&:present?).nil?
  167. errs << ::I18n.t('activerecord.errors.messages.blank')
  168. end
  169. value.each {|v| errs += validate_field_value_format(v)}
  170. else
  171. if is_required? && value.blank?
  172. errs << ::I18n.t('activerecord.errors.messages.blank')
  173. end
  174. errs += validate_field_value_format(value)
  175. end
  176. errs
  177. end
  178. # Returns true if value is a valid value for the custom field
  179. def valid_field_value?(value)
  180. validate_field_value(value).empty?
  181. end
  182. protected
  183. # Returns the error message for the given value regarding its format
  184. def validate_field_value_format(value)
  185. errs = []
  186. if value.present?
  187. errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp)
  188. errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length
  189. errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length
  190. # Format specific validations
  191. case field_format
  192. when 'int'
  193. errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
  194. when 'float'
  195. begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end
  196. when 'date'
  197. errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
  198. when 'list'
  199. errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value)
  200. end
  201. end
  202. errs
  203. end
  204. end