annotate .svn/pristine/34/3426569be9b65b6dce3f0afa56586bb79fc07741.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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