comparison .svn/pristine/f4/f43917011f27531ea9643b89a23ab3c1d309c86b.svn-base @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents
children
comparison
equal deleted inserted replaced
1516:b450a9d58aed 1517:dffacf8a6908
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 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?).map(&:to_s).uniq
76 if value.empty?
77 value << ''
78 end
79 else
80 value = value.to_s
81 end
82 custom_field_value.value = value
83 end
84 end
85 @custom_field_values_changed = true
86 end
87
88 def custom_field_values
89 @custom_field_values ||= available_custom_fields.collect do |field|
90 x = CustomFieldValue.new
91 x.custom_field = field
92 x.customized = self
93 if field.multiple?
94 values = custom_values.select { |v| v.custom_field == field }
95 if values.empty?
96 values << custom_values.build(:customized => self, :custom_field => field, :value => nil)
97 end
98 x.value = values.map(&:value)
99 else
100 cv = custom_values.detect { |v| v.custom_field == field }
101 cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil)
102 x.value = cv.value
103 end
104 x.value_was = x.value.dup if x.value
105 x
106 end
107 end
108
109 def visible_custom_field_values
110 custom_field_values.select(&:visible?)
111 end
112
113 def custom_field_values_changed?
114 @custom_field_values_changed == true
115 end
116
117 def custom_value_for(c)
118 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
119 custom_values.detect {|v| v.custom_field_id == field_id }
120 end
121
122 def custom_field_value(c)
123 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
124 custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
125 end
126
127 def validate_custom_field_values
128 if new_record? || custom_field_values_changed?
129 custom_field_values.each(&:validate_value)
130 end
131 end
132
133 def save_custom_field_values
134 target_custom_values = []
135 custom_field_values.each do |custom_field_value|
136 if custom_field_value.value.is_a?(Array)
137 custom_field_value.value.each do |v|
138 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
139 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
140 target_custom_values << target
141 end
142 else
143 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
144 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
145 target.value = custom_field_value.value
146 target_custom_values << target
147 end
148 end
149 self.custom_values = target_custom_values
150 custom_values.each(&:save)
151 @custom_field_values_changed = false
152 true
153 end
154
155 def reset_custom_values!
156 @custom_field_values = nil
157 @custom_field_values_changed = true
158 end
159
160 def reload_with_custom_fields(*args)
161 @custom_field_values = nil
162 @custom_field_values_changed = false
163 reload_without_custom_fields(*args)
164 end
165
166 module ClassMethods
167 end
168 end
169 end
170 end
171 end