##// END OF EJS Templates
remove trailing white-spaces from vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb...
Toshi MARUYAMA -
r9169:dd5c702e110e
parent child
Show More
@@ -1,161 +1,161
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 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, :as => :customized,
30 has_many :custom_values, :as => :customized,
31 :include => :custom_field,
31 :include => :custom_field,
32 :order => "#{CustomField.table_name}.position",
32 :order => "#{CustomField.table_name}.position",
33 :dependent => :delete_all,
33 :dependent => :delete_all,
34 :validate => false
34 :validate => false
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 end
44 end
45
45
46 def available_custom_fields
46 def available_custom_fields
47 CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'",
47 CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'",
48 :order => 'position')
48 :order => 'position')
49 end
49 end
50
50
51 # Sets the values of the object's custom fields
51 # Sets the values of the object's custom fields
52 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
52 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
53 def custom_fields=(values)
53 def custom_fields=(values)
54 values_to_hash = values.inject({}) do |hash, v|
54 values_to_hash = values.inject({}) do |hash, v|
55 v = v.stringify_keys
55 v = v.stringify_keys
56 if v['id'] && v.has_key?('value')
56 if v['id'] && v.has_key?('value')
57 hash[v['id']] = v['value']
57 hash[v['id']] = v['value']
58 end
58 end
59 hash
59 hash
60 end
60 end
61 self.custom_field_values = values_to_hash
61 self.custom_field_values = values_to_hash
62 end
62 end
63
63
64 # Sets the values of the object's custom fields
64 # Sets the values of the object's custom fields
65 # values is a hash like {'1' => 'foo', 2 => 'bar'}
65 # values is a hash like {'1' => 'foo', 2 => 'bar'}
66 def custom_field_values=(values)
66 def custom_field_values=(values)
67 values = values.stringify_keys
67 values = values.stringify_keys
68
68
69 custom_field_values.each do |custom_field_value|
69 custom_field_values.each do |custom_field_value|
70 key = custom_field_value.custom_field_id.to_s
70 key = custom_field_value.custom_field_id.to_s
71 if values.has_key?(key)
71 if values.has_key?(key)
72 value = values[key]
72 value = values[key]
73 if value.is_a?(Array)
73 if value.is_a?(Array)
74 value = value.reject(&:blank?).uniq
74 value = value.reject(&:blank?).uniq
75 if value.empty?
75 if value.empty?
76 value << ''
76 value << ''
77 end
77 end
78 end
78 end
79 custom_field_value.value = value
79 custom_field_value.value = value
80 end
80 end
81 end
81 end
82 @custom_field_values_changed = true
82 @custom_field_values_changed = true
83 end
83 end
84
84
85 def custom_field_values
85 def custom_field_values
86 @custom_field_values ||= available_custom_fields.collect do |field|
86 @custom_field_values ||= available_custom_fields.collect do |field|
87 x = CustomFieldValue.new
87 x = CustomFieldValue.new
88 x.custom_field = field
88 x.custom_field = field
89 x.customized = self
89 x.customized = self
90 if field.multiple?
90 if field.multiple?
91 values = custom_values.select { |v| v.custom_field == field }
91 values = custom_values.select { |v| v.custom_field == field }
92 if values.empty?
92 if values.empty?
93 values << custom_values.build(:customized => self, :custom_field => field, :value => nil)
93 values << custom_values.build(:customized => self, :custom_field => field, :value => nil)
94 end
94 end
95 x.value = values.map(&:value)
95 x.value = values.map(&:value)
96 else
96 else
97 cv = custom_values.detect { |v| v.custom_field == field }
97 cv = custom_values.detect { |v| v.custom_field == field }
98 cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil)
98 cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil)
99 x.value = cv.value
99 x.value = cv.value
100 end
100 end
101 x
101 x
102 end
102 end
103 end
103 end
104
104
105 def visible_custom_field_values
105 def visible_custom_field_values
106 custom_field_values.select(&:visible?)
106 custom_field_values.select(&:visible?)
107 end
107 end
108
108
109 def custom_field_values_changed?
109 def custom_field_values_changed?
110 @custom_field_values_changed == true
110 @custom_field_values_changed == true
111 end
111 end
112
112
113 def custom_value_for(c)
113 def custom_value_for(c)
114 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
114 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
115 custom_values.detect {|v| v.custom_field_id == field_id }
115 custom_values.detect {|v| v.custom_field_id == field_id }
116 end
116 end
117
117
118 def custom_field_value(c)
118 def custom_field_value(c)
119 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
119 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
120 custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
120 custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
121 end
121 end
122
122
123 def validate_custom_field_values
123 def validate_custom_field_values
124 if new_record? || custom_field_values_changed?
124 if new_record? || custom_field_values_changed?
125 custom_field_values.each(&:validate_value)
125 custom_field_values.each(&:validate_value)
126 end
126 end
127 end
127 end
128
128
129 def save_custom_field_values
129 def save_custom_field_values
130 target_custom_values = []
130 target_custom_values = []
131 custom_field_values.each do |custom_field_value|
131 custom_field_values.each do |custom_field_value|
132 if custom_field_value.value.is_a?(Array)
132 if custom_field_value.value.is_a?(Array)
133 custom_field_value.value.each do |v|
133 custom_field_value.value.each do |v|
134 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
134 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
135 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
135 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
136 target_custom_values << target
136 target_custom_values << target
137 end
137 end
138 else
138 else
139 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
139 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
140 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
140 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
141 target.value = custom_field_value.value
141 target.value = custom_field_value.value
142 target_custom_values << target
142 target_custom_values << target
143 end
143 end
144 end
144 end
145 self.custom_values = target_custom_values
145 self.custom_values = target_custom_values
146 custom_values.each(&:save)
146 custom_values.each(&:save)
147 @custom_field_values_changed = false
147 @custom_field_values_changed = false
148 true
148 true
149 end
149 end
150
150
151 def reset_custom_values!
151 def reset_custom_values!
152 @custom_field_values = nil
152 @custom_field_values = nil
153 @custom_field_values_changed = true
153 @custom_field_values_changed = true
154 end
154 end
155
155
156 module ClassMethods
156 module ClassMethods
157 end
157 end
158 end
158 end
159 end
159 end
160 end
160 end
161 end
161 end
General Comments 0
You need to be logged in to leave comments. Login now