annotate .svn/pristine/b0/b08b8359ba61a19a3531e1766fc73fb2077273a2.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 module Redmine
Chris@1295 19 module Acts
Chris@1295 20 module Customizable
Chris@1295 21 def self.included(base)
Chris@1295 22 base.extend ClassMethods
Chris@1295 23 end
Chris@1295 24
Chris@1295 25 module ClassMethods
Chris@1295 26 def acts_as_customizable(options = {})
Chris@1295 27 return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
Chris@1295 28 cattr_accessor :customizable_options
Chris@1295 29 self.customizable_options = options
Chris@1295 30 has_many :custom_values, :as => :customized,
Chris@1295 31 :include => :custom_field,
Chris@1295 32 :order => "#{CustomField.table_name}.position",
Chris@1295 33 :dependent => :delete_all,
Chris@1295 34 :validate => false
Chris@1295 35 send :include, Redmine::Acts::Customizable::InstanceMethods
Chris@1295 36 validate :validate_custom_field_values
Chris@1295 37 after_save :save_custom_field_values
Chris@1295 38 end
Chris@1295 39 end
Chris@1295 40
Chris@1295 41 module InstanceMethods
Chris@1295 42 def self.included(base)
Chris@1295 43 base.extend ClassMethods
Chris@1295 44 end
Chris@1295 45
Chris@1295 46 def available_custom_fields
Chris@1295 47 CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'",
Chris@1295 48 :order => 'position')
Chris@1295 49 end
Chris@1295 50
Chris@1295 51 # Sets the values of the object's custom fields
Chris@1295 52 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
Chris@1295 53 def custom_fields=(values)
Chris@1295 54 values_to_hash = values.inject({}) do |hash, v|
Chris@1295 55 v = v.stringify_keys
Chris@1295 56 if v['id'] && v.has_key?('value')
Chris@1295 57 hash[v['id']] = v['value']
Chris@1295 58 end
Chris@1295 59 hash
Chris@1295 60 end
Chris@1295 61 self.custom_field_values = values_to_hash
Chris@1295 62 end
Chris@1295 63
Chris@1295 64 # Sets the values of the object's custom fields
Chris@1295 65 # values is a hash like {'1' => 'foo', 2 => 'bar'}
Chris@1295 66 def custom_field_values=(values)
Chris@1295 67 values = values.stringify_keys
Chris@1295 68
Chris@1295 69 custom_field_values.each do |custom_field_value|
Chris@1295 70 key = custom_field_value.custom_field_id.to_s
Chris@1295 71 if values.has_key?(key)
Chris@1295 72 value = values[key]
Chris@1295 73 if value.is_a?(Array)
Chris@1295 74 value = value.reject(&:blank?).uniq
Chris@1295 75 if value.empty?
Chris@1295 76 value << ''
Chris@1295 77 end
Chris@1295 78 end
Chris@1295 79 custom_field_value.value = value
Chris@1295 80 end
Chris@1295 81 end
Chris@1295 82 @custom_field_values_changed = true
Chris@1295 83 end
Chris@1295 84
Chris@1295 85 def custom_field_values
Chris@1295 86 @custom_field_values ||= available_custom_fields.collect do |field|
Chris@1295 87 x = CustomFieldValue.new
Chris@1295 88 x.custom_field = field
Chris@1295 89 x.customized = self
Chris@1295 90 if field.multiple?
Chris@1295 91 values = custom_values.select { |v| v.custom_field == field }
Chris@1295 92 if values.empty?
Chris@1295 93 values << custom_values.build(:customized => self, :custom_field => field, :value => nil)
Chris@1295 94 end
Chris@1295 95 x.value = values.map(&:value)
Chris@1295 96 else
Chris@1295 97 cv = custom_values.detect { |v| v.custom_field == field }
Chris@1295 98 cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil)
Chris@1295 99 x.value = cv.value
Chris@1295 100 end
Chris@1295 101 x
Chris@1295 102 end
Chris@1295 103 end
Chris@1295 104
Chris@1295 105 def visible_custom_field_values
Chris@1295 106 custom_field_values.select(&:visible?)
Chris@1295 107 end
Chris@1295 108
Chris@1295 109 def custom_field_values_changed?
Chris@1295 110 @custom_field_values_changed == true
Chris@1295 111 end
Chris@1295 112
Chris@1295 113 def custom_value_for(c)
Chris@1295 114 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
Chris@1295 115 custom_values.detect {|v| v.custom_field_id == field_id }
Chris@1295 116 end
Chris@1295 117
Chris@1295 118 def custom_field_value(c)
Chris@1295 119 field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
Chris@1295 120 custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
Chris@1295 121 end
Chris@1295 122
Chris@1295 123 def validate_custom_field_values
Chris@1295 124 if new_record? || custom_field_values_changed?
Chris@1295 125 custom_field_values.each(&:validate_value)
Chris@1295 126 end
Chris@1295 127 end
Chris@1295 128
Chris@1295 129 def save_custom_field_values
Chris@1295 130 target_custom_values = []
Chris@1295 131 custom_field_values.each do |custom_field_value|
Chris@1295 132 if custom_field_value.value.is_a?(Array)
Chris@1295 133 custom_field_value.value.each do |v|
Chris@1295 134 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
Chris@1295 135 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
Chris@1295 136 target_custom_values << target
Chris@1295 137 end
Chris@1295 138 else
Chris@1295 139 target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
Chris@1295 140 target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
Chris@1295 141 target.value = custom_field_value.value
Chris@1295 142 target_custom_values << target
Chris@1295 143 end
Chris@1295 144 end
Chris@1295 145 self.custom_values = target_custom_values
Chris@1295 146 custom_values.each(&:save)
Chris@1295 147 @custom_field_values_changed = false
Chris@1295 148 true
Chris@1295 149 end
Chris@1295 150
Chris@1295 151 def reset_custom_values!
Chris@1295 152 @custom_field_values = nil
Chris@1295 153 @custom_field_values_changed = true
Chris@1295 154 end
Chris@1295 155
Chris@1295 156 module ClassMethods
Chris@1295 157 end
Chris@1295 158 end
Chris@1295 159 end
Chris@1295 160 end
Chris@1295 161 end