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