@@ -1,135 +1,136 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2011 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. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class Enumeration < ActiveRecord::Base |
|
18 | class Enumeration < ActiveRecord::Base | |
19 | default_scope :order => "#{Enumeration.table_name}.position ASC" |
|
19 | default_scope :order => "#{Enumeration.table_name}.position ASC" | |
20 |
|
20 | |||
21 | belongs_to :project |
|
21 | belongs_to :project | |
22 |
|
22 | |||
23 | acts_as_list :scope => 'type = \'#{type}\'' |
|
23 | acts_as_list :scope => 'type = \'#{type}\'' | |
24 | acts_as_customizable |
|
24 | acts_as_customizable | |
25 | acts_as_tree :order => 'position ASC' |
|
25 | acts_as_tree :order => 'position ASC' | |
26 |
|
26 | |||
27 | before_destroy :check_integrity |
|
27 | before_destroy :check_integrity | |
|
28 | before_save :check_default | |||
28 |
|
29 | |||
29 | validates_presence_of :name |
|
30 | validates_presence_of :name | |
30 | validates_uniqueness_of :name, :scope => [:type, :project_id] |
|
31 | validates_uniqueness_of :name, :scope => [:type, :project_id] | |
31 | validates_length_of :name, :maximum => 30 |
|
32 | validates_length_of :name, :maximum => 30 | |
32 |
|
33 | |||
33 | named_scope :shared, :conditions => { :project_id => nil } |
|
34 | named_scope :shared, :conditions => { :project_id => nil } | |
34 | named_scope :active, :conditions => { :active => true } |
|
35 | named_scope :active, :conditions => { :active => true } | |
35 | named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} |
|
36 | named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} | |
36 |
|
37 | |||
37 | def self.default |
|
38 | def self.default | |
38 | # Creates a fake default scope so Enumeration.default will check |
|
39 | # Creates a fake default scope so Enumeration.default will check | |
39 | # it's type. STI subclasses will automatically add their own |
|
40 | # it's type. STI subclasses will automatically add their own | |
40 | # types to the finder. |
|
41 | # types to the finder. | |
41 | if self.descends_from_active_record? |
|
42 | if self.descends_from_active_record? | |
42 | find(:first, :conditions => { :is_default => true, :type => 'Enumeration' }) |
|
43 | find(:first, :conditions => { :is_default => true, :type => 'Enumeration' }) | |
43 | else |
|
44 | else | |
44 | # STI classes are |
|
45 | # STI classes are | |
45 | find(:first, :conditions => { :is_default => true }) |
|
46 | find(:first, :conditions => { :is_default => true }) | |
46 | end |
|
47 | end | |
47 | end |
|
48 | end | |
48 |
|
49 | |||
49 | # Overloaded on concrete classes |
|
50 | # Overloaded on concrete classes | |
50 | def option_name |
|
51 | def option_name | |
51 | nil |
|
52 | nil | |
52 | end |
|
53 | end | |
53 |
|
54 | |||
54 | def before_save |
|
55 | def check_default | |
55 | if is_default? && is_default_changed? |
|
56 | if is_default? && is_default_changed? | |
56 | Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type}) |
|
57 | Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type}) | |
57 | end |
|
58 | end | |
58 | end |
|
59 | end | |
59 |
|
60 | |||
60 | # Overloaded on concrete classes |
|
61 | # Overloaded on concrete classes | |
61 | def objects_count |
|
62 | def objects_count | |
62 | 0 |
|
63 | 0 | |
63 | end |
|
64 | end | |
64 |
|
65 | |||
65 | def in_use? |
|
66 | def in_use? | |
66 | self.objects_count != 0 |
|
67 | self.objects_count != 0 | |
67 | end |
|
68 | end | |
68 |
|
69 | |||
69 | # Is this enumeration overiding a system level enumeration? |
|
70 | # Is this enumeration overiding a system level enumeration? | |
70 | def is_override? |
|
71 | def is_override? | |
71 | !self.parent.nil? |
|
72 | !self.parent.nil? | |
72 | end |
|
73 | end | |
73 |
|
74 | |||
74 | alias :destroy_without_reassign :destroy |
|
75 | alias :destroy_without_reassign :destroy | |
75 |
|
76 | |||
76 | # Destroy the enumeration |
|
77 | # Destroy the enumeration | |
77 | # If a enumeration is specified, objects are reassigned |
|
78 | # If a enumeration is specified, objects are reassigned | |
78 | def destroy(reassign_to = nil) |
|
79 | def destroy(reassign_to = nil) | |
79 | if reassign_to && reassign_to.is_a?(Enumeration) |
|
80 | if reassign_to && reassign_to.is_a?(Enumeration) | |
80 | self.transfer_relations(reassign_to) |
|
81 | self.transfer_relations(reassign_to) | |
81 | end |
|
82 | end | |
82 | destroy_without_reassign |
|
83 | destroy_without_reassign | |
83 | end |
|
84 | end | |
84 |
|
85 | |||
85 | def <=>(enumeration) |
|
86 | def <=>(enumeration) | |
86 | position <=> enumeration.position |
|
87 | position <=> enumeration.position | |
87 | end |
|
88 | end | |
88 |
|
89 | |||
89 | def to_s; name end |
|
90 | def to_s; name end | |
90 |
|
91 | |||
91 | # Returns the Subclasses of Enumeration. Each Subclass needs to be |
|
92 | # Returns the Subclasses of Enumeration. Each Subclass needs to be | |
92 | # required in development mode. |
|
93 | # required in development mode. | |
93 | # |
|
94 | # | |
94 | # Note: subclasses is protected in ActiveRecord |
|
95 | # Note: subclasses is protected in ActiveRecord | |
95 | def self.get_subclasses |
|
96 | def self.get_subclasses | |
96 | @@subclasses[Enumeration] |
|
97 | @@subclasses[Enumeration] | |
97 | end |
|
98 | end | |
98 |
|
99 | |||
99 | # Does the +new+ Hash override the previous Enumeration? |
|
100 | # Does the +new+ Hash override the previous Enumeration? | |
100 | def self.overridding_change?(new, previous) |
|
101 | def self.overridding_change?(new, previous) | |
101 | if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous) |
|
102 | if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous) | |
102 | return false |
|
103 | return false | |
103 | else |
|
104 | else | |
104 | return true |
|
105 | return true | |
105 | end |
|
106 | end | |
106 | end |
|
107 | end | |
107 |
|
108 | |||
108 | # Does the +new+ Hash have the same custom values as the previous Enumeration? |
|
109 | # Does the +new+ Hash have the same custom values as the previous Enumeration? | |
109 | def self.same_custom_values?(new, previous) |
|
110 | def self.same_custom_values?(new, previous) | |
110 | previous.custom_field_values.each do |custom_value| |
|
111 | previous.custom_field_values.each do |custom_value| | |
111 | if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s] |
|
112 | if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s] | |
112 | return false |
|
113 | return false | |
113 | end |
|
114 | end | |
114 | end |
|
115 | end | |
115 |
|
116 | |||
116 | return true |
|
117 | return true | |
117 | end |
|
118 | end | |
118 |
|
119 | |||
119 | # Are the new and previous fields equal? |
|
120 | # Are the new and previous fields equal? | |
120 | def self.same_active_state?(new, previous) |
|
121 | def self.same_active_state?(new, previous) | |
121 | new = (new == "1" ? true : false) |
|
122 | new = (new == "1" ? true : false) | |
122 | return new == previous |
|
123 | return new == previous | |
123 | end |
|
124 | end | |
124 |
|
125 | |||
125 | private |
|
126 | private | |
126 | def check_integrity |
|
127 | def check_integrity | |
127 | raise "Can't delete enumeration" if self.in_use? |
|
128 | raise "Can't delete enumeration" if self.in_use? | |
128 | end |
|
129 | end | |
129 |
|
130 | |||
130 | end |
|
131 | end | |
131 |
|
132 | |||
132 | # Force load the subclasses in development mode |
|
133 | # Force load the subclasses in development mode | |
133 | require_dependency 'time_entry_activity' |
|
134 | require_dependency 'time_entry_activity' | |
134 | require_dependency 'document_category' |
|
135 | require_dependency 'document_category' | |
135 | require_dependency 'issue_priority' |
|
136 | require_dependency 'issue_priority' |
General Comments 0
You need to be logged in to leave comments.
Login now