annotate vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb @ 929:5f33065ddc4b redmine-1.3

Update to Redmine SVN rev 9414 on 1.3-stable branch
author Chris Cannam
date Wed, 27 Jun 2012 14:54:18 +0100
parents cbb26bc654de
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 module Redmine
Chris@0 19 module Acts
Chris@0 20 module Customizable
Chris@0 21 def self.included(base)
Chris@0 22 base.extend ClassMethods
Chris@0 23 end
Chris@0 24
Chris@0 25 module ClassMethods
Chris@0 26 def acts_as_customizable(options = {})
Chris@0 27 return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
Chris@0 28 cattr_accessor :customizable_options
Chris@0 29 self.customizable_options = options
Chris@0 30 has_many :custom_values, :as => :customized,
Chris@0 31 :include => :custom_field,
Chris@0 32 :order => "#{CustomField.table_name}.position",
Chris@0 33 :dependent => :delete_all
Chris@909 34 before_validation { |customized| customized.custom_field_values if customized.new_record? }
Chris@0 35 # Trigger validation only if custom values were changed
Chris@0 36 validates_associated :custom_values, :on => :update, :if => Proc.new { |customized| customized.custom_field_values_changed? }
Chris@0 37 send :include, Redmine::Acts::Customizable::InstanceMethods
Chris@0 38 # Save custom values when saving the customized object
Chris@0 39 after_save :save_custom_field_values
Chris@0 40 end
Chris@0 41 end
Chris@0 42
Chris@0 43 module InstanceMethods
Chris@0 44 def self.included(base)
Chris@0 45 base.extend ClassMethods
Chris@0 46 end
Chris@0 47
Chris@0 48 def available_custom_fields
Chris@0 49 CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'",
Chris@0 50 :order => 'position')
Chris@0 51 end
Chris@0 52
Chris@119 53 # Sets the values of the object's custom fields
Chris@119 54 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
Chris@119 55 def custom_fields=(values)
Chris@119 56 values_to_hash = values.inject({}) do |hash, v|
Chris@119 57 v = v.stringify_keys
Chris@119 58 if v['id'] && v.has_key?('value')
Chris@119 59 hash[v['id']] = v['value']
Chris@119 60 end
Chris@119 61 hash
Chris@119 62 end
Chris@119 63 self.custom_field_values = values_to_hash
Chris@119 64 end
Chris@119 65
Chris@119 66 # Sets the values of the object's custom fields
Chris@119 67 # values is a hash like {'1' => 'foo', 2 => 'bar'}
Chris@0 68 def custom_field_values=(values)
Chris@0 69 @custom_field_values_changed = true
Chris@0 70 values = values.stringify_keys
Chris@0 71 custom_field_values.each do |custom_value|
Chris@0 72 custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s)
Chris@0 73 end if values.is_a?(Hash)
Chris@0 74 end
Chris@0 75
Chris@0 76 def custom_field_values
Chris@441 77 @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@0 78 end
Chris@0 79
chris@37 80 def visible_custom_field_values
chris@37 81 custom_field_values.select(&:visible?)
chris@37 82 end
chris@37 83
Chris@0 84 def custom_field_values_changed?
Chris@0 85 @custom_field_values_changed == true
Chris@0 86 end
Chris@0 87
Chris@0 88 def custom_value_for(c)
Chris@0 89 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
Chris@0 90 custom_values.detect {|v| v.custom_field_id == field_id }
Chris@0 91 end
Chris@0 92
Chris@0 93 def save_custom_field_values
Chris@909 94 self.custom_values = custom_field_values
Chris@0 95 custom_field_values.each(&:save)
Chris@0 96 @custom_field_values_changed = false
Chris@0 97 @custom_field_values = nil
Chris@0 98 end
Chris@0 99
Chris@0 100 def reset_custom_values!
Chris@0 101 @custom_field_values = nil
Chris@0 102 @custom_field_values_changed = true
Chris@0 103 values = custom_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
Chris@0 104 custom_values.each {|cv| cv.destroy unless custom_field_values.include?(cv)}
Chris@0 105 end
Chris@0 106
Chris@0 107 module ClassMethods
Chris@0 108 end
Chris@0 109 end
Chris@0 110 end
Chris@0 111 end
Chris@0 112 end