To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 34 / 3426569be9b65b6dce3f0afa56586bb79fc07741.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (4.4 KB)
| 1 |
# Redmine - project management software |
|---|---|
| 2 |
# Copyright (C) 2006-2011 Jean-Philippe Lang |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU General Public License |
| 6 |
# as published by the Free Software Foundation; either version 2 |
| 7 |
# of the License, or (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
|
| 18 |
module Redmine |
| 19 |
module Acts |
| 20 |
module Customizable |
| 21 |
def self.included(base) |
| 22 |
base.extend ClassMethods |
| 23 |
end |
| 24 |
|
| 25 |
module ClassMethods |
| 26 |
def acts_as_customizable(options = {})
|
| 27 |
return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods) |
| 28 |
cattr_accessor :customizable_options |
| 29 |
self.customizable_options = options |
| 30 |
has_many :custom_values, :as => :customized, |
| 31 |
:include => :custom_field, |
| 32 |
:order => "#{CustomField.table_name}.position",
|
| 33 |
:dependent => :delete_all |
| 34 |
before_validation { |customized| customized.custom_field_values if customized.new_record? }
|
| 35 |
# Trigger validation only if custom values were changed |
| 36 |
validates_associated :custom_values, :on => :update, :if => Proc.new { |customized| customized.custom_field_values_changed? }
|
| 37 |
send :include, Redmine::Acts::Customizable::InstanceMethods |
| 38 |
# Save custom values when saving the customized object |
| 39 |
after_save :save_custom_field_values |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
module InstanceMethods |
| 44 |
def self.included(base) |
| 45 |
base.extend ClassMethods |
| 46 |
end |
| 47 |
|
| 48 |
def available_custom_fields |
| 49 |
CustomField.find(:all, :conditions => "type = '#{self.class.name}CustomField'",
|
| 50 |
:order => 'position') |
| 51 |
end |
| 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'}
|
| 68 |
def custom_field_values=(values) |
| 69 |
@custom_field_values_changed = true |
| 70 |
values = values.stringify_keys |
| 71 |
custom_field_values.each do |custom_value| |
| 72 |
custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s) |
| 73 |
end if values.is_a?(Hash) |
| 74 |
end |
| 75 |
|
| 76 |
def custom_field_values |
| 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) }
|
| 78 |
end |
| 79 |
|
| 80 |
def visible_custom_field_values |
| 81 |
custom_field_values.select(&:visible?) |
| 82 |
end |
| 83 |
|
| 84 |
def custom_field_values_changed? |
| 85 |
@custom_field_values_changed == true |
| 86 |
end |
| 87 |
|
| 88 |
def custom_value_for(c) |
| 89 |
field_id = (c.is_a?(CustomField) ? c.id : c.to_i) |
| 90 |
custom_values.detect {|v| v.custom_field_id == field_id }
|
| 91 |
end |
| 92 |
|
| 93 |
def save_custom_field_values |
| 94 |
self.custom_values = custom_field_values |
| 95 |
custom_field_values.each(&:save) |
| 96 |
@custom_field_values_changed = false |
| 97 |
@custom_field_values = nil |
| 98 |
end |
| 99 |
|
| 100 |
def reset_custom_values! |
| 101 |
@custom_field_values = nil |
| 102 |
@custom_field_values_changed = true |
| 103 |
values = custom_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
|
| 104 |
custom_values.each {|cv| cv.destroy unless custom_field_values.include?(cv)}
|
| 105 |
end |
| 106 |
|
| 107 |
module ClassMethods |
| 108 |
end |
| 109 |
end |
| 110 |
end |
| 111 |
end |
| 112 |
end |