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