annotate vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 94944d00e43c
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@0 53 def custom_field_values=(values)
Chris@0 54 @custom_field_values_changed = true
Chris@0 55 values = values.stringify_keys
Chris@0 56 custom_field_values.each do |custom_value|
Chris@0 57 custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s)
Chris@0 58 end if values.is_a?(Hash)
Chris@0 59 end
Chris@0 60
Chris@0 61 def custom_field_values
Chris@0 62 @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 63 end
Chris@0 64
Chris@0 65 def custom_field_values_changed?
Chris@0 66 @custom_field_values_changed == true
Chris@0 67 end
Chris@0 68
Chris@0 69 def custom_value_for(c)
Chris@0 70 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
Chris@0 71 custom_values.detect {|v| v.custom_field_id == field_id }
Chris@0 72 end
Chris@0 73
Chris@0 74 def save_custom_field_values
Chris@0 75 custom_field_values.each(&:save)
Chris@0 76 @custom_field_values_changed = false
Chris@0 77 @custom_field_values = nil
Chris@0 78 end
Chris@0 79
Chris@0 80 def reset_custom_values!
Chris@0 81 @custom_field_values = nil
Chris@0 82 @custom_field_values_changed = true
Chris@0 83 values = custom_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
Chris@0 84 custom_values.each {|cv| cv.destroy unless custom_field_values.include?(cv)}
Chris@0 85 end
Chris@0 86
Chris@0 87 module ClassMethods
Chris@0 88 end
Chris@0 89 end
Chris@0 90 end
Chris@0 91 end
Chris@0 92 end