To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / plugins / acts_as_customizable / lib / .svn / text-base / acts_as_customizable.rb.svn-base @ 442:753f1380d6bc
History | View | Annotate | Download (4.38 KB)
| 1 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software |
|---|---|---|---|
| 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
||
| 3 | 0:513646585e45 | Chris | # |
| 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_on_create { |customized| customized.custom_field_values }
|
||
| 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 | 119:8661b858af72 | Chris | # 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 | 0:513646585e45 | Chris | 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 | 245:051f544170fe | Chris | self.custom_values = custom_field_values |
| 75 | 0:513646585e45 | Chris | end |
| 76 | |||
| 77 | def custom_field_values |
||
| 78 | 441:cbce1fd3b1b7 | Chris | @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) }
|
| 79 | 0:513646585e45 | Chris | end |
| 80 | |||
| 81 | 37:94944d00e43c | chris | def visible_custom_field_values |
| 82 | custom_field_values.select(&:visible?) |
||
| 83 | end |
||
| 84 | |||
| 85 | 0:513646585e45 | Chris | def custom_field_values_changed? |
| 86 | @custom_field_values_changed == true |
||
| 87 | end |
||
| 88 | |||
| 89 | def custom_value_for(c) |
||
| 90 | field_id = (c.is_a?(CustomField) ? c.id : c.to_i) |
||
| 91 | custom_values.detect {|v| v.custom_field_id == field_id }
|
||
| 92 | end |
||
| 93 | |||
| 94 | def save_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 |