comparison app/models/custom_field.rb @ 514:7eba09d624db live

Merge
author Chris Cannam
date Thu, 14 Jul 2011 10:50:53 +0100
parents cbce1fd3b1b7
children cbb26bc654de
comparison
equal deleted inserted replaced
512:b9aebdd7dd40 514:7eba09d624db
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
21 serialize :possible_values 21 serialize :possible_values
22 22
23 validates_presence_of :name, :field_format 23 validates_presence_of :name, :field_format
24 validates_uniqueness_of :name, :scope => :type 24 validates_uniqueness_of :name, :scope => :type
25 validates_length_of :name, :maximum => 30 25 validates_length_of :name, :maximum => 30
26 validates_format_of :name, :with => /^[\w\s\.\'\-]*$/i
27 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats 26 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
28 27
29 def initialize(attributes = nil) 28 def initialize(attributes = nil)
30 super 29 super
31 self.possible_values ||= [] 30 self.possible_values ||= []
47 v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil) 46 v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil)
48 v.custom_field.is_required = false 47 v.custom_field.is_required = false
49 errors.add(:default_value, :invalid) unless v.valid? 48 errors.add(:default_value, :invalid) unless v.valid?
50 end 49 end
51 50
51 def possible_values_options(obj=nil)
52 case field_format
53 when 'user', 'version'
54 if obj.respond_to?(:project) && obj.project
55 case field_format
56 when 'user'
57 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
58 when 'version'
59 obj.project.versions.sort.collect {|u| [u.to_s, u.id.to_s]}
60 end
61 elsif obj.is_a?(Array)
62 obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v}
63 else
64 []
65 end
66 else
67 read_attribute :possible_values
68 end
69 end
70
71 def possible_values(obj=nil)
72 case field_format
73 when 'user', 'version'
74 possible_values_options(obj).collect(&:last)
75 else
76 read_attribute :possible_values
77 end
78 end
79
52 # Makes possible_values accept a multiline string 80 # Makes possible_values accept a multiline string
53 def possible_values=(arg) 81 def possible_values=(arg)
54 if arg.is_a?(Array) 82 if arg.is_a?(Array)
55 write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?}) 83 write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?})
56 else 84 else
70 casted = (value == '1' ? true : false) 98 casted = (value == '1' ? true : false)
71 when 'int' 99 when 'int'
72 casted = value.to_i 100 casted = value.to_i
73 when 'float' 101 when 'float'
74 casted = value.to_f 102 casted = value.to_f
103 when 'user', 'version'
104 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
75 end 105 end
76 end 106 end
77 casted 107 casted
78 end 108 end
79 109