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