2011-04-01 17:44:58 +04:00
|
|
|
# Redmine - project management software
|
2012-01-28 15:16:58 +04:00
|
|
|
# Copyright (C) 2006-2012 Jean-Philippe Lang
|
2008-06-28 00:13:56 +04:00
|
|
|
#
|
|
|
|
# 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
|
2008-08-11 22:24:39 +04:00
|
|
|
has_many :custom_values, :as => :customized,
|
|
|
|
:include => :custom_field,
|
|
|
|
:order => "#{CustomField.table_name}.position",
|
2012-01-28 15:16:58 +04:00
|
|
|
:dependent => :delete_all,
|
|
|
|
:validate => false
|
2008-06-28 00:13:56 +04:00
|
|
|
send :include, Redmine::Acts::Customizable::InstanceMethods
|
2012-01-28 15:16:58 +04:00
|
|
|
validate :validate_custom_field_values
|
2008-06-28 00:13:56 +04:00
|
|
|
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
|
|
|
|
|
2010-12-10 13:48:16 +03:00
|
|
|
# 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'}
|
2008-06-28 00:13:56 +04:00
|
|
|
def custom_field_values=(values)
|
|
|
|
values = values.stringify_keys
|
2012-01-28 15:16:58 +04:00
|
|
|
|
|
|
|
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]
|
2012-01-30 00:51:48 +04:00
|
|
|
if value.is_a?(Array)
|
|
|
|
value = value.reject(&:blank?).uniq
|
|
|
|
if value.empty?
|
|
|
|
value << ''
|
|
|
|
end
|
|
|
|
end
|
2012-01-28 15:16:58 +04:00
|
|
|
custom_field_value.value = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@custom_field_values_changed = true
|
2008-06-28 00:13:56 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def custom_field_values
|
2012-01-28 15:16:58 +04:00
|
|
|
@custom_field_values ||= available_custom_fields.collect do |field|
|
|
|
|
x = CustomFieldValue.new
|
|
|
|
x.custom_field = field
|
|
|
|
x.customized = self
|
2012-01-30 00:51:48 +04:00
|
|
|
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
|
2012-01-28 15:16:58 +04:00
|
|
|
x
|
|
|
|
end
|
2008-06-28 00:13:56 +04:00
|
|
|
end
|
|
|
|
|
2010-11-07 17:15:01 +03:00
|
|
|
def visible_custom_field_values
|
|
|
|
custom_field_values.select(&:visible?)
|
|
|
|
end
|
|
|
|
|
2008-06-28 00:13:56 +04:00
|
|
|
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
|
|
|
|
|
2012-01-28 15:16:58 +04:00
|
|
|
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
|
|
|
|
|
2008-06-28 00:13:56 +04:00
|
|
|
def save_custom_field_values
|
2012-01-28 15:16:58 +04:00
|
|
|
target_custom_values = []
|
|
|
|
custom_field_values.each do |custom_field_value|
|
2012-01-30 00:51:48 +04:00
|
|
|
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
|
2012-01-28 15:16:58 +04:00
|
|
|
end
|
|
|
|
self.custom_values = target_custom_values
|
|
|
|
custom_values.each(&:save)
|
2008-06-28 00:13:56 +04:00
|
|
|
@custom_field_values_changed = false
|
2012-01-28 15:16:58 +04:00
|
|
|
true
|
2008-06-28 00:13:56 +04:00
|
|
|
end
|
|
|
|
|
2010-03-13 17:56:49 +03:00
|
|
|
def reset_custom_values!
|
|
|
|
@custom_field_values = nil
|
|
|
|
@custom_field_values_changed = true
|
|
|
|
end
|
|
|
|
|
2008-06-28 00:13:56 +04:00
|
|
|
module ClassMethods
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|