1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
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.
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.
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.
21 def self.included(base)
22 base.extend ClassMethods
26 def acts_as_customizable(options = {})
27 return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
28 cattr_accessor :customizable_options
29 self.customizable_options = options
30 has_many :custom_values, :as => :customized,
31 :include => :custom_field,
32 :order => "#{CustomField.table_name}.position",
33 :dependent => :delete_all
34 before_validation_on_create { |customized| customized.custom_field_values }
35 # Trigger validation only if custom values were changed
36 validates_associated :custom_values, :on => :update, :if => Proc.new { |customized| customized.custom_field_values_changed? }
37 send :include, Redmine::Acts::Customizable::InstanceMethods
38 # Save custom values when saving the customized object
39 after_save :save_custom_field_values
43 module InstanceMethods
44 def self.included(base)
45 base.extend ClassMethods
48 def available_custom_fields
49 CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'",
53 def custom_field_values=(values)
54 @custom_field_values_changed = true
55 values = values.stringify_keys
56 custom_field_values.each do |custom_value|
57 custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s)
58 end if values.is_a?(Hash)
61 def custom_field_values
62 @custom_field_values ||= available_custom_fields.collect { |x| custom_values.detect { |v| v.custom_field == x } || custom_values.build(:custom_field => x, :value => nil) }
65 def custom_field_values_changed?
66 @custom_field_values_changed == true
69 def custom_value_for(c)
70 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
71 custom_values.detect {|v| v.custom_field_id == field_id }
74 def save_custom_field_values
75 custom_field_values.each(&:save)
76 @custom_field_values_changed = false
77 @custom_field_values = nil