To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / custom_value.rb @ 912:5e80956cc792

History | View | Annotate | Download (2.44 KB)

1 909:cbb26bc654de Chris
# Redmine - project management software
2
# Copyright (C) 2006-2011  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 CustomValue < ActiveRecord::Base
19
  belongs_to :custom_field
20
  belongs_to :customized, :polymorphic => true
21
22 909:cbb26bc654de Chris
  validate :validate_custom_value
23
24 0:513646585e45 Chris
  def after_initialize
25
    if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?))
26
      self.value ||= custom_field.default_value
27
    end
28
  end
29 909:cbb26bc654de Chris
30 0:513646585e45 Chris
  # Returns true if the boolean custom value is true
31
  def true?
32
    self.value == '1'
33
  end
34 909:cbb26bc654de Chris
35 0:513646585e45 Chris
  def editable?
36
    custom_field.editable?
37
  end
38 909:cbb26bc654de Chris
39 37:94944d00e43c chris
  def visible?
40
    custom_field.visible?
41
  end
42 909:cbb26bc654de Chris
43 0:513646585e45 Chris
  def required?
44
    custom_field.is_required?
45
  end
46 909:cbb26bc654de Chris
47 0:513646585e45 Chris
  def to_s
48
    value.to_s
49
  end
50 909:cbb26bc654de Chris
51 0:513646585e45 Chris
protected
52 909:cbb26bc654de Chris
  def validate_custom_value
53 0:513646585e45 Chris
    if value.blank?
54 909:cbb26bc654de Chris
      errors.add(:value, :blank) if custom_field.is_required? and value.blank?
55 0:513646585e45 Chris
    else
56
      errors.add(:value, :invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
57
      errors.add(:value, :too_short, :count => custom_field.min_length) if custom_field.min_length > 0 and value.length < custom_field.min_length
58
      errors.add(:value, :too_long, :count => custom_field.max_length) if custom_field.max_length > 0 and value.length > custom_field.max_length
59 909:cbb26bc654de Chris
60 0:513646585e45 Chris
      # Format specific validations
61
      case custom_field.field_format
62
      when 'int'
63
        errors.add(:value, :not_a_number) unless value =~ /^[+-]?\d+$/
64
      when 'float'
65
        begin; Kernel.Float(value); rescue; errors.add(:value, :invalid) end
66
      when 'date'
67 909:cbb26bc654de Chris
        errors.add(:value, :not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
68 0:513646585e45 Chris
      when 'list'
69
        errors.add(:value, :inclusion) unless custom_field.possible_values.include?(value)
70
      end
71
    end
72
  end
73
end