Mercurial > hg > soundsoftware-site
comparison app/models/project.rb @ 37:94944d00e43c
* Update to SVN trunk rev 4411
author | Chris Cannam <chris.cannam@soundsoftware.ac.uk> |
---|---|
date | Fri, 19 Nov 2010 13:24:41 +0000 |
parents | 40f7cfd4df19 |
children | af80e5618e9b db0caa9f0ff4 |
comparison
equal
deleted
inserted
replaced
22:40f7cfd4df19 | 37:94944d00e43c |
---|---|
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 | |
23 # Maximum length for project identifiers | |
24 IDENTIFIER_MAX_LENGTH = 100 | |
22 | 25 |
23 # Specific overidden Activities | 26 # Specific overidden Activities |
24 has_many :time_entry_activities | 27 has_many :time_entry_activities |
25 has_many :members, :include => [:user, :roles], :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}" | 28 has_many :members, :include => [:user, :roles], :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}" |
26 has_many :memberships, :class_name => 'Member' | 29 has_many :memberships, :class_name => 'Member' |
62 :author => nil | 65 :author => nil |
63 | 66 |
64 attr_protected :status, :enabled_module_names | 67 attr_protected :status, :enabled_module_names |
65 | 68 |
66 validates_presence_of :name, :identifier | 69 validates_presence_of :name, :identifier |
67 validates_uniqueness_of :name, :identifier | 70 validates_uniqueness_of :identifier |
68 validates_associated :repository, :wiki | 71 validates_associated :repository, :wiki |
69 validates_length_of :name, :maximum => 30 | 72 validates_length_of :name, :maximum => 255 |
70 validates_length_of :homepage, :maximum => 255 | 73 validates_length_of :homepage, :maximum => 255 |
71 validates_length_of :identifier, :in => 1..20 | 74 validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH |
72 # donwcase letters, digits, dashes but not digits only | 75 # donwcase letters, digits, dashes but not digits only |
73 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? } | 76 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? } |
74 # reserved words | 77 # reserved words |
75 validates_exclusion_of :identifier, :in => %w( new ) | 78 validates_exclusion_of :identifier, :in => %w( new ) |
76 | 79 |
216 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier) | 219 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier) |
217 end | 220 end |
218 | 221 |
219 def active? | 222 def active? |
220 self.status == STATUS_ACTIVE | 223 self.status == STATUS_ACTIVE |
224 end | |
225 | |
226 def archived? | |
227 self.status == STATUS_ARCHIVED | |
221 end | 228 end |
222 | 229 |
223 # Archives the project and its descendants | 230 # Archives the project and its descendants |
224 def archive | 231 def archive |
225 # Check that there is no issue of a non descendant project that is assigned | 232 # Check that there is no issue of a non descendant project that is assigned |
380 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort | 387 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort |
381 end | 388 end |
382 | 389 |
383 # Returns the mail adresses of users that should be always notified on project events | 390 # Returns the mail adresses of users that should be always notified on project events |
384 def recipients | 391 def recipients |
385 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} | 392 notified_users.collect {|user| user.mail} |
386 end | 393 end |
387 | 394 |
388 # Returns the users that should be notified on project events | 395 # Returns the users that should be notified on project events |
389 def notified_users | 396 def notified_users |
390 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user} | 397 # TODO: User part should be extracted to User#notify_about? |
398 members.select {|m| m.mail_notification? || m.user.mail_notification == 'all'}.collect {|m| m.user} | |
391 end | 399 end |
392 | 400 |
393 # Returns an array of all custom fields enabled for project issues | 401 # Returns an array of all custom fields enabled for project issues |
394 # (explictly associated custom fields and custom fields enabled for all projects) | 402 # (explictly associated custom fields and custom fields enabled for all projects) |
395 def all_issue_custom_fields | 403 def all_issue_custom_fields |
562 end | 570 end |
563 rescue ActiveRecord::RecordNotFound | 571 rescue ActiveRecord::RecordNotFound |
564 return nil | 572 return nil |
565 end | 573 end |
566 end | 574 end |
575 | |
576 # Yields the given block for each project with its level in the tree | |
577 def self.project_tree(projects, &block) | |
578 ancestors = [] | |
579 projects.sort_by(&:lft).each do |project| | |
580 while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) | |
581 ancestors.pop | |
582 end | |
583 yield project, ancestors.size | |
584 ancestors << project | |
585 end | |
586 end | |
567 | 587 |
568 private | 588 private |
569 | 589 |
570 # Destroys children before destroying self | 590 # Destroys children before destroying self |
571 def destroy_children | 591 def destroy_children |