annotate vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb @ 904:0a8317a50fa0 redmine-1.1

Close obsolete branch redmine-1.1
author Chris Cannam
date Fri, 14 Jan 2011 12:53:21 +0000
parents af80e5618e9b
children 051f544170fe
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2008 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@0 34 before_validation_on_create { |customized| customized.custom_field_values }
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@117 53 # Sets the values of the object's custom fields
Chris@117 54 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
Chris@117 55 def custom_fields=(values)
Chris@117 56 values_to_hash = values.inject({}) do |hash, v|
Chris@117 57 v = v.stringify_keys
Chris@117 58 if v['id'] && v.has_key?('value')
Chris@117 59 hash[v['id']] = v['value']
Chris@117 60 end
Chris@117 61 hash
Chris@117 62 end
Chris@117 63 self.custom_field_values = values_to_hash
Chris@117 64 end
Chris@117 65
Chris@117 66 # Sets the values of the object's custom fields
Chris@117 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@0 77 @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 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@0 94 custom_field_values.each(&:save)
Chris@0 95 @custom_field_values_changed = false
Chris@0 96 @custom_field_values = nil
Chris@0 97 end
Chris@0 98
Chris@0 99 def reset_custom_values!
Chris@0 100 @custom_field_values = nil
Chris@0 101 @custom_field_values_changed = true
Chris@0 102 values = custom_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
Chris@0 103 custom_values.each {|cv| cv.destroy unless custom_field_values.include?(cv)}
Chris@0 104 end
Chris@0 105
Chris@0 106 module ClassMethods
Chris@0 107 end
Chris@0 108 end
Chris@0 109 end
Chris@0 110 end
Chris@0 111 end