@@ -1,201 +1,202 | |||||
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 :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}" |
|
23 | has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}" | |
24 | has_many :users, :through => :members |
|
24 | has_many :users, :through => :members | |
25 | has_many :custom_values, :dependent => :delete_all, :as => :customized |
|
25 | has_many :custom_values, :dependent => :delete_all, :as => :customized | |
26 | has_many :enabled_modules, :dependent => :delete_all |
|
26 | has_many :enabled_modules, :dependent => :delete_all | |
27 | has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" |
|
27 | has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" | |
28 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] |
|
28 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] | |
29 | has_many :issue_changes, :through => :issues, :source => :journals |
|
29 | has_many :issue_changes, :through => :issues, :source => :journals | |
30 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" |
|
30 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" | |
31 | has_many :time_entries, :dependent => :delete_all |
|
31 | has_many :time_entries, :dependent => :delete_all | |
32 | has_many :queries, :dependent => :delete_all |
|
32 | has_many :queries, :dependent => :delete_all | |
33 | has_many :documents, :dependent => :destroy |
|
33 | has_many :documents, :dependent => :destroy | |
34 | has_many :news, :dependent => :delete_all, :include => :author |
|
34 | has_many :news, :dependent => :delete_all, :include => :author | |
35 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" |
|
35 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" | |
36 | has_many :boards, :order => "position ASC" |
|
36 | has_many :boards, :order => "position ASC" | |
37 | has_one :repository, :dependent => :destroy |
|
37 | has_one :repository, :dependent => :destroy | |
38 | has_many :changesets, :through => :repository |
|
38 | has_many :changesets, :through => :repository | |
39 | has_one :wiki, :dependent => :destroy |
|
39 | has_one :wiki, :dependent => :destroy | |
40 | # Custom field for the project issues |
|
40 | # Custom field for the project issues | |
41 | has_and_belongs_to_many :custom_fields, |
|
41 | has_and_belongs_to_many :custom_fields, | |
42 | :class_name => 'IssueCustomField', |
|
42 | :class_name => 'IssueCustomField', | |
43 | :order => "#{CustomField.table_name}.position", |
|
43 | :order => "#{CustomField.table_name}.position", | |
44 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", |
|
44 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", | |
45 | :association_foreign_key => 'custom_field_id' |
|
45 | :association_foreign_key => 'custom_field_id' | |
46 |
|
46 | |||
47 | acts_as_tree :order => "name", :counter_cache => true |
|
47 | acts_as_tree :order => "name", :counter_cache => true | |
48 |
|
48 | |||
49 | acts_as_searchable :columns => ['name', 'description'], :project_key => 'id' |
|
49 | acts_as_searchable :columns => ['name', 'description'], :project_key => 'id' | |
50 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, |
|
50 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, | |
51 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}} |
|
51 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}} | |
52 |
|
52 | |||
53 | attr_protected :status, :enabled_module_names |
|
53 | attr_protected :status, :enabled_module_names | |
54 |
|
54 | |||
55 | validates_presence_of :name, :description, :identifier |
|
55 | validates_presence_of :name, :description, :identifier | |
56 | validates_uniqueness_of :name, :identifier |
|
56 | validates_uniqueness_of :name, :identifier | |
57 | validates_associated :custom_values, :on => :update |
|
57 | validates_associated :custom_values, :on => :update | |
58 | validates_associated :repository, :wiki |
|
58 | validates_associated :repository, :wiki | |
59 | validates_length_of :name, :maximum => 30 |
|
59 | validates_length_of :name, :maximum => 30 | |
60 | validates_length_of :description, :maximum => 255 |
|
60 | validates_length_of :description, :maximum => 255 | |
61 | validates_length_of :homepage, :maximum => 60 |
|
61 | validates_length_of :homepage, :maximum => 60 | |
62 | validates_length_of :identifier, :in => 3..20 |
|
62 | validates_length_of :identifier, :in => 3..20 | |
63 | validates_format_of :identifier, :with => /^[a-z0-9\-]*$/ |
|
63 | validates_format_of :identifier, :with => /^[a-z0-9\-]*$/ | |
64 |
|
64 | |||
65 | before_destroy :delete_all_members |
|
65 | before_destroy :delete_all_members | |
66 |
|
66 | |||
67 | def identifier=(identifier) |
|
67 | def identifier=(identifier) | |
68 | super unless identifier_frozen? |
|
68 | super unless identifier_frozen? | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | def identifier_frozen? |
|
71 | def identifier_frozen? | |
72 | errors[:identifier].nil? && !(new_record? || identifier.blank?) |
|
72 | errors[:identifier].nil? && !(new_record? || identifier.blank?) | |
73 | end |
|
73 | end | |
74 |
|
74 | |||
75 | def issues_with_subprojects(include_subprojects=false) |
|
75 | def issues_with_subprojects(include_subprojects=false) | |
76 | conditions = nil |
|
76 | conditions = nil | |
77 | if include_subprojects && !active_children.empty? |
|
77 | if include_subprojects && !active_children.empty? | |
78 | ids = [id] + active_children.collect {|c| c.id} |
|
78 | ids = [id] + active_children.collect {|c| c.id} | |
79 | conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"] |
|
79 | conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"] | |
80 | end |
|
80 | end | |
81 | conditions ||= ["#{Issue.table_name}.project_id = ?", id] |
|
81 | conditions ||= ["#{Issue.table_name}.project_id = ?", id] | |
82 | Issue.with_scope :find => { :conditions => conditions } do |
|
82 | # Quick and dirty fix for Rails 2 compatibility | |
|
83 | Issue.send(:with_scope, :find => { :conditions => conditions }) do | |||
83 | yield |
|
84 | yield | |
84 | end |
|
85 | end | |
85 | end |
|
86 | end | |
86 |
|
87 | |||
87 | # Return all issues status changes for the project between the 2 given dates |
|
88 | # Return all issues status changes for the project between the 2 given dates | |
88 | def issues_status_changes(from, to) |
|
89 | def issues_status_changes(from, to) | |
89 | Journal.find(:all, :include => [:issue, :details, :user], |
|
90 | Journal.find(:all, :include => [:issue, :details, :user], | |
90 | :conditions => ["#{Journal.table_name}.journalized_type = 'Issue'" + |
|
91 | :conditions => ["#{Journal.table_name}.journalized_type = 'Issue'" + | |
91 | " AND #{Issue.table_name}.project_id = ?" + |
|
92 | " AND #{Issue.table_name}.project_id = ?" + | |
92 | " AND #{JournalDetail.table_name}.prop_key = 'status_id'" + |
|
93 | " AND #{JournalDetail.table_name}.prop_key = 'status_id'" + | |
93 | " AND #{Journal.table_name}.created_on BETWEEN ? AND ?", |
|
94 | " AND #{Journal.table_name}.created_on BETWEEN ? AND ?", | |
94 | id, from, to+1]) |
|
95 | id, from, to+1]) | |
95 | end |
|
96 | end | |
96 |
|
97 | |||
97 | # returns latest created projects |
|
98 | # returns latest created projects | |
98 | # non public projects will be returned only if user is a member of those |
|
99 | # non public projects will be returned only if user is a member of those | |
99 | def self.latest(user=nil, count=5) |
|
100 | def self.latest(user=nil, count=5) | |
100 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") |
|
101 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") | |
101 | end |
|
102 | end | |
102 |
|
103 | |||
103 | def self.visible_by(user=nil) |
|
104 | def self.visible_by(user=nil) | |
104 | if user && user.admin? |
|
105 | if user && user.admin? | |
105 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
106 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" | |
106 | elsif user && user.memberships.any? |
|
107 | elsif user && user.memberships.any? | |
107 | 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(',')}))" |
|
108 | 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(',')}))" | |
108 | else |
|
109 | else | |
109 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}" |
|
110 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}" | |
110 | end |
|
111 | end | |
111 | end |
|
112 | end | |
112 |
|
113 | |||
113 | def active? |
|
114 | def active? | |
114 | self.status == STATUS_ACTIVE |
|
115 | self.status == STATUS_ACTIVE | |
115 | end |
|
116 | end | |
116 |
|
117 | |||
117 | def archive |
|
118 | def archive | |
118 | # Archive subprojects if any |
|
119 | # Archive subprojects if any | |
119 | children.each do |subproject| |
|
120 | children.each do |subproject| | |
120 | subproject.archive |
|
121 | subproject.archive | |
121 | end |
|
122 | end | |
122 | update_attribute :status, STATUS_ARCHIVED |
|
123 | update_attribute :status, STATUS_ARCHIVED | |
123 | end |
|
124 | end | |
124 |
|
125 | |||
125 | def unarchive |
|
126 | def unarchive | |
126 | return false if parent && !parent.active? |
|
127 | return false if parent && !parent.active? | |
127 | update_attribute :status, STATUS_ACTIVE |
|
128 | update_attribute :status, STATUS_ACTIVE | |
128 | end |
|
129 | end | |
129 |
|
130 | |||
130 | def active_children |
|
131 | def active_children | |
131 | children.select {|child| child.active?} |
|
132 | children.select {|child| child.active?} | |
132 | end |
|
133 | end | |
133 |
|
134 | |||
134 | # Deletes all project's members |
|
135 | # Deletes all project's members | |
135 | def delete_all_members |
|
136 | def delete_all_members | |
136 | Member.delete_all(['project_id = ?', id]) |
|
137 | Member.delete_all(['project_id = ?', id]) | |
137 | end |
|
138 | end | |
138 |
|
139 | |||
139 | # Users issues can be assigned to |
|
140 | # Users issues can be assigned to | |
140 | def assignable_users |
|
141 | def assignable_users | |
141 | members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort |
|
142 | members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort | |
142 | end |
|
143 | end | |
143 |
|
144 | |||
144 | # Returns the mail adresses of users that should be always notified on project events |
|
145 | # Returns the mail adresses of users that should be always notified on project events | |
145 | def recipients |
|
146 | def recipients | |
146 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} |
|
147 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} | |
147 | end |
|
148 | end | |
148 |
|
149 | |||
149 | # Returns an array of all custom fields enabled for project issues |
|
150 | # Returns an array of all custom fields enabled for project issues | |
150 | # (explictly associated custom fields and custom fields enabled for all projects) |
|
151 | # (explictly associated custom fields and custom fields enabled for all projects) | |
151 | def custom_fields_for_issues(tracker) |
|
152 | def custom_fields_for_issues(tracker) | |
152 | all_custom_fields.select {|c| tracker.custom_fields.include? c } |
|
153 | all_custom_fields.select {|c| tracker.custom_fields.include? c } | |
153 | end |
|
154 | end | |
154 |
|
155 | |||
155 | def all_custom_fields |
|
156 | def all_custom_fields | |
156 | @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq |
|
157 | @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq | |
157 | end |
|
158 | end | |
158 |
|
159 | |||
159 | def <=>(project) |
|
160 | def <=>(project) | |
160 | name.downcase <=> project.name.downcase |
|
161 | name.downcase <=> project.name.downcase | |
161 | end |
|
162 | end | |
162 |
|
163 | |||
163 | def allows_to?(action) |
|
164 | def allows_to?(action) | |
164 | if action.is_a? Hash |
|
165 | if action.is_a? Hash | |
165 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
166 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" | |
166 | else |
|
167 | else | |
167 | allowed_permissions.include? action |
|
168 | allowed_permissions.include? action | |
168 | end |
|
169 | end | |
169 | end |
|
170 | end | |
170 |
|
171 | |||
171 | def module_enabled?(module_name) |
|
172 | def module_enabled?(module_name) | |
172 | module_name = module_name.to_s |
|
173 | module_name = module_name.to_s | |
173 | enabled_modules.detect {|m| m.name == module_name} |
|
174 | enabled_modules.detect {|m| m.name == module_name} | |
174 | end |
|
175 | end | |
175 |
|
176 | |||
176 | def enabled_module_names=(module_names) |
|
177 | def enabled_module_names=(module_names) | |
177 | enabled_modules.clear |
|
178 | enabled_modules.clear | |
178 | module_names = [] unless module_names && module_names.is_a?(Array) |
|
179 | module_names = [] unless module_names && module_names.is_a?(Array) | |
179 | module_names.each do |name| |
|
180 | module_names.each do |name| | |
180 | enabled_modules << EnabledModule.new(:name => name.to_s) |
|
181 | enabled_modules << EnabledModule.new(:name => name.to_s) | |
181 | end |
|
182 | end | |
182 | end |
|
183 | end | |
183 |
|
184 | |||
184 | protected |
|
185 | protected | |
185 | def validate |
|
186 | def validate | |
186 | errors.add(parent_id, " must be a root project") if parent and parent.parent |
|
187 | errors.add(parent_id, " must be a root project") if parent and parent.parent | |
187 | errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0 |
|
188 | errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0 | |
188 | end |
|
189 | end | |
189 |
|
190 | |||
190 | private |
|
191 | private | |
191 | def allowed_permissions |
|
192 | def allowed_permissions | |
192 | @allowed_permissions ||= begin |
|
193 | @allowed_permissions ||= begin | |
193 | module_names = enabled_modules.collect {|m| m.name} |
|
194 | module_names = enabled_modules.collect {|m| m.name} | |
194 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} |
|
195 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} | |
195 | end |
|
196 | end | |
196 | end |
|
197 | end | |
197 |
|
198 | |||
198 | def allowed_actions |
|
199 | def allowed_actions | |
199 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
200 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten | |
200 | end |
|
201 | end | |
201 | end |
|
202 | end |
General Comments 0
You need to be logged in to leave comments.
Login now