Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: module Redmine Chris@909: module Acts Chris@909: module Customizable Chris@909: def self.included(base) Chris@909: base.extend ClassMethods Chris@909: end Chris@909: Chris@909: module ClassMethods Chris@909: def acts_as_customizable(options = {}) Chris@909: return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods) Chris@909: cattr_accessor :customizable_options Chris@909: self.customizable_options = options Chris@909: has_many :custom_values, :as => :customized, Chris@909: :include => :custom_field, Chris@909: :order => "#{CustomField.table_name}.position", Chris@909: :dependent => :delete_all Chris@909: before_validation { |customized| customized.custom_field_values if customized.new_record? } Chris@909: # Trigger validation only if custom values were changed Chris@909: validates_associated :custom_values, :on => :update, :if => Proc.new { |customized| customized.custom_field_values_changed? } Chris@909: send :include, Redmine::Acts::Customizable::InstanceMethods Chris@909: # Save custom values when saving the customized object Chris@909: after_save :save_custom_field_values Chris@909: end Chris@909: end Chris@909: Chris@909: module InstanceMethods Chris@909: def self.included(base) Chris@909: base.extend ClassMethods Chris@909: end Chris@909: Chris@909: def available_custom_fields Chris@909: CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'", Chris@909: :order => 'position') Chris@909: end Chris@909: Chris@909: # Sets the values of the object's custom fields Chris@909: # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}] Chris@909: def custom_fields=(values) Chris@909: values_to_hash = values.inject({}) do |hash, v| Chris@909: v = v.stringify_keys Chris@909: if v['id'] && v.has_key?('value') Chris@909: hash[v['id']] = v['value'] Chris@909: end Chris@909: hash Chris@909: end Chris@909: self.custom_field_values = values_to_hash Chris@909: end Chris@909: Chris@909: # Sets the values of the object's custom fields Chris@909: # values is a hash like {'1' => 'foo', 2 => 'bar'} Chris@909: def custom_field_values=(values) Chris@909: @custom_field_values_changed = true Chris@909: values = values.stringify_keys Chris@909: custom_field_values.each do |custom_value| Chris@909: custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s) Chris@909: end if values.is_a?(Hash) Chris@909: end Chris@909: Chris@909: def custom_field_values Chris@909: @custom_field_values ||= available_custom_fields.collect { |x| custom_values.detect { |v| v.custom_field == x } || custom_values.build(:customized => self, :custom_field => x, :value => nil) } Chris@909: end Chris@909: Chris@909: def visible_custom_field_values Chris@909: custom_field_values.select(&:visible?) Chris@909: end Chris@909: Chris@909: def custom_field_values_changed? Chris@909: @custom_field_values_changed == true Chris@909: end Chris@909: Chris@909: def custom_value_for(c) Chris@909: field_id = (c.is_a?(CustomField) ? c.id : c.to_i) Chris@909: custom_values.detect {|v| v.custom_field_id == field_id } Chris@909: end Chris@909: Chris@909: def save_custom_field_values Chris@909: self.custom_values = custom_field_values Chris@909: custom_field_values.each(&:save) Chris@909: @custom_field_values_changed = false Chris@909: @custom_field_values = nil Chris@909: end Chris@909: Chris@909: def reset_custom_values! Chris@909: @custom_field_values = nil Chris@909: @custom_field_values_changed = true Chris@909: values = custom_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} Chris@909: custom_values.each {|cv| cv.destroy unless custom_field_values.include?(cv)} Chris@909: end Chris@909: Chris@909: module ClassMethods Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end