##// END OF EJS Templates
Set inverse on custom_values association....
Jean-Philippe Lang -
r15838:2c70d9e9886e
parent child
Show More
@@ -1,168 +1,169
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 :inverse_of => :customized,
32 33 :dependent => :delete_all,
33 34 :validate => false
34 35
35 36 send :include, Redmine::Acts::Customizable::InstanceMethods
36 37 validate :validate_custom_field_values
37 38 after_save :save_custom_field_values
38 39 end
39 40 end
40 41
41 42 module InstanceMethods
42 43 def self.included(base)
43 44 base.extend ClassMethods
44 45 end
45 46
46 47 def available_custom_fields
47 48 CustomField.where("type = '#{self.class.name}CustomField'").sorted.to_a
48 49 end
49 50
50 51 # Sets the values of the object's custom fields
51 52 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
52 53 def custom_fields=(values)
53 54 values_to_hash = values.inject({}) do |hash, v|
54 55 v = v.stringify_keys
55 56 if v['id'] && v.has_key?('value')
56 57 hash[v['id']] = v['value']
57 58 end
58 59 hash
59 60 end
60 61 self.custom_field_values = values_to_hash
61 62 end
62 63
63 64 # Sets the values of the object's custom fields
64 65 # values is a hash like {'1' => 'foo', 2 => 'bar'}
65 66 def custom_field_values=(values)
66 67 values = values.stringify_keys
67 68
68 69 custom_field_values.each do |custom_field_value|
69 70 key = custom_field_value.custom_field_id.to_s
70 71 if values.has_key?(key)
71 72 custom_field_value.value = values[key]
72 73 end
73 74 end
74 75 @custom_field_values_changed = true
75 76 end
76 77
77 78 def custom_field_values
78 79 @custom_field_values ||= available_custom_fields.collect do |field|
79 80 x = CustomFieldValue.new
80 81 x.custom_field = field
81 82 x.customized = self
82 83 if field.multiple?
83 84 values = custom_values.select { |v| v.custom_field == field }
84 85 if values.empty?
85 86 values << custom_values.build(:customized => self, :custom_field => field)
86 87 end
87 88 x.instance_variable_set("@value", values.map(&:value))
88 89 else
89 90 cv = custom_values.detect { |v| v.custom_field == field }
90 91 cv ||= custom_values.build(:customized => self, :custom_field => field)
91 92 x.instance_variable_set("@value", cv.value)
92 93 end
93 94 x.value_was = x.value.dup if x.value
94 95 x
95 96 end
96 97 end
97 98
98 99 def visible_custom_field_values
99 100 custom_field_values.select(&:visible?)
100 101 end
101 102
102 103 def custom_field_values_changed?
103 104 @custom_field_values_changed == true
104 105 end
105 106
106 107 def custom_value_for(c)
107 108 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
108 109 custom_values.detect {|v| v.custom_field_id == field_id }
109 110 end
110 111
111 112 def custom_field_value(c)
112 113 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
113 114 custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
114 115 end
115 116
116 117 def validate_custom_field_values
117 118 if new_record? || custom_field_values_changed?
118 119 custom_field_values.each(&:validate_value)
119 120 end
120 121 end
121 122
122 123 def save_custom_field_values
123 124 target_custom_values = []
124 125 custom_field_values.each do |custom_field_value|
125 126 if custom_field_value.value.is_a?(Array)
126 127 custom_field_value.value.each do |v|
127 128 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
128 129 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
129 130 target_custom_values << target
130 131 end
131 132 else
132 133 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
133 134 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
134 135 target.value = custom_field_value.value
135 136 target_custom_values << target
136 137 end
137 138 end
138 139 self.custom_values = target_custom_values
139 140 custom_values.each(&:save)
140 141 @custom_field_values_changed = false
141 142 true
142 143 end
143 144
144 145 def reassign_custom_field_values
145 146 if @custom_field_values
146 147 values = @custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
147 148 @custom_field_values = nil
148 149 self.custom_field_values = values
149 150 end
150 151 end
151 152
152 153 def reset_custom_values!
153 154 @custom_field_values = nil
154 155 @custom_field_values_changed = true
155 156 end
156 157
157 158 def reload(*args)
158 159 @custom_field_values = nil
159 160 @custom_field_values_changed = false
160 161 super
161 162 end
162 163
163 164 module ClassMethods
164 165 end
165 166 end
166 167 end
167 168 end
168 169 end
General Comments 0
You need to be logged in to leave comments. Login now