comparison vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb @ 524:1248a47e81b3 feature_36

Merge from branch "luisf"
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Mon, 25 Jul 2011 14:39:38 +0100
parents cbce1fd3b1b7
children cbb26bc654de
comparison
equal deleted inserted replaced
519:3be6bc3c2a17 524:1248a47e81b3
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
48 def available_custom_fields 48 def available_custom_fields
49 CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'", 49 CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'",
50 :order => 'position') 50 :order => 'position')
51 end 51 end
52 52
53 # Sets the values of the object's custom fields
54 # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
55 def custom_fields=(values)
56 values_to_hash = values.inject({}) do |hash, v|
57 v = v.stringify_keys
58 if v['id'] && v.has_key?('value')
59 hash[v['id']] = v['value']
60 end
61 hash
62 end
63 self.custom_field_values = values_to_hash
64 end
65
66 # Sets the values of the object's custom fields
67 # values is a hash like {'1' => 'foo', 2 => 'bar'}
53 def custom_field_values=(values) 68 def custom_field_values=(values)
54 @custom_field_values_changed = true 69 @custom_field_values_changed = true
55 values = values.stringify_keys 70 values = values.stringify_keys
56 custom_field_values.each do |custom_value| 71 custom_field_values.each do |custom_value|
57 custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s) 72 custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s)
58 end if values.is_a?(Hash) 73 end if values.is_a?(Hash)
74 self.custom_values = custom_field_values
59 end 75 end
60 76
61 def custom_field_values 77 def custom_field_values
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) } 78 @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) }
63 end 79 end
64 80
65 def visible_custom_field_values 81 def visible_custom_field_values
66 custom_field_values.select(&:visible?) 82 custom_field_values.select(&:visible?)
67 end 83 end