@@ -1,168 +1,173 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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 | include Redmine::SubclassFactory |
|
19 | include Redmine::SubclassFactory | |
20 |
|
20 | |||
21 | default_scope lambda {order(:position)} |
|
21 | default_scope lambda {order(:position)} | |
22 |
|
22 | |||
23 | belongs_to :project |
|
23 | belongs_to :project | |
24 |
|
24 | |||
25 | acts_as_list :scope => 'type = \'#{type}\' AND #{parent_id ? "parent_id = #{parent_id}" : "parent_id IS NULL"}' |
|
25 | acts_as_list :scope => 'type = \'#{type}\' AND #{parent_id ? "parent_id = #{parent_id}" : "parent_id IS NULL"}' | |
26 | acts_as_customizable |
|
26 | acts_as_customizable | |
27 | acts_as_tree |
|
27 | acts_as_tree | |
28 |
|
28 | |||
29 | before_destroy :check_integrity |
|
29 | before_destroy :check_integrity | |
30 | before_save :check_default |
|
30 | before_save :check_default | |
31 |
|
31 | |||
32 | attr_protected :type |
|
32 | attr_protected :type | |
33 |
|
33 | |||
34 | validates_presence_of :name |
|
34 | validates_presence_of :name | |
35 | validates_uniqueness_of :name, :scope => [:type, :project_id] |
|
35 | validates_uniqueness_of :name, :scope => [:type, :project_id] | |
36 | validates_length_of :name, :maximum => 30 |
|
36 | validates_length_of :name, :maximum => 30 | |
37 |
|
37 | |||
38 | scope :shared, lambda { where(:project_id => nil) } |
|
38 | scope :shared, lambda { where(:project_id => nil) } | |
39 | scope :sorted, lambda { order(:position) } |
|
39 | scope :sorted, lambda { order(:position) } | |
40 | scope :active, lambda { where(:active => true) } |
|
40 | scope :active, lambda { where(:active => true) } | |
41 | scope :system, lambda { where(:project_id => nil) } |
|
41 | scope :system, lambda { where(:project_id => nil) } | |
42 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} |
|
42 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} | |
43 |
|
43 | |||
44 | def self.default |
|
44 | def self.default | |
45 | # Creates a fake default scope so Enumeration.default will check |
|
45 | # Creates a fake default scope so Enumeration.default will check | |
46 | # it's type. STI subclasses will automatically add their own |
|
46 | # it's type. STI subclasses will automatically add their own | |
47 | # types to the finder. |
|
47 | # types to the finder. | |
48 | if self.descends_from_active_record? |
|
48 | if self.descends_from_active_record? | |
49 | where(:is_default => true, :type => 'Enumeration').first |
|
49 | where(:is_default => true, :type => 'Enumeration').first | |
50 | else |
|
50 | else | |
51 | # STI classes are |
|
51 | # STI classes are | |
52 | where(:is_default => true).first |
|
52 | where(:is_default => true).first | |
53 | end |
|
53 | end | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | # Overloaded on concrete classes |
|
56 | # Overloaded on concrete classes | |
57 | def option_name |
|
57 | def option_name | |
58 | nil |
|
58 | nil | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def check_default |
|
61 | def check_default | |
62 | if is_default? && is_default_changed? |
|
62 | if is_default? && is_default_changed? | |
63 | Enumeration.where({:type => type}).update_all({:is_default => false}) |
|
63 | Enumeration.where({:type => type}).update_all({:is_default => false}) | |
64 | end |
|
64 | end | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | # Overloaded on concrete classes |
|
67 | # Overloaded on concrete classes | |
68 | def objects_count |
|
68 | def objects_count | |
69 | 0 |
|
69 | 0 | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def in_use? |
|
72 | def in_use? | |
73 | self.objects_count != 0 |
|
73 | self.objects_count != 0 | |
74 | end |
|
74 | end | |
75 |
|
75 | |||
76 | # Is this enumeration overriding a system level enumeration? |
|
76 | # Is this enumeration overriding a system level enumeration? | |
77 | def is_override? |
|
77 | def is_override? | |
78 | !self.parent.nil? |
|
78 | !self.parent.nil? | |
79 | end |
|
79 | end | |
80 |
|
80 | |||
81 | alias :destroy_without_reassign :destroy |
|
81 | alias :destroy_without_reassign :destroy | |
82 |
|
82 | |||
83 | # Destroy the enumeration |
|
83 | # Destroy the enumeration | |
84 | # If a enumeration is specified, objects are reassigned |
|
84 | # If a enumeration is specified, objects are reassigned | |
85 | def destroy(reassign_to = nil) |
|
85 | def destroy(reassign_to = nil) | |
86 | if reassign_to && reassign_to.is_a?(Enumeration) |
|
86 | if reassign_to && reassign_to.is_a?(Enumeration) | |
87 | self.transfer_relations(reassign_to) |
|
87 | self.transfer_relations(reassign_to) | |
88 | end |
|
88 | end | |
89 | destroy_without_reassign |
|
89 | destroy_without_reassign | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | def <=>(enumeration) |
|
92 | def <=>(enumeration) | |
93 | position <=> enumeration.position |
|
93 | position <=> enumeration.position | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | def to_s; name end |
|
96 | def to_s; name end | |
97 |
|
97 | |||
98 | # Returns the Subclasses of Enumeration. Each Subclass needs to be |
|
98 | # Returns the Subclasses of Enumeration. Each Subclass needs to be | |
99 | # required in development mode. |
|
99 | # required in development mode. | |
100 | # |
|
100 | # | |
101 | # Note: subclasses is protected in ActiveRecord |
|
101 | # Note: subclasses is protected in ActiveRecord | |
102 | def self.get_subclasses |
|
102 | def self.get_subclasses | |
103 | subclasses |
|
103 | subclasses | |
104 | end |
|
104 | end | |
105 |
|
105 | |||
106 | # Does the +new+ Hash override the previous Enumeration? |
|
106 | # Does the +new+ Hash override the previous Enumeration? | |
107 | def self.overriding_change?(new, previous) |
|
107 | def self.overriding_change?(new, previous) | |
108 | if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous) |
|
108 | if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous) | |
109 | return false |
|
109 | return false | |
110 | else |
|
110 | else | |
111 | return true |
|
111 | return true | |
112 | end |
|
112 | end | |
113 | end |
|
113 | end | |
114 |
|
114 | |||
115 | # Does the +new+ Hash have the same custom values as the previous Enumeration? |
|
115 | # Does the +new+ Hash have the same custom values as the previous Enumeration? | |
116 | def self.same_custom_values?(new, previous) |
|
116 | def self.same_custom_values?(new, previous) | |
117 | previous.custom_field_values.each do |custom_value| |
|
117 | previous.custom_field_values.each do |custom_value| | |
118 | if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s] |
|
118 | if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s] | |
119 | return false |
|
119 | return false | |
120 | end |
|
120 | end | |
121 | end |
|
121 | end | |
122 |
|
122 | |||
123 | return true |
|
123 | return true | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | # Are the new and previous fields equal? |
|
126 | # Are the new and previous fields equal? | |
127 | def self.same_active_state?(new, previous) |
|
127 | def self.same_active_state?(new, previous) | |
128 | new = (new == "1" ? true : false) |
|
128 | new = (new == "1" ? true : false) | |
129 | return new == previous |
|
129 | return new == previous | |
130 | end |
|
130 | end | |
131 |
|
131 | |||
132 | # Overrides acts_as_list reset_positions_in_list so that enumeration overrides |
|
132 | # Overrides acts_as_list reset_positions_in_list so that enumeration overrides | |
133 | # get the same position as the overriden enumeration |
|
133 | # get the same position as the overriden enumeration | |
134 | def reset_positions_in_list |
|
134 | def reset_positions_in_list | |
135 | super |
|
135 | super | |
136 | self.class. |
|
136 | # TODO: no database specific statement | |
137 | where("parent_id IS NOT NULL"). |
|
137 | if Redmine::Database.mysql? | |
138 | update_all("position = (SELECT MIN(position) FROM #{self.class.table_name} p WHERE p.id = #{self.class.table_name}.parent_id)") |
|
138 | self.class.connection.execute("UPDATE #{self.class.table_name} c JOIN #{self.class.table_name} p on p.id = c.parent_id SET c.position = p.position") | |
|
139 | else | |||
|
140 | self.class. | |||
|
141 | where("parent_id IS NOT NULL"). | |||
|
142 | update_all("position = (SELECT MIN(position) FROM #{self.class.table_name} p WHERE p.id = #{self.class.table_name}.parent_id)") | |||
|
143 | end | |||
139 | end |
|
144 | end | |
140 |
|
145 | |||
141 | private |
|
146 | private | |
142 | def check_integrity |
|
147 | def check_integrity | |
143 | raise "Cannot delete enumeration" if self.in_use? |
|
148 | raise "Cannot delete enumeration" if self.in_use? | |
144 | end |
|
149 | end | |
145 |
|
150 | |||
146 | # Overrides acts_as_list add_to_list_bottom so that enumeration overrides |
|
151 | # Overrides acts_as_list add_to_list_bottom so that enumeration overrides | |
147 | # get the same position as the overriden enumeration |
|
152 | # get the same position as the overriden enumeration | |
148 | def add_to_list_bottom |
|
153 | def add_to_list_bottom | |
149 | if parent |
|
154 | if parent | |
150 | self[position_column] = parent.position |
|
155 | self[position_column] = parent.position | |
151 | else |
|
156 | else | |
152 | super |
|
157 | super | |
153 | end |
|
158 | end | |
154 | end |
|
159 | end | |
155 |
|
160 | |||
156 | # Overrides acts_as_list remove_from_list so that enumeration overrides |
|
161 | # Overrides acts_as_list remove_from_list so that enumeration overrides | |
157 | # get the same position as the overriden enumeration |
|
162 | # get the same position as the overriden enumeration | |
158 | def remove_from_list |
|
163 | def remove_from_list | |
159 | if parent_id.blank? |
|
164 | if parent_id.blank? | |
160 | super |
|
165 | super | |
161 | end |
|
166 | end | |
162 | end |
|
167 | end | |
163 | end |
|
168 | end | |
164 |
|
169 | |||
165 | # Force load the subclasses in development mode |
|
170 | # Force load the subclasses in development mode | |
166 | require_dependency 'time_entry_activity' |
|
171 | require_dependency 'time_entry_activity' | |
167 | require_dependency 'document_category' |
|
172 | require_dependency 'document_category' | |
168 | require_dependency 'issue_priority' |
|
173 | require_dependency 'issue_priority' |
@@ -1,68 +1,73 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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 | module Redmine |
|
18 | module Redmine | |
19 | # Helper module to get information about the Redmine database |
|
19 | # Helper module to get information about the Redmine database | |
20 | module Database |
|
20 | module Database | |
21 | class << self |
|
21 | class << self | |
22 | # Returns true if the database is PostgreSQL |
|
22 | # Returns true if the database is PostgreSQL | |
23 | def postgresql? |
|
23 | def postgresql? | |
24 | (ActiveRecord::Base.connection.adapter_name =~ /postgresql/i).present? |
|
24 | (ActiveRecord::Base.connection.adapter_name =~ /postgresql/i).present? | |
25 | end |
|
25 | end | |
26 |
|
26 | |||
27 | # Returns the PostgreSQL version or nil if another DBMS is used |
|
27 | # Returns the PostgreSQL version or nil if another DBMS is used | |
28 | def postgresql_version |
|
28 | def postgresql_version | |
29 | postgresql? ? ActiveRecord::Base.connection.send(:postgresql_version) : nil |
|
29 | postgresql? ? ActiveRecord::Base.connection.send(:postgresql_version) : nil | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | # Returns true if the database is a PostgreSQL >=9.0 database with the unaccent extension installed |
|
32 | # Returns true if the database is a PostgreSQL >=9.0 database with the unaccent extension installed | |
33 | def postgresql_unaccent? |
|
33 | def postgresql_unaccent? | |
34 | if postgresql? |
|
34 | if postgresql? | |
35 | return @postgresql_unaccent unless @postgresql_unaccent.nil? |
|
35 | return @postgresql_unaccent unless @postgresql_unaccent.nil? | |
36 | begin |
|
36 | begin | |
37 | sql = "SELECT name FROM pg_available_extensions WHERE installed_version IS NOT NULL and name = 'unaccent'" |
|
37 | sql = "SELECT name FROM pg_available_extensions WHERE installed_version IS NOT NULL and name = 'unaccent'" | |
38 | @postgresql_unaccent = postgresql_version >= 90000 && ActiveRecord::Base.connection.select_value(sql).present? |
|
38 | @postgresql_unaccent = postgresql_version >= 90000 && ActiveRecord::Base.connection.select_value(sql).present? | |
39 | rescue |
|
39 | rescue | |
40 | false |
|
40 | false | |
41 | end |
|
41 | end | |
42 | else |
|
42 | else | |
43 | false |
|
43 | false | |
44 | end |
|
44 | end | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
|
47 | # Returns true if the database is MySQL | |||
|
48 | def mysql? | |||
|
49 | (ActiveRecord::Base.connection.adapter_name =~ /mysql/i).present? | |||
|
50 | end | |||
|
51 | ||||
47 | # Returns a SQL statement for case/accent (if possible) insensitive match |
|
52 | # Returns a SQL statement for case/accent (if possible) insensitive match | |
48 | def like(left, right, options={}) |
|
53 | def like(left, right, options={}) | |
49 | neg = (options[:match] == false ? 'NOT ' : '') |
|
54 | neg = (options[:match] == false ? 'NOT ' : '') | |
50 |
|
55 | |||
51 | if postgresql? |
|
56 | if postgresql? | |
52 | if postgresql_unaccent? |
|
57 | if postgresql_unaccent? | |
53 | "unaccent(#{left}) #{neg}ILIKE unaccent(#{right})" |
|
58 | "unaccent(#{left}) #{neg}ILIKE unaccent(#{right})" | |
54 | else |
|
59 | else | |
55 | "#{left} #{neg}ILIKE #{right}" |
|
60 | "#{left} #{neg}ILIKE #{right}" | |
56 | end |
|
61 | end | |
57 | else |
|
62 | else | |
58 | "#{left} #{neg}LIKE #{right}" |
|
63 | "#{left} #{neg}LIKE #{right}" | |
59 | end |
|
64 | end | |
60 | end |
|
65 | end | |
61 |
|
66 | |||
62 | # Resets database information |
|
67 | # Resets database information | |
63 | def reset |
|
68 | def reset | |
64 | @postgresql_unaccent = nil |
|
69 | @postgresql_unaccent = nil | |
65 | end |
|
70 | end | |
66 | end |
|
71 | end | |
67 | end |
|
72 | end | |
68 | end |
|
73 | end |
General Comments 0
You need to be logged in to leave comments.
Login now