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