comparison .svn/pristine/95/954503d8b00c9c216119d7e8717102d3c9f6c727.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 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 :validate => false
35
36 send :include, Redmine::Acts::Customizable::InstanceMethods
37 validate :validate_custom_field_values
38 after_save :save_custom_field_values
39 end
40 end
41
42 module InstanceMethods
43 def self.included(base)
44 base.extend ClassMethods
45 base.send :alias_method_chain, :reload, :custom_fields
46 end
47
48 def available_custom_fields
49 CustomField.where("type = '#{self.class.name}CustomField'").sorted.all
50 end
51
52 # Sets the values of the object's custom fields
53 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
54 def custom_fields=(values)
55 values_to_hash = values.inject({}) do |hash, v|
56 v = v.stringify_keys
57 if v['id'] && v.has_key?('value')
58 hash[v['id']] = v['value']
59 end
60 hash
61 end
62 self.custom_field_values = values_to_hash
63 end
64
65 # Sets the values of the object's custom fields
66 # values is a hash like {'1' => 'foo', 2 => 'bar'}
67 def custom_field_values=(values)
68 values = values.stringify_keys
69
70 custom_field_values.each do |custom_field_value|
71 key = custom_field_value.custom_field_id.to_s
72 if values.has_key?(key)
73 value = values[key]
74 if value.is_a?(Array)
75 value = value.reject(&:blank?).uniq
76 if value.empty?
77 value << ''
78 end
79 end
80 custom_field_value.value = value
81 end
82 end
83 @custom_field_values_changed = true
84 end
85
86 def custom_field_values
87 @custom_field_values ||= available_custom_fields.collect do |field|
88 x = CustomFieldValue.new
89 x.custom_field = field
90 x.customized = self
91 if field.multiple?
92 values = custom_values.select { |v| v.custom_field == field }
93 if values.empty?
94 values << custom_values.build(:customized => self, :custom_field => field, :value => nil)
95 end
96 x.value = values.map(&:value)
97 else
98 cv = custom_values.detect { |v| v.custom_field == field }
99 cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil)
100 x.value = cv.value
101 end
102 x
103 end
104 end
105
106 def visible_custom_field_values
107 custom_field_values.select(&:visible?)
108 end
109
110 def custom_field_values_changed?
111 @custom_field_values_changed == true
112 end
113
114 def custom_value_for(c)
115 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
116 custom_values.detect {|v| v.custom_field_id == field_id }
117 end
118
119 def custom_field_value(c)
120 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
121 custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
122 end
123
124 def validate_custom_field_values
125 if new_record? || custom_field_values_changed?
126 custom_field_values.each(&:validate_value)
127 end
128 end
129
130 def save_custom_field_values
131 target_custom_values = []
132 custom_field_values.each do |custom_field_value|
133 if custom_field_value.value.is_a?(Array)
134 custom_field_value.value.each do |v|
135 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
136 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
137 target_custom_values << target
138 end
139 else
140 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
141 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
142 target.value = custom_field_value.value
143 target_custom_values << target
144 end
145 end
146 self.custom_values = target_custom_values
147 custom_values.each(&:save)
148 @custom_field_values_changed = false
149 true
150 end
151
152 def reset_custom_values!
153 @custom_field_values = nil
154 @custom_field_values_changed = true
155 end
156
157 def reload_with_custom_fields(*args)
158 @custom_field_values = nil
159 @custom_field_values_changed = false
160 reload_without_custom_fields(*args)
161 end
162
163 module ClassMethods
164 end
165 end
166 end
167 end
168 end