##// END OF EJS Templates
Removes a call to alias_method_chain....
Jean-Philippe Lang -
r15271:e58e676b7c15
parent child
Show More
@@ -1,178 +1,177
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module Acts
20 20 module Customizable
21 21 def self.included(base)
22 22 base.extend ClassMethods
23 23 end
24 24
25 25 module ClassMethods
26 26 def acts_as_customizable(options = {})
27 27 return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
28 28 cattr_accessor :customizable_options
29 29 self.customizable_options = options
30 30 has_many :custom_values, lambda {includes(:custom_field).order("#{CustomField.table_name}.position")},
31 31 :as => :customized,
32 32 :dependent => :delete_all,
33 33 :validate => false
34 34
35 35 send :include, Redmine::Acts::Customizable::InstanceMethods
36 36 validate :validate_custom_field_values
37 37 after_save :save_custom_field_values
38 38 end
39 39 end
40 40
41 41 module InstanceMethods
42 42 def self.included(base)
43 43 base.extend ClassMethods
44 base.send :alias_method_chain, :reload, :custom_fields
45 44 end
46 45
47 46 def available_custom_fields
48 47 CustomField.where("type = '#{self.class.name}CustomField'").sorted.to_a
49 48 end
50 49
51 50 # Sets the values of the object's custom fields
52 51 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
53 52 def custom_fields=(values)
54 53 values_to_hash = values.inject({}) do |hash, v|
55 54 v = v.stringify_keys
56 55 if v['id'] && v.has_key?('value')
57 56 hash[v['id']] = v['value']
58 57 end
59 58 hash
60 59 end
61 60 self.custom_field_values = values_to_hash
62 61 end
63 62
64 63 # Sets the values of the object's custom fields
65 64 # values is a hash like {'1' => 'foo', 2 => 'bar'}
66 65 def custom_field_values=(values)
67 66 values = values.stringify_keys
68 67
69 68 custom_field_values.each do |custom_field_value|
70 69 key = custom_field_value.custom_field_id.to_s
71 70 if values.has_key?(key)
72 71 value = values[key]
73 72 if value.is_a?(Array)
74 73 value = value.reject(&:blank?).map(&:to_s).uniq
75 74 if value.empty?
76 75 value << ''
77 76 end
78 77 else
79 78 value = value.to_s
80 79 end
81 80 custom_field_value.value = value
82 81 end
83 82 end
84 83 @custom_field_values_changed = true
85 84 end
86 85
87 86 def custom_field_values
88 87 @custom_field_values ||= available_custom_fields.collect do |field|
89 88 x = CustomFieldValue.new
90 89 x.custom_field = field
91 90 x.customized = self
92 91 if field.multiple?
93 92 values = custom_values.select { |v| v.custom_field == field }
94 93 if values.empty?
95 94 values << custom_values.build(:customized => self, :custom_field => field)
96 95 end
97 96 x.value = values.map(&:value)
98 97 else
99 98 cv = custom_values.detect { |v| v.custom_field == field }
100 99 cv ||= custom_values.build(:customized => self, :custom_field => field)
101 100 x.value = cv.value
102 101 end
103 102 x.value_was = x.value.dup if x.value
104 103 x
105 104 end
106 105 end
107 106
108 107 def visible_custom_field_values
109 108 custom_field_values.select(&:visible?)
110 109 end
111 110
112 111 def custom_field_values_changed?
113 112 @custom_field_values_changed == true
114 113 end
115 114
116 115 def custom_value_for(c)
117 116 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
118 117 custom_values.detect {|v| v.custom_field_id == field_id }
119 118 end
120 119
121 120 def custom_field_value(c)
122 121 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
123 122 custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
124 123 end
125 124
126 125 def validate_custom_field_values
127 126 if new_record? || custom_field_values_changed?
128 127 custom_field_values.each(&:validate_value)
129 128 end
130 129 end
131 130
132 131 def save_custom_field_values
133 132 target_custom_values = []
134 133 custom_field_values.each do |custom_field_value|
135 134 if custom_field_value.value.is_a?(Array)
136 135 custom_field_value.value.each do |v|
137 136 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
138 137 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
139 138 target_custom_values << target
140 139 end
141 140 else
142 141 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
143 142 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
144 143 target.value = custom_field_value.value
145 144 target_custom_values << target
146 145 end
147 146 end
148 147 self.custom_values = target_custom_values
149 148 custom_values.each(&:save)
150 149 @custom_field_values_changed = false
151 150 true
152 151 end
153 152
154 153 def reassign_custom_field_values
155 154 if @custom_field_values
156 155 values = @custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
157 156 @custom_field_values = nil
158 157 self.custom_field_values = values
159 158 end
160 159 end
161 160
162 161 def reset_custom_values!
163 162 @custom_field_values = nil
164 163 @custom_field_values_changed = true
165 164 end
166 165
167 def reload_with_custom_fields(*args)
166 def reload(*args)
168 167 @custom_field_values = nil
169 168 @custom_field_values_changed = false
170 reload_without_custom_fields(*args)
169 super
171 170 end
172 171
173 172 module ClassMethods
174 173 end
175 174 end
176 175 end
177 176 end
178 177 end
General Comments 0
You need to be logged in to leave comments. Login now