##// END OF EJS Templates
Do not DELETE/INSERT enabled_modules when updating project modules....
Jean-Philippe Lang -
r2412:b05ed594a086
parent child
Show More
@@ -1,319 +1,323
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 Project < ActiveRecord::Base
18 class Project < ActiveRecord::Base
19 # Project statuses
19 # Project statuses
20 STATUS_ACTIVE = 1
20 STATUS_ACTIVE = 1
21 STATUS_ARCHIVED = 9
21 STATUS_ARCHIVED = 9
22
22
23 has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
23 has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
24 has_many :users, :through => :members
24 has_many :users, :through => :members
25 has_many :enabled_modules, :dependent => :delete_all
25 has_many :enabled_modules, :dependent => :delete_all
26 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
26 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
27 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
27 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
28 has_many :issue_changes, :through => :issues, :source => :journals
28 has_many :issue_changes, :through => :issues, :source => :journals
29 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
29 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
30 has_many :time_entries, :dependent => :delete_all
30 has_many :time_entries, :dependent => :delete_all
31 has_many :queries, :dependent => :delete_all
31 has_many :queries, :dependent => :delete_all
32 has_many :documents, :dependent => :destroy
32 has_many :documents, :dependent => :destroy
33 has_many :news, :dependent => :delete_all, :include => :author
33 has_many :news, :dependent => :delete_all, :include => :author
34 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
34 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
35 has_many :boards, :dependent => :destroy, :order => "position ASC"
35 has_many :boards, :dependent => :destroy, :order => "position ASC"
36 has_one :repository, :dependent => :destroy
36 has_one :repository, :dependent => :destroy
37 has_many :changesets, :through => :repository
37 has_many :changesets, :through => :repository
38 has_one :wiki, :dependent => :destroy
38 has_one :wiki, :dependent => :destroy
39 # Custom field for the project issues
39 # Custom field for the project issues
40 has_and_belongs_to_many :issue_custom_fields,
40 has_and_belongs_to_many :issue_custom_fields,
41 :class_name => 'IssueCustomField',
41 :class_name => 'IssueCustomField',
42 :order => "#{CustomField.table_name}.position",
42 :order => "#{CustomField.table_name}.position",
43 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
43 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
44 :association_foreign_key => 'custom_field_id'
44 :association_foreign_key => 'custom_field_id'
45
45
46 acts_as_nested_set :order => 'name', :dependent => :destroy
46 acts_as_nested_set :order => 'name', :dependent => :destroy
47 acts_as_attachable :view_permission => :view_files,
47 acts_as_attachable :view_permission => :view_files,
48 :delete_permission => :manage_files
48 :delete_permission => :manage_files
49
49
50 acts_as_customizable
50 acts_as_customizable
51 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
51 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
52 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
52 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
53 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
53 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
54 :author => nil
54 :author => nil
55
55
56 attr_protected :status, :enabled_module_names
56 attr_protected :status, :enabled_module_names
57
57
58 validates_presence_of :name, :identifier
58 validates_presence_of :name, :identifier
59 validates_uniqueness_of :name, :identifier
59 validates_uniqueness_of :name, :identifier
60 validates_associated :repository, :wiki
60 validates_associated :repository, :wiki
61 validates_length_of :name, :maximum => 30
61 validates_length_of :name, :maximum => 30
62 validates_length_of :homepage, :maximum => 255
62 validates_length_of :homepage, :maximum => 255
63 validates_length_of :identifier, :in => 2..20
63 validates_length_of :identifier, :in => 2..20
64 validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
64 validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
65
65
66 before_destroy :delete_all_members
66 before_destroy :delete_all_members
67
67
68 named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
68 named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
69 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
69 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
70 named_scope :public, { :conditions => { :is_public => true } }
70 named_scope :public, { :conditions => { :is_public => true } }
71 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
71 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
72
72
73 def identifier=(identifier)
73 def identifier=(identifier)
74 super unless identifier_frozen?
74 super unless identifier_frozen?
75 end
75 end
76
76
77 def identifier_frozen?
77 def identifier_frozen?
78 errors[:identifier].nil? && !(new_record? || identifier.blank?)
78 errors[:identifier].nil? && !(new_record? || identifier.blank?)
79 end
79 end
80
80
81 def issues_with_subprojects(include_subprojects=false)
81 def issues_with_subprojects(include_subprojects=false)
82 conditions = nil
82 conditions = nil
83 if include_subprojects
83 if include_subprojects
84 ids = [id] + descendants.collect(&:id)
84 ids = [id] + descendants.collect(&:id)
85 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
85 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
86 end
86 end
87 conditions ||= ["#{Project.table_name}.id = ?", id]
87 conditions ||= ["#{Project.table_name}.id = ?", id]
88 # Quick and dirty fix for Rails 2 compatibility
88 # Quick and dirty fix for Rails 2 compatibility
89 Issue.send(:with_scope, :find => { :conditions => conditions }) do
89 Issue.send(:with_scope, :find => { :conditions => conditions }) do
90 Version.send(:with_scope, :find => { :conditions => conditions }) do
90 Version.send(:with_scope, :find => { :conditions => conditions }) do
91 yield
91 yield
92 end
92 end
93 end
93 end
94 end
94 end
95
95
96 # returns latest created projects
96 # returns latest created projects
97 # non public projects will be returned only if user is a member of those
97 # non public projects will be returned only if user is a member of those
98 def self.latest(user=nil, count=5)
98 def self.latest(user=nil, count=5)
99 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
99 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
100 end
100 end
101
101
102 def self.visible_by(user=nil)
102 def self.visible_by(user=nil)
103 user ||= User.current
103 user ||= User.current
104 if user && user.admin?
104 if user && user.admin?
105 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
105 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
106 elsif user && user.memberships.any?
106 elsif user && user.memberships.any?
107 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
107 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
108 else
108 else
109 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
109 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
110 end
110 end
111 end
111 end
112
112
113 def self.allowed_to_condition(user, permission, options={})
113 def self.allowed_to_condition(user, permission, options={})
114 statements = []
114 statements = []
115 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
115 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
116 if perm = Redmine::AccessControl.permission(permission)
116 if perm = Redmine::AccessControl.permission(permission)
117 unless perm.project_module.nil?
117 unless perm.project_module.nil?
118 # If the permission belongs to a project module, make sure the module is enabled
118 # If the permission belongs to a project module, make sure the module is enabled
119 base_statement << " AND EXISTS (SELECT em.id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}' AND em.project_id=#{Project.table_name}.id)"
119 base_statement << " AND EXISTS (SELECT em.id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}' AND em.project_id=#{Project.table_name}.id)"
120 end
120 end
121 end
121 end
122 if options[:project]
122 if options[:project]
123 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
123 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
124 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
124 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
125 base_statement = "(#{project_statement}) AND (#{base_statement})"
125 base_statement = "(#{project_statement}) AND (#{base_statement})"
126 end
126 end
127 if user.admin?
127 if user.admin?
128 # no restriction
128 # no restriction
129 else
129 else
130 statements << "1=0"
130 statements << "1=0"
131 if user.logged?
131 if user.logged?
132 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
132 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
133 allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
133 allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
134 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
134 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
135 elsif Role.anonymous.allowed_to?(permission)
135 elsif Role.anonymous.allowed_to?(permission)
136 # anonymous user allowed on public project
136 # anonymous user allowed on public project
137 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
137 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
138 else
138 else
139 # anonymous user is not authorized
139 # anonymous user is not authorized
140 end
140 end
141 end
141 end
142 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
142 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
143 end
143 end
144
144
145 def project_condition(with_subprojects)
145 def project_condition(with_subprojects)
146 cond = "#{Project.table_name}.id = #{id}"
146 cond = "#{Project.table_name}.id = #{id}"
147 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
147 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
148 cond
148 cond
149 end
149 end
150
150
151 def self.find(*args)
151 def self.find(*args)
152 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
152 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
153 project = find_by_identifier(*args)
153 project = find_by_identifier(*args)
154 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
154 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
155 project
155 project
156 else
156 else
157 super
157 super
158 end
158 end
159 end
159 end
160
160
161 def to_param
161 def to_param
162 # id is used for projects with a numeric identifier (compatibility)
162 # id is used for projects with a numeric identifier (compatibility)
163 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
163 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
164 end
164 end
165
165
166 def active?
166 def active?
167 self.status == STATUS_ACTIVE
167 self.status == STATUS_ACTIVE
168 end
168 end
169
169
170 # Archives the project and its descendants recursively
170 # Archives the project and its descendants recursively
171 def archive
171 def archive
172 # Archive subprojects if any
172 # Archive subprojects if any
173 children.each do |subproject|
173 children.each do |subproject|
174 subproject.archive
174 subproject.archive
175 end
175 end
176 update_attribute :status, STATUS_ARCHIVED
176 update_attribute :status, STATUS_ARCHIVED
177 end
177 end
178
178
179 # Unarchives the project
179 # Unarchives the project
180 # All its ancestors must be active
180 # All its ancestors must be active
181 def unarchive
181 def unarchive
182 return false if ancestors.detect {|a| !a.active?}
182 return false if ancestors.detect {|a| !a.active?}
183 update_attribute :status, STATUS_ACTIVE
183 update_attribute :status, STATUS_ACTIVE
184 end
184 end
185
185
186 # Returns an array of projects the project can be moved to
186 # Returns an array of projects the project can be moved to
187 def possible_parents
187 def possible_parents
188 @possible_parents ||= (Project.active.find(:all) - self_and_descendants)
188 @possible_parents ||= (Project.active.find(:all) - self_and_descendants)
189 end
189 end
190
190
191 # Sets the parent of the project
191 # Sets the parent of the project
192 # Argument can be either a Project, a String, a Fixnum or nil
192 # Argument can be either a Project, a String, a Fixnum or nil
193 def set_parent!(p)
193 def set_parent!(p)
194 unless p.nil? || p.is_a?(Project)
194 unless p.nil? || p.is_a?(Project)
195 if p.to_s.blank?
195 if p.to_s.blank?
196 p = nil
196 p = nil
197 else
197 else
198 p = Project.find_by_id(p)
198 p = Project.find_by_id(p)
199 return false unless p
199 return false unless p
200 end
200 end
201 end
201 end
202 if p == parent && !p.nil?
202 if p == parent && !p.nil?
203 # Nothing to do
203 # Nothing to do
204 true
204 true
205 elsif p.nil? || (p.active? && move_possible?(p))
205 elsif p.nil? || (p.active? && move_possible?(p))
206 # Insert the project so that target's children or root projects stay alphabetically sorted
206 # Insert the project so that target's children or root projects stay alphabetically sorted
207 sibs = (p.nil? ? self.class.roots : p.children)
207 sibs = (p.nil? ? self.class.roots : p.children)
208 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
208 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
209 if to_be_inserted_before
209 if to_be_inserted_before
210 move_to_left_of(to_be_inserted_before)
210 move_to_left_of(to_be_inserted_before)
211 elsif p.nil?
211 elsif p.nil?
212 if sibs.empty?
212 if sibs.empty?
213 # move_to_root adds the project in first (ie. left) position
213 # move_to_root adds the project in first (ie. left) position
214 move_to_root
214 move_to_root
215 else
215 else
216 move_to_right_of(sibs.last) unless self == sibs.last
216 move_to_right_of(sibs.last) unless self == sibs.last
217 end
217 end
218 else
218 else
219 # move_to_child_of adds the project in last (ie.right) position
219 # move_to_child_of adds the project in last (ie.right) position
220 move_to_child_of(p)
220 move_to_child_of(p)
221 end
221 end
222 true
222 true
223 else
223 else
224 # Can not move to the given target
224 # Can not move to the given target
225 false
225 false
226 end
226 end
227 end
227 end
228
228
229 # Returns an array of the trackers used by the project and its active sub projects
229 # Returns an array of the trackers used by the project and its active sub projects
230 def rolled_up_trackers
230 def rolled_up_trackers
231 @rolled_up_trackers ||=
231 @rolled_up_trackers ||=
232 Tracker.find(:all, :include => :projects,
232 Tracker.find(:all, :include => :projects,
233 :select => "DISTINCT #{Tracker.table_name}.*",
233 :select => "DISTINCT #{Tracker.table_name}.*",
234 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
234 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
235 :order => "#{Tracker.table_name}.position")
235 :order => "#{Tracker.table_name}.position")
236 end
236 end
237
237
238 # Deletes all project's members
238 # Deletes all project's members
239 def delete_all_members
239 def delete_all_members
240 Member.delete_all(['project_id = ?', id])
240 Member.delete_all(['project_id = ?', id])
241 end
241 end
242
242
243 # Users issues can be assigned to
243 # Users issues can be assigned to
244 def assignable_users
244 def assignable_users
245 members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort
245 members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort
246 end
246 end
247
247
248 # Returns the mail adresses of users that should be always notified on project events
248 # Returns the mail adresses of users that should be always notified on project events
249 def recipients
249 def recipients
250 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
250 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
251 end
251 end
252
252
253 # Returns an array of all custom fields enabled for project issues
253 # Returns an array of all custom fields enabled for project issues
254 # (explictly associated custom fields and custom fields enabled for all projects)
254 # (explictly associated custom fields and custom fields enabled for all projects)
255 def all_issue_custom_fields
255 def all_issue_custom_fields
256 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
256 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
257 end
257 end
258
258
259 def project
259 def project
260 self
260 self
261 end
261 end
262
262
263 def <=>(project)
263 def <=>(project)
264 name.downcase <=> project.name.downcase
264 name.downcase <=> project.name.downcase
265 end
265 end
266
266
267 def to_s
267 def to_s
268 name
268 name
269 end
269 end
270
270
271 # Returns a short description of the projects (first lines)
271 # Returns a short description of the projects (first lines)
272 def short_description(length = 255)
272 def short_description(length = 255)
273 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
273 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
274 end
274 end
275
275
276 def allows_to?(action)
276 def allows_to?(action)
277 if action.is_a? Hash
277 if action.is_a? Hash
278 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
278 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
279 else
279 else
280 allowed_permissions.include? action
280 allowed_permissions.include? action
281 end
281 end
282 end
282 end
283
283
284 def module_enabled?(module_name)
284 def module_enabled?(module_name)
285 module_name = module_name.to_s
285 module_name = module_name.to_s
286 enabled_modules.detect {|m| m.name == module_name}
286 enabled_modules.detect {|m| m.name == module_name}
287 end
287 end
288
288
289 def enabled_module_names=(module_names)
289 def enabled_module_names=(module_names)
290 enabled_modules.clear
290 if module_names && module_names.is_a?(Array)
291 module_names = [] unless module_names && module_names.is_a?(Array)
291 module_names = module_names.collect(&:to_s)
292 module_names.each do |name|
292 # remove disabled modules
293 enabled_modules << EnabledModule.new(:name => name.to_s)
293 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
294 # add new modules
295 module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)}
296 else
297 enabled_modules.clear
294 end
298 end
295 end
299 end
296
300
297 # Returns an auto-generated project identifier based on the last identifier used
301 # Returns an auto-generated project identifier based on the last identifier used
298 def self.next_identifier
302 def self.next_identifier
299 p = Project.find(:first, :order => 'created_on DESC')
303 p = Project.find(:first, :order => 'created_on DESC')
300 p.nil? ? nil : p.identifier.to_s.succ
304 p.nil? ? nil : p.identifier.to_s.succ
301 end
305 end
302
306
303 protected
307 protected
304 def validate
308 def validate
305 errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
309 errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
306 end
310 end
307
311
308 private
312 private
309 def allowed_permissions
313 def allowed_permissions
310 @allowed_permissions ||= begin
314 @allowed_permissions ||= begin
311 module_names = enabled_modules.collect {|m| m.name}
315 module_names = enabled_modules.collect {|m| m.name}
312 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
316 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
313 end
317 end
314 end
318 end
315
319
316 def allowed_actions
320 def allowed_actions
317 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
321 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
318 end
322 end
319 end
323 end
@@ -1,221 +1,236
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class ProjectTest < Test::Unit::TestCase
20 class ProjectTest < Test::Unit::TestCase
21 fixtures :projects, :issues, :issue_statuses, :journals, :journal_details, :users, :members, :roles, :projects_trackers, :trackers, :boards
21 fixtures :projects, :enabled_modules,
22 :issues, :issue_statuses, :journals, :journal_details,
23 :users, :members, :roles, :projects_trackers, :trackers, :boards
22
24
23 def setup
25 def setup
24 @ecookbook = Project.find(1)
26 @ecookbook = Project.find(1)
25 @ecookbook_sub1 = Project.find(3)
27 @ecookbook_sub1 = Project.find(3)
26 end
28 end
27
29
28 def test_truth
30 def test_truth
29 assert_kind_of Project, @ecookbook
31 assert_kind_of Project, @ecookbook
30 assert_equal "eCookbook", @ecookbook.name
32 assert_equal "eCookbook", @ecookbook.name
31 end
33 end
32
34
33 def test_update
35 def test_update
34 assert_equal "eCookbook", @ecookbook.name
36 assert_equal "eCookbook", @ecookbook.name
35 @ecookbook.name = "eCook"
37 @ecookbook.name = "eCook"
36 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
38 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
37 @ecookbook.reload
39 @ecookbook.reload
38 assert_equal "eCook", @ecookbook.name
40 assert_equal "eCook", @ecookbook.name
39 end
41 end
40
42
41 def test_validate
43 def test_validate
42 @ecookbook.name = ""
44 @ecookbook.name = ""
43 assert !@ecookbook.save
45 assert !@ecookbook.save
44 assert_equal 1, @ecookbook.errors.count
46 assert_equal 1, @ecookbook.errors.count
45 assert_equal "activerecord_error_blank", @ecookbook.errors.on(:name)
47 assert_equal "activerecord_error_blank", @ecookbook.errors.on(:name)
46 end
48 end
47
49
48 def test_archive
50 def test_archive
49 user = @ecookbook.members.first.user
51 user = @ecookbook.members.first.user
50 @ecookbook.archive
52 @ecookbook.archive
51 @ecookbook.reload
53 @ecookbook.reload
52
54
53 assert !@ecookbook.active?
55 assert !@ecookbook.active?
54 assert !user.projects.include?(@ecookbook)
56 assert !user.projects.include?(@ecookbook)
55 # Subproject are also archived
57 # Subproject are also archived
56 assert !@ecookbook.children.empty?
58 assert !@ecookbook.children.empty?
57 assert @ecookbook.descendants.active.empty?
59 assert @ecookbook.descendants.active.empty?
58 end
60 end
59
61
60 def test_unarchive
62 def test_unarchive
61 user = @ecookbook.members.first.user
63 user = @ecookbook.members.first.user
62 @ecookbook.archive
64 @ecookbook.archive
63 # A subproject of an archived project can not be unarchived
65 # A subproject of an archived project can not be unarchived
64 assert !@ecookbook_sub1.unarchive
66 assert !@ecookbook_sub1.unarchive
65
67
66 # Unarchive project
68 # Unarchive project
67 assert @ecookbook.unarchive
69 assert @ecookbook.unarchive
68 @ecookbook.reload
70 @ecookbook.reload
69 assert @ecookbook.active?
71 assert @ecookbook.active?
70 assert user.projects.include?(@ecookbook)
72 assert user.projects.include?(@ecookbook)
71 # Subproject can now be unarchived
73 # Subproject can now be unarchived
72 @ecookbook_sub1.reload
74 @ecookbook_sub1.reload
73 assert @ecookbook_sub1.unarchive
75 assert @ecookbook_sub1.unarchive
74 end
76 end
75
77
76 def test_destroy
78 def test_destroy
77 # 2 active members
79 # 2 active members
78 assert_equal 2, @ecookbook.members.size
80 assert_equal 2, @ecookbook.members.size
79 # and 1 is locked
81 # and 1 is locked
80 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
82 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
81 # some boards
83 # some boards
82 assert @ecookbook.boards.any?
84 assert @ecookbook.boards.any?
83
85
84 @ecookbook.destroy
86 @ecookbook.destroy
85 # make sure that the project non longer exists
87 # make sure that the project non longer exists
86 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
88 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
87 # make sure related data was removed
89 # make sure related data was removed
88 assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
90 assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
89 assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
91 assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
90 end
92 end
91
93
92 def test_move_an_orphan_project_to_a_root_project
94 def test_move_an_orphan_project_to_a_root_project
93 sub = Project.find(2)
95 sub = Project.find(2)
94 sub.set_parent! @ecookbook
96 sub.set_parent! @ecookbook
95 assert_equal @ecookbook.id, sub.parent.id
97 assert_equal @ecookbook.id, sub.parent.id
96 @ecookbook.reload
98 @ecookbook.reload
97 assert_equal 4, @ecookbook.children.size
99 assert_equal 4, @ecookbook.children.size
98 end
100 end
99
101
100 def test_move_an_orphan_project_to_a_subproject
102 def test_move_an_orphan_project_to_a_subproject
101 sub = Project.find(2)
103 sub = Project.find(2)
102 assert sub.set_parent!(@ecookbook_sub1)
104 assert sub.set_parent!(@ecookbook_sub1)
103 end
105 end
104
106
105 def test_move_a_root_project_to_a_project
107 def test_move_a_root_project_to_a_project
106 sub = @ecookbook
108 sub = @ecookbook
107 assert sub.set_parent!(Project.find(2))
109 assert sub.set_parent!(Project.find(2))
108 end
110 end
109
111
110 def test_should_not_move_a_project_to_its_children
112 def test_should_not_move_a_project_to_its_children
111 sub = @ecookbook
113 sub = @ecookbook
112 assert !(sub.set_parent!(Project.find(3)))
114 assert !(sub.set_parent!(Project.find(3)))
113 end
115 end
114
116
115 def test_set_parent_should_add_roots_in_alphabetical_order
117 def test_set_parent_should_add_roots_in_alphabetical_order
116 ProjectCustomField.delete_all
118 ProjectCustomField.delete_all
117 Project.delete_all
119 Project.delete_all
118 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
120 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
119 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
121 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
120 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
122 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
121 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
123 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
122
124
123 assert_equal 4, Project.count
125 assert_equal 4, Project.count
124 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
126 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
125 end
127 end
126
128
127 def test_set_parent_should_add_children_in_alphabetical_order
129 def test_set_parent_should_add_children_in_alphabetical_order
128 ProjectCustomField.delete_all
130 ProjectCustomField.delete_all
129 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
131 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
130 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
132 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
131 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
133 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
132 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
134 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
133 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
135 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
134
136
135 parent.reload
137 parent.reload
136 assert_equal 4, parent.children.size
138 assert_equal 4, parent.children.size
137 assert_equal parent.children.sort_by(&:name), parent.children
139 assert_equal parent.children.sort_by(&:name), parent.children
138 end
140 end
139
141
140 def test_rebuild_should_sort_children_alphabetically
142 def test_rebuild_should_sort_children_alphabetically
141 ProjectCustomField.delete_all
143 ProjectCustomField.delete_all
142 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
144 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
143 Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
145 Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
144 Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
146 Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
145 Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
147 Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
146 Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
148 Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
147
149
148 Project.update_all("lft = NULL, rgt = NULL")
150 Project.update_all("lft = NULL, rgt = NULL")
149 Project.rebuild!
151 Project.rebuild!
150
152
151 parent.reload
153 parent.reload
152 assert_equal 4, parent.children.size
154 assert_equal 4, parent.children.size
153 assert_equal parent.children.sort_by(&:name), parent.children
155 assert_equal parent.children.sort_by(&:name), parent.children
154 end
156 end
155
157
156 def test_parent
158 def test_parent
157 p = Project.find(6).parent
159 p = Project.find(6).parent
158 assert p.is_a?(Project)
160 assert p.is_a?(Project)
159 assert_equal 5, p.id
161 assert_equal 5, p.id
160 end
162 end
161
163
162 def test_ancestors
164 def test_ancestors
163 a = Project.find(6).ancestors
165 a = Project.find(6).ancestors
164 assert a.first.is_a?(Project)
166 assert a.first.is_a?(Project)
165 assert_equal [1, 5], a.collect(&:id)
167 assert_equal [1, 5], a.collect(&:id)
166 end
168 end
167
169
168 def test_root
170 def test_root
169 r = Project.find(6).root
171 r = Project.find(6).root
170 assert r.is_a?(Project)
172 assert r.is_a?(Project)
171 assert_equal 1, r.id
173 assert_equal 1, r.id
172 end
174 end
173
175
174 def test_children
176 def test_children
175 c = Project.find(1).children
177 c = Project.find(1).children
176 assert c.first.is_a?(Project)
178 assert c.first.is_a?(Project)
177 assert_equal [5, 3, 4], c.collect(&:id)
179 assert_equal [5, 3, 4], c.collect(&:id)
178 end
180 end
179
181
180 def test_descendants
182 def test_descendants
181 d = Project.find(1).descendants
183 d = Project.find(1).descendants
182 assert d.first.is_a?(Project)
184 assert d.first.is_a?(Project)
183 assert_equal [5, 6, 3, 4], d.collect(&:id)
185 assert_equal [5, 6, 3, 4], d.collect(&:id)
184 end
186 end
185
187
186 def test_rolled_up_trackers
188 def test_rolled_up_trackers
187 parent = Project.find(1)
189 parent = Project.find(1)
188 parent.trackers = Tracker.find([1,2])
190 parent.trackers = Tracker.find([1,2])
189 child = parent.children.find(3)
191 child = parent.children.find(3)
190
192
191 assert_equal [1, 2], parent.tracker_ids
193 assert_equal [1, 2], parent.tracker_ids
192 assert_equal [2, 3], child.tracker_ids
194 assert_equal [2, 3], child.tracker_ids
193
195
194 assert_kind_of Tracker, parent.rolled_up_trackers.first
196 assert_kind_of Tracker, parent.rolled_up_trackers.first
195 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
197 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
196
198
197 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
199 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
198 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
200 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
199 end
201 end
200
202
201 def test_rolled_up_trackers_should_ignore_archived_subprojects
203 def test_rolled_up_trackers_should_ignore_archived_subprojects
202 parent = Project.find(1)
204 parent = Project.find(1)
203 parent.trackers = Tracker.find([1,2])
205 parent.trackers = Tracker.find([1,2])
204 child = parent.children.find(3)
206 child = parent.children.find(3)
205 child.trackers = Tracker.find([1,3])
207 child.trackers = Tracker.find([1,3])
206 parent.children.each(&:archive)
208 parent.children.each(&:archive)
207
209
208 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
210 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
209 end
211 end
210
212
211 def test_next_identifier
213 def test_next_identifier
212 ProjectCustomField.delete_all
214 ProjectCustomField.delete_all
213 Project.create!(:name => 'last', :identifier => 'p2008040')
215 Project.create!(:name => 'last', :identifier => 'p2008040')
214 assert_equal 'p2008041', Project.next_identifier
216 assert_equal 'p2008041', Project.next_identifier
215 end
217 end
216
218
217 def test_next_identifier_first_project
219 def test_next_identifier_first_project
218 Project.delete_all
220 Project.delete_all
219 assert_nil Project.next_identifier
221 assert_nil Project.next_identifier
220 end
222 end
223
224 def test_enabled_module_names_should_not_recreate_enabled_modules
225 project = Project.find(1)
226 # Remove one module
227 modules = project.enabled_modules.slice(0..-2)
228 assert modules.any?
229 assert_difference 'EnabledModule.count', -1 do
230 project.enabled_module_names = modules.collect(&:name)
231 end
232 project.reload
233 # Ids should be preserved
234 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
235 end
221 end
236 end
General Comments 0
You need to be logged in to leave comments. Login now