##// END OF EJS Templates
Fixed an error when trying to delete a project (versions/issues dependency)...
Jean-Philippe Lang -
r571:7eb3b186c4f2
parent child
Show More
@@ -1,123 +1,123
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 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
24 23 has_many :members, :dependent => :delete_all, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
25 24 has_many :users, :through => :members
26 25 has_many :custom_values, :dependent => :delete_all, :as => :customized
27 26 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
27 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
28 28 has_many :time_entries, :dependent => :delete_all
29 29 has_many :queries, :dependent => :delete_all
30 30 has_many :documents, :dependent => :destroy
31 31 has_many :news, :dependent => :delete_all, :include => :author
32 32 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
33 33 has_many :boards, :order => "position ASC"
34 34 has_one :repository, :dependent => :destroy
35 35 has_one :wiki, :dependent => :destroy
36 36 has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", :association_foreign_key => 'custom_field_id'
37 37 acts_as_tree :order => "name", :counter_cache => true
38 38
39 39 attr_protected :status
40 40
41 41 validates_presence_of :name, :description, :identifier
42 42 validates_uniqueness_of :name, :identifier
43 43 validates_associated :custom_values, :on => :update
44 44 validates_associated :repository, :wiki
45 45 validates_length_of :name, :maximum => 30
46 46 validates_format_of :name, :with => /^[\w\s\'\-]*$/i
47 47 validates_length_of :description, :maximum => 255
48 48 validates_length_of :identifier, :in => 3..12
49 49 validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
50 50
51 51 def identifier=(identifier)
52 52 super unless identifier_frozen?
53 53 end
54 54
55 55 def identifier_frozen?
56 56 errors[:identifier].nil? && !(new_record? || identifier.blank?)
57 57 end
58 58
59 59 def issues_with_subprojects(include_subprojects=false)
60 60 conditions = nil
61 61 if include_subprojects && !active_children.empty?
62 62 ids = [id] + active_children.collect {|c| c.id}
63 63 conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"]
64 64 end
65 65 conditions ||= ["#{Issue.table_name}.project_id = ?", id]
66 66 Issue.with_scope :find => { :conditions => conditions } do
67 67 yield
68 68 end
69 69 end
70 70
71 71 # returns latest created projects
72 72 # non public projects will be returned only if user is a member of those
73 73 def self.latest(user=nil, count=5)
74 74 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
75 75 end
76 76
77 77 def self.visible_by(user=nil)
78 78 if user && user.admin?
79 79 return ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
80 80 elsif user && user.memberships.any?
81 81 return ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = ? or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))", true]
82 82 else
83 83 return ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = ?", true]
84 84 end
85 85 end
86 86
87 87 def active?
88 88 self.status == STATUS_ACTIVE
89 89 end
90 90
91 91 def archive
92 92 # Archive subprojects if any
93 93 children.each do |subproject|
94 94 subproject.archive
95 95 end
96 96 update_attribute :status, STATUS_ARCHIVED
97 97 end
98 98
99 99 def unarchive
100 100 return false if parent && !parent.active?
101 101 update_attribute :status, STATUS_ACTIVE
102 102 end
103 103
104 104 def active_children
105 105 children.select {|child| child.active?}
106 106 end
107 107
108 108 # Returns an array of all custom fields enabled for project issues
109 109 # (explictly associated custom fields and custom fields enabled for all projects)
110 110 def custom_fields_for_issues(tracker)
111 111 all_custom_fields.select {|c| tracker.custom_fields.include? c }
112 112 end
113 113
114 114 def all_custom_fields
115 115 @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq
116 116 end
117 117
118 118 protected
119 119 def validate
120 120 errors.add(parent_id, " must be a root project") if parent and parent.parent
121 121 errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
122 122 end
123 123 end
General Comments 0
You need to be logged in to leave comments. Login now