##// END OF EJS Templates
Load roles when loading project members....
Jean-Philippe Lang -
r2964:d73fb1fab896
parent child
Show More
@@ -1,607 +1,607
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 # Specific overidden Activities
24 24 has_many :time_entry_activities do
25 25 def active
26 26 find(:all, :conditions => {:active => true})
27 27 end
28 28 end
29 has_many :members, :include => :user, :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
29 has_many :members, :include => [:user, :roles], :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
30 30 has_many :member_principals, :class_name => 'Member',
31 31 :include => :principal,
32 32 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
33 33 has_many :users, :through => :members
34 34 has_many :principals, :through => :member_principals, :source => :principal
35 35
36 36 has_many :enabled_modules, :dependent => :delete_all
37 37 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
38 38 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
39 39 has_many :issue_changes, :through => :issues, :source => :journals
40 40 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
41 41 has_many :time_entries, :dependent => :delete_all
42 42 has_many :queries, :dependent => :delete_all
43 43 has_many :documents, :dependent => :destroy
44 44 has_many :news, :dependent => :delete_all, :include => :author
45 45 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
46 46 has_many :boards, :dependent => :destroy, :order => "position ASC"
47 47 has_one :repository, :dependent => :destroy
48 48 has_many :changesets, :through => :repository
49 49 has_one :wiki, :dependent => :destroy
50 50 # Custom field for the project issues
51 51 has_and_belongs_to_many :issue_custom_fields,
52 52 :class_name => 'IssueCustomField',
53 53 :order => "#{CustomField.table_name}.position",
54 54 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
55 55 :association_foreign_key => 'custom_field_id'
56 56
57 57 acts_as_nested_set :order => 'name', :dependent => :destroy
58 58 acts_as_attachable :view_permission => :view_files,
59 59 :delete_permission => :manage_files
60 60
61 61 acts_as_customizable
62 62 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
63 63 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
64 64 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
65 65 :author => nil
66 66
67 67 attr_protected :status, :enabled_module_names
68 68
69 69 validates_presence_of :name, :identifier
70 70 validates_uniqueness_of :name, :identifier
71 71 validates_associated :repository, :wiki
72 72 validates_length_of :name, :maximum => 30
73 73 validates_length_of :homepage, :maximum => 255
74 74 validates_length_of :identifier, :in => 1..20
75 75 # donwcase letters, digits, dashes but not digits only
76 76 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
77 77 # reserved words
78 78 validates_exclusion_of :identifier, :in => %w( new )
79 79
80 80 before_destroy :delete_all_members
81 81
82 82 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] } }
83 83 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
84 84 named_scope :all_public, { :conditions => { :is_public => true } }
85 85 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
86 86
87 87 def identifier=(identifier)
88 88 super unless identifier_frozen?
89 89 end
90 90
91 91 def identifier_frozen?
92 92 errors[:identifier].nil? && !(new_record? || identifier.blank?)
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 # Returns a SQL :conditions string used to find all active projects for the specified user.
102 102 #
103 103 # Examples:
104 104 # Projects.visible_by(admin) => "projects.status = 1"
105 105 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
106 106 def self.visible_by(user=nil)
107 107 user ||= User.current
108 108 if user && user.admin?
109 109 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
110 110 elsif user && user.memberships.any?
111 111 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(',')}))"
112 112 else
113 113 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
114 114 end
115 115 end
116 116
117 117 def self.allowed_to_condition(user, permission, options={})
118 118 statements = []
119 119 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
120 120 if perm = Redmine::AccessControl.permission(permission)
121 121 unless perm.project_module.nil?
122 122 # If the permission belongs to a project module, make sure the module is enabled
123 123 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
124 124 end
125 125 end
126 126 if options[:project]
127 127 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
128 128 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
129 129 base_statement = "(#{project_statement}) AND (#{base_statement})"
130 130 end
131 131 if user.admin?
132 132 # no restriction
133 133 else
134 134 statements << "1=0"
135 135 if user.logged?
136 136 if Role.non_member.allowed_to?(permission) && !options[:member]
137 137 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
138 138 end
139 139 allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id}
140 140 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
141 141 else
142 142 if Role.anonymous.allowed_to?(permission) && !options[:member]
143 143 # anonymous user allowed on public project
144 144 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
145 145 end
146 146 end
147 147 end
148 148 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
149 149 end
150 150
151 151 # Returns the Systemwide and project specific activities
152 152 def activities(include_inactive=false)
153 153 if include_inactive
154 154 return all_activities
155 155 else
156 156 return active_activities
157 157 end
158 158 end
159 159
160 160 # Will create a new Project specific Activity or update an existing one
161 161 #
162 162 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
163 163 # does not successfully save.
164 164 def update_or_create_time_entry_activity(id, activity_hash)
165 165 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
166 166 self.create_time_entry_activity_if_needed(activity_hash)
167 167 else
168 168 activity = project.time_entry_activities.find_by_id(id.to_i)
169 169 activity.update_attributes(activity_hash) if activity
170 170 end
171 171 end
172 172
173 173 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
174 174 #
175 175 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
176 176 # does not successfully save.
177 177 def create_time_entry_activity_if_needed(activity)
178 178 if activity['parent_id']
179 179
180 180 parent_activity = TimeEntryActivity.find(activity['parent_id'])
181 181 activity['name'] = parent_activity.name
182 182 activity['position'] = parent_activity.position
183 183
184 184 if Enumeration.overridding_change?(activity, parent_activity)
185 185 project_activity = self.time_entry_activities.create(activity)
186 186
187 187 if project_activity.new_record?
188 188 raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved"
189 189 else
190 190 self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id])
191 191 end
192 192 end
193 193 end
194 194 end
195 195
196 196 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
197 197 #
198 198 # Examples:
199 199 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
200 200 # project.project_condition(false) => "projects.id = 1"
201 201 def project_condition(with_subprojects)
202 202 cond = "#{Project.table_name}.id = #{id}"
203 203 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
204 204 cond
205 205 end
206 206
207 207 def self.find(*args)
208 208 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
209 209 project = find_by_identifier(*args)
210 210 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
211 211 project
212 212 else
213 213 super
214 214 end
215 215 end
216 216
217 217 def to_param
218 218 # id is used for projects with a numeric identifier (compatibility)
219 219 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
220 220 end
221 221
222 222 def active?
223 223 self.status == STATUS_ACTIVE
224 224 end
225 225
226 226 # Archives the project and its descendants recursively
227 227 def archive
228 228 # Archive subprojects if any
229 229 children.each do |subproject|
230 230 subproject.archive
231 231 end
232 232 update_attribute :status, STATUS_ARCHIVED
233 233 end
234 234
235 235 # Unarchives the project
236 236 # All its ancestors must be active
237 237 def unarchive
238 238 return false if ancestors.detect {|a| !a.active?}
239 239 update_attribute :status, STATUS_ACTIVE
240 240 end
241 241
242 242 # Returns an array of projects the project can be moved to
243 243 # by the current user
244 244 def allowed_parents
245 245 return @allowed_parents if @allowed_parents
246 246 @allowed_parents = (Project.find(:all, :conditions => Project.allowed_to_condition(User.current, :add_project, :member => true)) - self_and_descendants)
247 247 unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent)
248 248 @allowed_parents << parent
249 249 end
250 250 @allowed_parents
251 251 end
252 252
253 253 # Sets the parent of the project with authorization check
254 254 def set_allowed_parent!(p)
255 255 unless p.nil? || p.is_a?(Project)
256 256 if p.to_s.blank?
257 257 p = nil
258 258 else
259 259 p = Project.find_by_id(p)
260 260 return false unless p
261 261 end
262 262 end
263 263 if p.nil?
264 264 if !new_record? && allowed_parents.empty?
265 265 return false
266 266 end
267 267 elsif !allowed_parents.include?(p)
268 268 return false
269 269 end
270 270 set_parent!(p)
271 271 end
272 272
273 273 # Sets the parent of the project
274 274 # Argument can be either a Project, a String, a Fixnum or nil
275 275 def set_parent!(p)
276 276 unless p.nil? || p.is_a?(Project)
277 277 if p.to_s.blank?
278 278 p = nil
279 279 else
280 280 p = Project.find_by_id(p)
281 281 return false unless p
282 282 end
283 283 end
284 284 if p == parent && !p.nil?
285 285 # Nothing to do
286 286 true
287 287 elsif p.nil? || (p.active? && move_possible?(p))
288 288 # Insert the project so that target's children or root projects stay alphabetically sorted
289 289 sibs = (p.nil? ? self.class.roots : p.children)
290 290 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
291 291 if to_be_inserted_before
292 292 move_to_left_of(to_be_inserted_before)
293 293 elsif p.nil?
294 294 if sibs.empty?
295 295 # move_to_root adds the project in first (ie. left) position
296 296 move_to_root
297 297 else
298 298 move_to_right_of(sibs.last) unless self == sibs.last
299 299 end
300 300 else
301 301 # move_to_child_of adds the project in last (ie.right) position
302 302 move_to_child_of(p)
303 303 end
304 304 true
305 305 else
306 306 # Can not move to the given target
307 307 false
308 308 end
309 309 end
310 310
311 311 # Returns an array of the trackers used by the project and its active sub projects
312 312 def rolled_up_trackers
313 313 @rolled_up_trackers ||=
314 314 Tracker.find(:all, :include => :projects,
315 315 :select => "DISTINCT #{Tracker.table_name}.*",
316 316 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
317 317 :order => "#{Tracker.table_name}.position")
318 318 end
319 319
320 320 # Closes open and locked project versions that are completed
321 321 def close_completed_versions
322 322 Version.transaction do
323 323 versions.find(:all, :conditions => {:status => %w(open locked)}).each do |version|
324 324 if version.completed?
325 325 version.update_attribute(:status, 'closed')
326 326 end
327 327 end
328 328 end
329 329 end
330 330
331 331 # Returns a hash of project users grouped by role
332 332 def users_by_role
333 333 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
334 334 m.roles.each do |r|
335 335 h[r] ||= []
336 336 h[r] << m.user
337 337 end
338 338 h
339 339 end
340 340 end
341 341
342 342 # Deletes all project's members
343 343 def delete_all_members
344 344 me, mr = Member.table_name, MemberRole.table_name
345 345 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
346 346 Member.delete_all(['project_id = ?', id])
347 347 end
348 348
349 349 # Users issues can be assigned to
350 350 def assignable_users
351 351 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort
352 352 end
353 353
354 354 # Returns the mail adresses of users that should be always notified on project events
355 355 def recipients
356 356 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
357 357 end
358 358
359 359 # Returns an array of all custom fields enabled for project issues
360 360 # (explictly associated custom fields and custom fields enabled for all projects)
361 361 def all_issue_custom_fields
362 362 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
363 363 end
364 364
365 365 def project
366 366 self
367 367 end
368 368
369 369 def <=>(project)
370 370 name.downcase <=> project.name.downcase
371 371 end
372 372
373 373 def to_s
374 374 name
375 375 end
376 376
377 377 # Returns a short description of the projects (first lines)
378 378 def short_description(length = 255)
379 379 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
380 380 end
381 381
382 382 # Return true if this project is allowed to do the specified action.
383 383 # action can be:
384 384 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
385 385 # * a permission Symbol (eg. :edit_project)
386 386 def allows_to?(action)
387 387 if action.is_a? Hash
388 388 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
389 389 else
390 390 allowed_permissions.include? action
391 391 end
392 392 end
393 393
394 394 def module_enabled?(module_name)
395 395 module_name = module_name.to_s
396 396 enabled_modules.detect {|m| m.name == module_name}
397 397 end
398 398
399 399 def enabled_module_names=(module_names)
400 400 if module_names && module_names.is_a?(Array)
401 401 module_names = module_names.collect(&:to_s)
402 402 # remove disabled modules
403 403 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
404 404 # add new modules
405 405 module_names.reject {|name| module_enabled?(name)}.each {|name| enabled_modules << EnabledModule.new(:name => name)}
406 406 else
407 407 enabled_modules.clear
408 408 end
409 409 end
410 410
411 411 # Returns an auto-generated project identifier based on the last identifier used
412 412 def self.next_identifier
413 413 p = Project.find(:first, :order => 'created_on DESC')
414 414 p.nil? ? nil : p.identifier.to_s.succ
415 415 end
416 416
417 417 # Copies and saves the Project instance based on the +project+.
418 418 # Duplicates the source project's:
419 419 # * Wiki
420 420 # * Versions
421 421 # * Categories
422 422 # * Issues
423 423 # * Members
424 424 # * Queries
425 425 #
426 426 # Accepts an +options+ argument to specify what to copy
427 427 #
428 428 # Examples:
429 429 # project.copy(1) # => copies everything
430 430 # project.copy(1, :only => 'members') # => copies members only
431 431 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
432 432 def copy(project, options={})
433 433 project = project.is_a?(Project) ? project : Project.find(project)
434 434
435 435 to_be_copied = %w(wiki versions issue_categories issues members queries boards)
436 436 to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
437 437
438 438 Project.transaction do
439 439 if save
440 440 reload
441 441 to_be_copied.each do |name|
442 442 send "copy_#{name}", project
443 443 end
444 444 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
445 445 save
446 446 end
447 447 end
448 448 end
449 449
450 450
451 451 # Copies +project+ and returns the new instance. This will not save
452 452 # the copy
453 453 def self.copy_from(project)
454 454 begin
455 455 project = project.is_a?(Project) ? project : Project.find(project)
456 456 if project
457 457 # clear unique attributes
458 458 attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt')
459 459 copy = Project.new(attributes)
460 460 copy.enabled_modules = project.enabled_modules
461 461 copy.trackers = project.trackers
462 462 copy.custom_values = project.custom_values.collect {|v| v.clone}
463 463 copy.issue_custom_fields = project.issue_custom_fields
464 464 return copy
465 465 else
466 466 return nil
467 467 end
468 468 rescue ActiveRecord::RecordNotFound
469 469 return nil
470 470 end
471 471 end
472 472
473 473 private
474 474
475 475 # Copies wiki from +project+
476 476 def copy_wiki(project)
477 477 # Check that the source project has a wiki first
478 478 unless project.wiki.nil?
479 479 self.wiki ||= Wiki.new
480 480 wiki.attributes = project.wiki.attributes.dup.except("id", "project_id")
481 481 project.wiki.pages.each do |page|
482 482 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on"))
483 483 new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id"))
484 484 new_wiki_page.content = new_wiki_content
485 485 wiki.pages << new_wiki_page
486 486 end
487 487 end
488 488 end
489 489
490 490 # Copies versions from +project+
491 491 def copy_versions(project)
492 492 project.versions.each do |version|
493 493 new_version = Version.new
494 494 new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on")
495 495 self.versions << new_version
496 496 end
497 497 end
498 498
499 499 # Copies issue categories from +project+
500 500 def copy_issue_categories(project)
501 501 project.issue_categories.each do |issue_category|
502 502 new_issue_category = IssueCategory.new
503 503 new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id")
504 504 self.issue_categories << new_issue_category
505 505 end
506 506 end
507 507
508 508 # Copies issues from +project+
509 509 def copy_issues(project)
510 510 project.issues.each do |issue|
511 511 new_issue = Issue.new
512 512 new_issue.copy_from(issue)
513 513 # Reassign fixed_versions by name, since names are unique per
514 514 # project and the versions for self are not yet saved
515 515 if issue.fixed_version
516 516 new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first
517 517 end
518 518 # Reassign the category by name, since names are unique per
519 519 # project and the categories for self are not yet saved
520 520 if issue.category
521 521 new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first
522 522 end
523 523 self.issues << new_issue
524 524 end
525 525 end
526 526
527 527 # Copies members from +project+
528 528 def copy_members(project)
529 529 project.members.each do |member|
530 530 new_member = Member.new
531 531 new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on")
532 532 new_member.role_ids = member.role_ids.dup
533 533 new_member.project = self
534 534 self.members << new_member
535 535 end
536 536 end
537 537
538 538 # Copies queries from +project+
539 539 def copy_queries(project)
540 540 project.queries.each do |query|
541 541 new_query = Query.new
542 542 new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria")
543 543 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
544 544 new_query.project = self
545 545 self.queries << new_query
546 546 end
547 547 end
548 548
549 549 # Copies boards from +project+
550 550 def copy_boards(project)
551 551 project.boards.each do |board|
552 552 new_board = Board.new
553 553 new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
554 554 new_board.project = self
555 555 self.boards << new_board
556 556 end
557 557 end
558 558
559 559 def allowed_permissions
560 560 @allowed_permissions ||= begin
561 561 module_names = enabled_modules.collect {|m| m.name}
562 562 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
563 563 end
564 564 end
565 565
566 566 def allowed_actions
567 567 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
568 568 end
569 569
570 570 # Returns all the active Systemwide and project specific activities
571 571 def active_activities
572 572 overridden_activity_ids = self.time_entry_activities.active.collect(&:parent_id)
573 573
574 574 if overridden_activity_ids.empty?
575 575 return TimeEntryActivity.active
576 576 else
577 577 return system_activities_and_project_overrides
578 578 end
579 579 end
580 580
581 581 # Returns all the Systemwide and project specific activities
582 582 # (inactive and active)
583 583 def all_activities
584 584 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
585 585
586 586 if overridden_activity_ids.empty?
587 587 return TimeEntryActivity.all
588 588 else
589 589 return system_activities_and_project_overrides(true)
590 590 end
591 591 end
592 592
593 593 # Returns the systemwide active activities merged with the project specific overrides
594 594 def system_activities_and_project_overrides(include_inactive=false)
595 595 if include_inactive
596 596 return TimeEntryActivity.all.
597 597 find(:all,
598 598 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
599 599 self.time_entry_activities
600 600 else
601 601 return TimeEntryActivity.active.
602 602 find(:all,
603 603 :conditions => ["id NOT IN (?)", self.time_entry_activities.active.collect(&:parent_id)]) +
604 604 self.time_entry_activities.active
605 605 end
606 606 end
607 607 end
General Comments 0
You need to be logged in to leave comments. Login now