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