Chris@441: # Redmine - project management software Chris@441: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class CustomField < ActiveRecord::Base Chris@0: has_many :custom_values, :dependent => :delete_all Chris@0: acts_as_list :scope => 'type = \'#{self.class}\'' Chris@0: serialize :possible_values Chris@909: Chris@0: validates_presence_of :name, :field_format Chris@0: validates_uniqueness_of :name, :scope => :type Chris@0: validates_length_of :name, :maximum => 30 Chris@0: validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats Chris@0: Chris@909: validate :validate_values Chris@909: Chris@0: def initialize(attributes = nil) Chris@0: super Chris@0: self.possible_values ||= [] Chris@0: end Chris@909: Chris@0: def before_validation Chris@0: # make sure these fields are not searchable Chris@0: self.searchable = false if %w(int float date bool).include?(field_format) Chris@0: true Chris@0: end Chris@909: Chris@909: def validate_values Chris@0: if self.field_format == "list" Chris@0: errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? Chris@0: errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array Chris@0: end Chris@909: Chris@909: if regexp.present? Chris@909: begin Chris@909: Regexp.new(regexp) Chris@909: rescue Chris@909: errors.add(:regexp, :invalid) Chris@909: end Chris@909: end Chris@909: Chris@0: # validate default value Chris@0: v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil) Chris@0: v.custom_field.is_required = false Chris@0: errors.add(:default_value, :invalid) unless v.valid? Chris@0: end Chris@909: Chris@441: def possible_values_options(obj=nil) Chris@441: case field_format Chris@441: when 'user', 'version' Chris@441: if obj.respond_to?(:project) && obj.project Chris@441: case field_format Chris@441: when 'user' Chris@441: obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} Chris@441: when 'version' Chris@909: obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} Chris@441: end Chris@441: elsif obj.is_a?(Array) Chris@441: obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v} Chris@441: else Chris@441: [] Chris@441: end Chris@441: else Chris@441: read_attribute :possible_values Chris@441: end Chris@441: end Chris@909: Chris@441: def possible_values(obj=nil) Chris@441: case field_format Chris@441: when 'user', 'version' Chris@441: possible_values_options(obj).collect(&:last) Chris@441: else Chris@441: read_attribute :possible_values Chris@441: end Chris@441: end Chris@909: Chris@0: # Makes possible_values accept a multiline string Chris@0: def possible_values=(arg) Chris@0: if arg.is_a?(Array) Chris@0: write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?}) Chris@0: else Chris@0: self.possible_values = arg.to_s.split(/[\n\r]+/) Chris@0: end Chris@0: end Chris@909: Chris@0: def cast_value(value) Chris@0: casted = nil Chris@0: unless value.blank? Chris@0: case field_format Chris@0: when 'string', 'text', 'list' Chris@0: casted = value Chris@0: when 'date' Chris@0: casted = begin; value.to_date; rescue; nil end Chris@0: when 'bool' Chris@0: casted = (value == '1' ? true : false) Chris@0: when 'int' Chris@0: casted = value.to_i Chris@0: when 'float' Chris@0: casted = value.to_f Chris@441: when 'user', 'version' Chris@441: casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) Chris@0: end Chris@0: end Chris@0: casted Chris@0: end Chris@909: Chris@0: # Returns a ORDER BY clause that can used to sort customized Chris@0: # objects by their value of the custom field. Chris@0: # Returns false, if the custom field can not be used for sorting. Chris@0: def order_statement Chris@0: case field_format Chris@0: when 'string', 'text', 'list', 'date', 'bool' Chris@0: # COALESCE is here to make sure that blank and NULL values are sorted equally Chris@909: "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" + Chris@0: " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + Chris@0: " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + Chris@0: " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" Chris@0: when 'int', 'float' Chris@0: # Make the database cast values into numeric Chris@0: # Postgresql will raise an error if a value can not be casted! Chris@0: # CustomValue validations should ensure that it doesn't occur Chris@909: "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" + Chris@0: " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + Chris@0: " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + Chris@0: " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)" Chris@0: else Chris@0: nil Chris@0: end Chris@0: end Chris@0: Chris@0: def <=>(field) Chris@0: position <=> field.position Chris@0: end Chris@909: Chris@0: def self.customized_class Chris@0: self.name =~ /^(.+)CustomField$/ Chris@0: begin; $1.constantize; rescue nil; end Chris@0: end Chris@909: Chris@0: # to move in project_custom_field Chris@0: def self.for_all Chris@0: find(:all, :conditions => ["is_for_all=?", true], :order => 'position') Chris@0: end Chris@909: Chris@0: def type_name Chris@0: nil Chris@0: end Chris@0: end