OSDN Git Service

a5598dbd262f795ddf52327643f560d222df879a
[redminele/redminele.git] / redmine / vendor / plugins / acts_as_customizable / lib / acts_as_customizable.rb
1 # redMine - project management software
2 # Copyright (C) 2006-2008  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
18 module Redmine
19   module Acts
20     module Customizable
21       def self.included(base)
22         base.extend ClassMethods
23       end
24
25       module 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
40         end
41       end
42
43       module InstanceMethods
44         def self.included(base)
45           base.extend ClassMethods
46         end
47         
48         def available_custom_fields
49           CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'",
50                                  :order => 'position')
51         end
52         
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)
59         end
60         
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) }
63         end
64         
65         def custom_field_values_changed?
66           @custom_field_values_changed == true
67         end
68         
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 }
72         end
73         
74         def save_custom_field_values
75           custom_field_values.each(&:save)
76           @custom_field_values_changed = false
77           @custom_field_values = nil
78         end
79         
80         module ClassMethods
81         end
82       end
83     end
84   end
85 end