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