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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # redMine - project management software
  2. # Copyright (C) 2006 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. has_many :custom_values, :dependent => :delete_all
  19. acts_as_list :scope => 'type = \'#{self.class}\''
  20. serialize :possible_values
  21. FIELD_FORMATS = { "string" => { :name => :label_string, :order => 1 },
  22. "text" => { :name => :label_text, :order => 2 },
  23. "int" => { :name => :label_integer, :order => 3 },
  24. "float" => { :name => :label_float, :order => 4 },
  25. "list" => { :name => :label_list, :order => 5 },
  26. "date" => { :name => :label_date, :order => 6 },
  27. "bool" => { :name => :label_boolean, :order => 7 }
  28. }.freeze
  29. validates_presence_of :name, :field_format
  30. validates_uniqueness_of :name
  31. validates_length_of :name, :maximum => 30
  32. validates_format_of :name, :with => /^[\w\s\'\-]*$/i
  33. validates_inclusion_of :field_format, :in => FIELD_FORMATS.keys
  34. def initialize(attributes = nil)
  35. super
  36. self.possible_values ||= []
  37. end
  38. def before_validation
  39. # remove empty values
  40. self.possible_values = self.possible_values.collect{|v| v unless v.empty?}.compact
  41. # make sure these fields are not searchable
  42. self.searchable = false if %w(int float date bool).include?(field_format)
  43. true
  44. end
  45. def validate
  46. if self.field_format == "list"
  47. errors.add(:possible_values, :activerecord_error_blank) if self.possible_values.nil? || self.possible_values.empty?
  48. errors.add(:possible_values, :activerecord_error_invalid) unless self.possible_values.is_a? Array
  49. end
  50. # validate default value
  51. v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil)
  52. v.custom_field.is_required = false
  53. errors.add(:default_value, :activerecord_error_invalid) unless v.valid?
  54. end
  55. def <=>(field)
  56. position <=> field.position
  57. end
  58. # to move in project_custom_field
  59. def self.for_all
  60. find(:all, :conditions => ["is_for_all=?", true])
  61. end
  62. def type_name
  63. nil
  64. end
  65. end