@@ -1,419 +1,415 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2009 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 ProjectsController < ApplicationController |
|
18 | class ProjectsController < ApplicationController | |
19 | menu_item :overview |
|
19 | menu_item :overview | |
20 | menu_item :activity, :only => :activity |
|
20 | menu_item :activity, :only => :activity | |
21 | menu_item :roadmap, :only => :roadmap |
|
21 | menu_item :roadmap, :only => :roadmap | |
22 | menu_item :files, :only => [:list_files, :add_file] |
|
22 | menu_item :files, :only => [:list_files, :add_file] | |
23 | menu_item :settings, :only => :settings |
|
23 | menu_item :settings, :only => :settings | |
24 |
|
24 | |||
25 | before_filter :find_project, :except => [ :index, :list, :add, :copy, :activity ] |
|
25 | before_filter :find_project, :except => [ :index, :list, :add, :copy, :activity ] | |
26 | before_filter :find_optional_project, :only => :activity |
|
26 | before_filter :find_optional_project, :only => :activity | |
27 | before_filter :authorize, :except => [ :index, :list, :add, :copy, :archive, :unarchive, :destroy, :activity ] |
|
27 | before_filter :authorize, :except => [ :index, :list, :add, :copy, :archive, :unarchive, :destroy, :activity ] | |
28 | before_filter :authorize_global, :only => :add |
|
28 | before_filter :authorize_global, :only => :add | |
29 | before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] |
|
29 | before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] | |
30 | accept_key_auth :activity |
|
30 | accept_key_auth :activity | |
31 |
|
31 | |||
32 | after_filter :only => [:add, :edit, :archive, :unarchive, :destroy] do |controller| |
|
32 | after_filter :only => [:add, :edit, :archive, :unarchive, :destroy] do |controller| | |
33 | if controller.request.post? |
|
33 | if controller.request.post? | |
34 | controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt' |
|
34 | controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt' | |
35 | end |
|
35 | end | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | helper :sort |
|
38 | helper :sort | |
39 | include SortHelper |
|
39 | include SortHelper | |
40 | helper :custom_fields |
|
40 | helper :custom_fields | |
41 | include CustomFieldsHelper |
|
41 | include CustomFieldsHelper | |
42 | helper :issues |
|
42 | helper :issues | |
43 | helper IssuesHelper |
|
43 | helper IssuesHelper | |
44 | helper :queries |
|
44 | helper :queries | |
45 | include QueriesHelper |
|
45 | include QueriesHelper | |
46 | helper :repositories |
|
46 | helper :repositories | |
47 | include RepositoriesHelper |
|
47 | include RepositoriesHelper | |
48 | include ProjectsHelper |
|
48 | include ProjectsHelper | |
49 |
|
49 | |||
50 | # Lists visible projects |
|
50 | # Lists visible projects | |
51 | def index |
|
51 | def index | |
52 | respond_to do |format| |
|
52 | respond_to do |format| | |
53 | format.html { |
|
53 | format.html { | |
54 | @projects = Project.visible.find(:all, :order => 'lft') |
|
54 | @projects = Project.visible.find(:all, :order => 'lft') | |
55 | } |
|
55 | } | |
56 | format.atom { |
|
56 | format.atom { | |
57 | projects = Project.visible.find(:all, :order => 'created_on DESC', |
|
57 | projects = Project.visible.find(:all, :order => 'created_on DESC', | |
58 | :limit => Setting.feeds_limit.to_i) |
|
58 | :limit => Setting.feeds_limit.to_i) | |
59 | render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") |
|
59 | render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") | |
60 | } |
|
60 | } | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | # Add a new project |
|
64 | # Add a new project | |
65 | def add |
|
65 | def add | |
66 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
|
66 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") | |
67 | @trackers = Tracker.all |
|
67 | @trackers = Tracker.all | |
68 | @project = Project.new(params[:project]) |
|
68 | @project = Project.new(params[:project]) | |
69 | if request.get? |
|
69 | if request.get? | |
70 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? |
|
70 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? | |
71 | @project.trackers = Tracker.all |
|
71 | @project.trackers = Tracker.all | |
72 | @project.is_public = Setting.default_projects_public? |
|
72 | @project.is_public = Setting.default_projects_public? | |
73 | @project.enabled_module_names = Setting.default_projects_modules |
|
73 | @project.enabled_module_names = Setting.default_projects_modules | |
74 | else |
|
74 | else | |
75 | @project.enabled_module_names = params[:enabled_modules] |
|
75 | @project.enabled_module_names = params[:enabled_modules] | |
76 | if validate_parent_id && @project.save |
|
76 | if validate_parent_id && @project.save | |
77 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
77 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') | |
78 | # Add current user as a project member if he is not admin |
|
78 | # Add current user as a project member if he is not admin | |
79 | unless User.current.admin? |
|
79 | unless User.current.admin? | |
80 | r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first |
|
80 | r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first | |
81 | m = Member.new(:user => User.current, :roles => [r]) |
|
81 | m = Member.new(:user => User.current, :roles => [r]) | |
82 | @project.members << m |
|
82 | @project.members << m | |
83 | end |
|
83 | end | |
84 | flash[:notice] = l(:notice_successful_create) |
|
84 | flash[:notice] = l(:notice_successful_create) | |
85 | redirect_to :controller => 'projects', :action => 'settings', :id => @project |
|
85 | redirect_to :controller => 'projects', :action => 'settings', :id => @project | |
86 | end |
|
86 | end | |
87 | end |
|
87 | end | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | def copy |
|
90 | def copy | |
91 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
|
91 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") | |
92 | @trackers = Tracker.all |
|
92 | @trackers = Tracker.all | |
93 | @root_projects = Project.find(:all, |
|
93 | @root_projects = Project.find(:all, | |
94 | :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}", |
|
94 | :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}", | |
95 | :order => 'name') |
|
95 | :order => 'name') | |
96 | @source_project = Project.find(params[:id]) |
|
96 | @source_project = Project.find(params[:id]) | |
97 | if request.get? |
|
97 | if request.get? | |
98 | @project = Project.copy_from(@source_project) |
|
98 | @project = Project.copy_from(@source_project) | |
99 | if @project |
|
99 | if @project | |
100 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? |
|
100 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? | |
101 | else |
|
101 | else | |
102 | redirect_to :controller => 'admin', :action => 'projects' |
|
102 | redirect_to :controller => 'admin', :action => 'projects' | |
103 | end |
|
103 | end | |
104 | else |
|
104 | else | |
105 | @project = Project.new(params[:project]) |
|
105 | @project = Project.new(params[:project]) | |
106 | @project.enabled_module_names = params[:enabled_modules] |
|
106 | @project.enabled_module_names = params[:enabled_modules] | |
107 | if validate_parent_id && @project.copy(@source_project, :only => params[:only]) |
|
107 | if validate_parent_id && @project.copy(@source_project, :only => params[:only]) | |
108 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
108 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') | |
109 | flash[:notice] = l(:notice_successful_create) |
|
109 | flash[:notice] = l(:notice_successful_create) | |
110 | redirect_to :controller => 'admin', :action => 'projects' |
|
110 | redirect_to :controller => 'admin', :action => 'projects' | |
111 | elsif !@project.new_record? |
|
111 | elsif !@project.new_record? | |
112 | # Project was created |
|
112 | # Project was created | |
113 | # But some objects were not copied due to validation failures |
|
113 | # But some objects were not copied due to validation failures | |
114 | # (eg. issues from disabled trackers) |
|
114 | # (eg. issues from disabled trackers) | |
115 | # TODO: inform about that |
|
115 | # TODO: inform about that | |
116 | redirect_to :controller => 'admin', :action => 'projects' |
|
116 | redirect_to :controller => 'admin', :action => 'projects' | |
117 | end |
|
117 | end | |
118 | end |
|
118 | end | |
119 | rescue ActiveRecord::RecordNotFound |
|
119 | rescue ActiveRecord::RecordNotFound | |
120 | redirect_to :controller => 'admin', :action => 'projects' |
|
120 | redirect_to :controller => 'admin', :action => 'projects' | |
121 | end |
|
121 | end | |
122 |
|
122 | |||
123 | # Show @project |
|
123 | # Show @project | |
124 | def show |
|
124 | def show | |
125 | if params[:jump] |
|
125 | if params[:jump] | |
126 | # try to redirect to the requested menu item |
|
126 | # try to redirect to the requested menu item | |
127 | redirect_to_project_menu_item(@project, params[:jump]) && return |
|
127 | redirect_to_project_menu_item(@project, params[:jump]) && return | |
128 | end |
|
128 | end | |
129 |
|
129 | |||
130 | @users_by_role = @project.users_by_role |
|
130 | @users_by_role = @project.users_by_role | |
131 | @subprojects = @project.children.visible |
|
131 | @subprojects = @project.children.visible | |
132 | @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") |
|
132 | @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") | |
133 | @trackers = @project.rolled_up_trackers |
|
133 | @trackers = @project.rolled_up_trackers | |
134 |
|
134 | |||
135 | cond = @project.project_condition(Setting.display_subprojects_issues?) |
|
135 | cond = @project.project_condition(Setting.display_subprojects_issues?) | |
136 |
|
136 | |||
137 | @open_issues_by_tracker = Issue.visible.count(:group => :tracker, |
|
137 | @open_issues_by_tracker = Issue.visible.count(:group => :tracker, | |
138 | :include => [:project, :status, :tracker], |
|
138 | :include => [:project, :status, :tracker], | |
139 | :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false]) |
|
139 | :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false]) | |
140 | @total_issues_by_tracker = Issue.visible.count(:group => :tracker, |
|
140 | @total_issues_by_tracker = Issue.visible.count(:group => :tracker, | |
141 | :include => [:project, :status, :tracker], |
|
141 | :include => [:project, :status, :tracker], | |
142 | :conditions => cond) |
|
142 | :conditions => cond) | |
143 |
|
143 | |||
144 | TimeEntry.visible_by(User.current) do |
|
144 | TimeEntry.visible_by(User.current) do | |
145 | @total_hours = TimeEntry.sum(:hours, |
|
145 | @total_hours = TimeEntry.sum(:hours, | |
146 | :include => :project, |
|
146 | :include => :project, | |
147 | :conditions => cond).to_f |
|
147 | :conditions => cond).to_f | |
148 | end |
|
148 | end | |
149 | @key = User.current.rss_key |
|
149 | @key = User.current.rss_key | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | def settings |
|
152 | def settings | |
153 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
|
153 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") | |
154 | @issue_category ||= IssueCategory.new |
|
154 | @issue_category ||= IssueCategory.new | |
155 | @member ||= @project.members.new |
|
155 | @member ||= @project.members.new | |
156 | @trackers = Tracker.all |
|
156 | @trackers = Tracker.all | |
157 | @repository ||= @project.repository |
|
157 | @repository ||= @project.repository | |
158 | @wiki ||= @project.wiki |
|
158 | @wiki ||= @project.wiki | |
159 | end |
|
159 | end | |
160 |
|
160 | |||
161 | # Edit @project |
|
161 | # Edit @project | |
162 | def edit |
|
162 | def edit | |
163 | if request.post? |
|
163 | if request.post? | |
164 | @project.attributes = params[:project] |
|
164 | @project.attributes = params[:project] | |
165 | if validate_parent_id && @project.save |
|
165 | if validate_parent_id && @project.save | |
166 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
166 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') | |
167 | flash[:notice] = l(:notice_successful_update) |
|
167 | flash[:notice] = l(:notice_successful_update) | |
168 | redirect_to :action => 'settings', :id => @project |
|
168 | redirect_to :action => 'settings', :id => @project | |
169 | else |
|
169 | else | |
170 | settings |
|
170 | settings | |
171 | render :action => 'settings' |
|
171 | render :action => 'settings' | |
172 | end |
|
172 | end | |
173 | end |
|
173 | end | |
174 | end |
|
174 | end | |
175 |
|
175 | |||
176 | def modules |
|
176 | def modules | |
177 | @project.enabled_module_names = params[:enabled_modules] |
|
177 | @project.enabled_module_names = params[:enabled_modules] | |
178 | redirect_to :action => 'settings', :id => @project, :tab => 'modules' |
|
178 | redirect_to :action => 'settings', :id => @project, :tab => 'modules' | |
179 | end |
|
179 | end | |
180 |
|
180 | |||
181 | def archive |
|
181 | def archive | |
182 | if request.post? |
|
182 | if request.post? | |
183 | unless @project.archive |
|
183 | unless @project.archive | |
184 | flash[:error] = l(:error_can_not_archive_project) |
|
184 | flash[:error] = l(:error_can_not_archive_project) | |
185 | end |
|
185 | end | |
186 | end |
|
186 | end | |
187 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) |
|
187 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) | |
188 | end |
|
188 | end | |
189 |
|
189 | |||
190 | def unarchive |
|
190 | def unarchive | |
191 | @project.unarchive if request.post? && !@project.active? |
|
191 | @project.unarchive if request.post? && !@project.active? | |
192 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) |
|
192 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) | |
193 | end |
|
193 | end | |
194 |
|
194 | |||
195 | # Delete @project |
|
195 | # Delete @project | |
196 | def destroy |
|
196 | def destroy | |
197 | @project_to_destroy = @project |
|
197 | @project_to_destroy = @project | |
198 | if request.post? and params[:confirm] |
|
198 | if request.post? and params[:confirm] | |
199 | @project_to_destroy.destroy |
|
199 | @project_to_destroy.destroy | |
200 | redirect_to :controller => 'admin', :action => 'projects' |
|
200 | redirect_to :controller => 'admin', :action => 'projects' | |
201 | end |
|
201 | end | |
202 | # hide project in layout |
|
202 | # hide project in layout | |
203 | @project = nil |
|
203 | @project = nil | |
204 | end |
|
204 | end | |
205 |
|
205 | |||
206 | # Add a new issue category to @project |
|
206 | # Add a new issue category to @project | |
207 | def add_issue_category |
|
207 | def add_issue_category | |
208 | @category = @project.issue_categories.build(params[:category]) |
|
208 | @category = @project.issue_categories.build(params[:category]) | |
209 | if request.post? |
|
209 | if request.post? | |
210 | if @category.save |
|
210 | if @category.save | |
211 | respond_to do |format| |
|
211 | respond_to do |format| | |
212 | format.html do |
|
212 | format.html do | |
213 | flash[:notice] = l(:notice_successful_create) |
|
213 | flash[:notice] = l(:notice_successful_create) | |
214 | redirect_to :action => 'settings', :tab => 'categories', :id => @project |
|
214 | redirect_to :action => 'settings', :tab => 'categories', :id => @project | |
215 | end |
|
215 | end | |
216 | format.js do |
|
216 | format.js do | |
217 | # IE doesn't support the replace_html rjs method for select box options |
|
217 | # IE doesn't support the replace_html rjs method for select box options | |
218 | render(:update) {|page| page.replace "issue_category_id", |
|
218 | render(:update) {|page| page.replace "issue_category_id", | |
219 | content_tag('select', '<option></option>' + options_from_collection_for_select(@project.issue_categories, 'id', 'name', @category.id), :id => 'issue_category_id', :name => 'issue[category_id]') |
|
219 | content_tag('select', '<option></option>' + options_from_collection_for_select(@project.issue_categories, 'id', 'name', @category.id), :id => 'issue_category_id', :name => 'issue[category_id]') | |
220 | } |
|
220 | } | |
221 | end |
|
221 | end | |
222 | end |
|
222 | end | |
223 | else |
|
223 | else | |
224 | respond_to do |format| |
|
224 | respond_to do |format| | |
225 | format.html |
|
225 | format.html | |
226 | format.js do |
|
226 | format.js do | |
227 | render(:update) {|page| page.alert(@category.errors.full_messages.join('\n')) } |
|
227 | render(:update) {|page| page.alert(@category.errors.full_messages.join('\n')) } | |
228 | end |
|
228 | end | |
229 | end |
|
229 | end | |
230 | end |
|
230 | end | |
231 | end |
|
231 | end | |
232 | end |
|
232 | end | |
233 |
|
233 | |||
234 | # Add a new version to @project |
|
234 | # Add a new version to @project | |
235 | def add_version |
|
235 | def add_version | |
236 | @version = @project.versions.build |
|
236 | @version = @project.versions.build | |
237 | if params[:version] |
|
237 | if params[:version] | |
238 | attributes = params[:version].dup |
|
238 | attributes = params[:version].dup | |
239 | attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing']) |
|
239 | attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing']) | |
240 | @version.attributes = attributes |
|
240 | @version.attributes = attributes | |
241 | end |
|
241 | end | |
242 | if request.post? |
|
242 | if request.post? | |
243 | if @version.save |
|
243 | if @version.save | |
244 | respond_to do |format| |
|
244 | respond_to do |format| | |
245 | format.html do |
|
245 | format.html do | |
246 | flash[:notice] = l(:notice_successful_create) |
|
246 | flash[:notice] = l(:notice_successful_create) | |
247 | redirect_to :action => 'settings', :tab => 'versions', :id => @project |
|
247 | redirect_to :action => 'settings', :tab => 'versions', :id => @project | |
248 | end |
|
248 | end | |
249 | format.js do |
|
249 | format.js do | |
250 | # IE doesn't support the replace_html rjs method for select box options |
|
250 | # IE doesn't support the replace_html rjs method for select box options | |
251 | render(:update) {|page| page.replace "issue_fixed_version_id", |
|
251 | render(:update) {|page| page.replace "issue_fixed_version_id", | |
252 | content_tag('select', '<option></option>' + version_options_for_select(@project.shared_versions.open, @version), :id => 'issue_fixed_version_id', :name => 'issue[fixed_version_id]') |
|
252 | content_tag('select', '<option></option>' + version_options_for_select(@project.shared_versions.open, @version), :id => 'issue_fixed_version_id', :name => 'issue[fixed_version_id]') | |
253 | } |
|
253 | } | |
254 | end |
|
254 | end | |
255 | end |
|
255 | end | |
256 | else |
|
256 | else | |
257 | respond_to do |format| |
|
257 | respond_to do |format| | |
258 | format.html |
|
258 | format.html | |
259 | format.js do |
|
259 | format.js do | |
260 | render(:update) {|page| page.alert(@version.errors.full_messages.join('\n')) } |
|
260 | render(:update) {|page| page.alert(@version.errors.full_messages.join('\n')) } | |
261 | end |
|
261 | end | |
262 | end |
|
262 | end | |
263 | end |
|
263 | end | |
264 | end |
|
264 | end | |
265 | end |
|
265 | end | |
266 |
|
266 | |||
267 | def add_file |
|
267 | def add_file | |
268 | if request.post? |
|
268 | if request.post? | |
269 | container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id])) |
|
269 | container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id])) | |
270 | attachments = attach_files(container, params[:attachments]) |
|
270 | attachments = attach_files(container, params[:attachments]) | |
271 | if !attachments.empty? && Setting.notified_events.include?('file_added') |
|
271 | if !attachments.empty? && Setting.notified_events.include?('file_added') | |
272 | Mailer.deliver_attachments_added(attachments) |
|
272 | Mailer.deliver_attachments_added(attachments) | |
273 | end |
|
273 | end | |
274 | redirect_to :controller => 'projects', :action => 'list_files', :id => @project |
|
274 | redirect_to :controller => 'projects', :action => 'list_files', :id => @project | |
275 | return |
|
275 | return | |
276 | end |
|
276 | end | |
277 | @versions = @project.versions.sort |
|
277 | @versions = @project.versions.sort | |
278 | end |
|
278 | end | |
279 |
|
279 | |||
280 | def save_activities |
|
280 | def save_activities | |
281 | if request.post? && params[:enumerations] |
|
281 | if request.post? && params[:enumerations] | |
282 | Project.transaction do |
|
282 | Project.transaction do | |
283 | params[:enumerations].each do |id, activity| |
|
283 | params[:enumerations].each do |id, activity| | |
284 | @project.update_or_create_time_entry_activity(id, activity) |
|
284 | @project.update_or_create_time_entry_activity(id, activity) | |
285 | end |
|
285 | end | |
286 | end |
|
286 | end | |
287 | end |
|
287 | end | |
288 |
|
288 | |||
289 | redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project |
|
289 | redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project | |
290 | end |
|
290 | end | |
291 |
|
291 | |||
292 | def reset_activities |
|
292 | def reset_activities | |
293 | @project.time_entry_activities.each do |time_entry_activity| |
|
293 | @project.time_entry_activities.each do |time_entry_activity| | |
294 | time_entry_activity.destroy(time_entry_activity.parent) |
|
294 | time_entry_activity.destroy(time_entry_activity.parent) | |
295 | end |
|
295 | end | |
296 | redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project |
|
296 | redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project | |
297 | end |
|
297 | end | |
298 |
|
298 | |||
299 | def list_files |
|
299 | def list_files | |
300 | sort_init 'filename', 'asc' |
|
300 | sort_init 'filename', 'asc' | |
301 | sort_update 'filename' => "#{Attachment.table_name}.filename", |
|
301 | sort_update 'filename' => "#{Attachment.table_name}.filename", | |
302 | 'created_on' => "#{Attachment.table_name}.created_on", |
|
302 | 'created_on' => "#{Attachment.table_name}.created_on", | |
303 | 'size' => "#{Attachment.table_name}.filesize", |
|
303 | 'size' => "#{Attachment.table_name}.filesize", | |
304 | 'downloads' => "#{Attachment.table_name}.downloads" |
|
304 | 'downloads' => "#{Attachment.table_name}.downloads" | |
305 |
|
305 | |||
306 | @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)] |
|
306 | @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)] | |
307 | @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse |
|
307 | @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse | |
308 | render :layout => !request.xhr? |
|
308 | render :layout => !request.xhr? | |
309 | end |
|
309 | end | |
310 |
|
310 | |||
311 | def roadmap |
|
311 | def roadmap | |
312 | @trackers = @project.trackers.find(:all, :order => 'position') |
|
312 | @trackers = @project.trackers.find(:all, :order => 'position') | |
313 | retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?}) |
|
313 | retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?}) | |
314 | @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') |
|
314 | @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') | |
315 | project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id] |
|
315 | project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id] | |
316 |
|
316 | |||
317 | @versions = @project.shared_versions.sort |
|
317 | @versions = @project.shared_versions.sort | |
318 | @versions.reject! {|version| version.closed? || version.completed? } unless params[:completed] |
|
318 | @versions.reject! {|version| version.closed? || version.completed? } unless params[:completed] | |
319 |
|
319 | |||
320 | @issues_by_version = {} |
|
320 | @issues_by_version = {} | |
321 | unless @selected_tracker_ids.empty? |
|
321 | unless @selected_tracker_ids.empty? | |
322 | @versions.each do |version| |
|
322 | @versions.each do |version| | |
323 | conditions = {:tracker_id => @selected_tracker_ids} |
|
|||
324 | if !@project.versions.include?(version) |
|
|||
325 | conditions.merge!(:project_id => project_ids) |
|
|||
326 | end |
|
|||
327 | issues = version.fixed_issues.visible.find(:all, |
|
323 | issues = version.fixed_issues.visible.find(:all, | |
328 | :include => [:project, :status, :tracker, :priority], |
|
324 | :include => [:project, :status, :tracker, :priority], | |
329 |
:conditions => |
|
325 | :conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids}, | |
330 | :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id") |
|
326 | :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id") | |
331 | @issues_by_version[version] = issues |
|
327 | @issues_by_version[version] = issues | |
332 | end |
|
328 | end | |
333 | end |
|
329 | end | |
334 | @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].empty?} |
|
330 | @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].empty?} | |
335 | end |
|
331 | end | |
336 |
|
332 | |||
337 | def activity |
|
333 | def activity | |
338 | @days = Setting.activity_days_default.to_i |
|
334 | @days = Setting.activity_days_default.to_i | |
339 |
|
335 | |||
340 | if params[:from] |
|
336 | if params[:from] | |
341 | begin; @date_to = params[:from].to_date + 1; rescue; end |
|
337 | begin; @date_to = params[:from].to_date + 1; rescue; end | |
342 | end |
|
338 | end | |
343 |
|
339 | |||
344 | @date_to ||= Date.today + 1 |
|
340 | @date_to ||= Date.today + 1 | |
345 | @date_from = @date_to - @days |
|
341 | @date_from = @date_to - @days | |
346 | @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') |
|
342 | @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') | |
347 | @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id])) |
|
343 | @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id])) | |
348 |
|
344 | |||
349 | @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project, |
|
345 | @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project, | |
350 | :with_subprojects => @with_subprojects, |
|
346 | :with_subprojects => @with_subprojects, | |
351 | :author => @author) |
|
347 | :author => @author) | |
352 | @activity.scope_select {|t| !params["show_#{t}"].nil?} |
|
348 | @activity.scope_select {|t| !params["show_#{t}"].nil?} | |
353 | @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty? |
|
349 | @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty? | |
354 |
|
350 | |||
355 | events = @activity.events(@date_from, @date_to) |
|
351 | events = @activity.events(@date_from, @date_to) | |
356 |
|
352 | |||
357 | if events.empty? || stale?(:etag => [events.first, User.current]) |
|
353 | if events.empty? || stale?(:etag => [events.first, User.current]) | |
358 | respond_to do |format| |
|
354 | respond_to do |format| | |
359 | format.html { |
|
355 | format.html { | |
360 | @events_by_day = events.group_by(&:event_date) |
|
356 | @events_by_day = events.group_by(&:event_date) | |
361 | render :layout => false if request.xhr? |
|
357 | render :layout => false if request.xhr? | |
362 | } |
|
358 | } | |
363 | format.atom { |
|
359 | format.atom { | |
364 | title = l(:label_activity) |
|
360 | title = l(:label_activity) | |
365 | if @author |
|
361 | if @author | |
366 | title = @author.name |
|
362 | title = @author.name | |
367 | elsif @activity.scope.size == 1 |
|
363 | elsif @activity.scope.size == 1 | |
368 | title = l("label_#{@activity.scope.first.singularize}_plural") |
|
364 | title = l("label_#{@activity.scope.first.singularize}_plural") | |
369 | end |
|
365 | end | |
370 | render_feed(events, :title => "#{@project || Setting.app_title}: #{title}") |
|
366 | render_feed(events, :title => "#{@project || Setting.app_title}: #{title}") | |
371 | } |
|
367 | } | |
372 | end |
|
368 | end | |
373 | end |
|
369 | end | |
374 |
|
370 | |||
375 | rescue ActiveRecord::RecordNotFound |
|
371 | rescue ActiveRecord::RecordNotFound | |
376 | render_404 |
|
372 | render_404 | |
377 | end |
|
373 | end | |
378 |
|
374 | |||
379 | private |
|
375 | private | |
380 | # Find project of id params[:id] |
|
376 | # Find project of id params[:id] | |
381 | # if not found, redirect to project list |
|
377 | # if not found, redirect to project list | |
382 | # Used as a before_filter |
|
378 | # Used as a before_filter | |
383 | def find_project |
|
379 | def find_project | |
384 | @project = Project.find(params[:id]) |
|
380 | @project = Project.find(params[:id]) | |
385 | rescue ActiveRecord::RecordNotFound |
|
381 | rescue ActiveRecord::RecordNotFound | |
386 | render_404 |
|
382 | render_404 | |
387 | end |
|
383 | end | |
388 |
|
384 | |||
389 | def find_optional_project |
|
385 | def find_optional_project | |
390 | return true unless params[:id] |
|
386 | return true unless params[:id] | |
391 | @project = Project.find(params[:id]) |
|
387 | @project = Project.find(params[:id]) | |
392 | authorize |
|
388 | authorize | |
393 | rescue ActiveRecord::RecordNotFound |
|
389 | rescue ActiveRecord::RecordNotFound | |
394 | render_404 |
|
390 | render_404 | |
395 | end |
|
391 | end | |
396 |
|
392 | |||
397 | def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil) |
|
393 | def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil) | |
398 | if ids = params[:tracker_ids] |
|
394 | if ids = params[:tracker_ids] | |
399 | @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s } |
|
395 | @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s } | |
400 | else |
|
396 | else | |
401 | @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s } |
|
397 | @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s } | |
402 | end |
|
398 | end | |
403 | end |
|
399 | end | |
404 |
|
400 | |||
405 | # Validates parent_id param according to user's permissions |
|
401 | # Validates parent_id param according to user's permissions | |
406 | # TODO: move it to Project model in a validation that depends on User.current |
|
402 | # TODO: move it to Project model in a validation that depends on User.current | |
407 | def validate_parent_id |
|
403 | def validate_parent_id | |
408 | return true if User.current.admin? |
|
404 | return true if User.current.admin? | |
409 | parent_id = params[:project] && params[:project][:parent_id] |
|
405 | parent_id = params[:project] && params[:project][:parent_id] | |
410 | if parent_id || @project.new_record? |
|
406 | if parent_id || @project.new_record? | |
411 | parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) |
|
407 | parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) | |
412 | unless @project.allowed_parents.include?(parent) |
|
408 | unless @project.allowed_parents.include?(parent) | |
413 | @project.errors.add :parent_id, :invalid |
|
409 | @project.errors.add :parent_id, :invalid | |
414 | return false |
|
410 | return false | |
415 | end |
|
411 | end | |
416 | end |
|
412 | end | |
417 | true |
|
413 | true | |
418 | end |
|
414 | end | |
419 | end |
|
415 | end |
@@ -1,684 +1,684 | |||||
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 | # Specific overidden Activities |
|
23 | # Specific overidden Activities | |
24 | has_many :time_entry_activities |
|
24 | 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}" |
|
25 | 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' |
|
26 | has_many :memberships, :class_name => 'Member' | |
27 | has_many :member_principals, :class_name => 'Member', |
|
27 | has_many :member_principals, :class_name => 'Member', | |
28 | :include => :principal, |
|
28 | :include => :principal, | |
29 | :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})" |
|
29 | :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})" | |
30 | has_many :users, :through => :members |
|
30 | has_many :users, :through => :members | |
31 | has_many :principals, :through => :member_principals, :source => :principal |
|
31 | has_many :principals, :through => :member_principals, :source => :principal | |
32 |
|
32 | |||
33 | has_many :enabled_modules, :dependent => :delete_all |
|
33 | has_many :enabled_modules, :dependent => :delete_all | |
34 | has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" |
|
34 | has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" | |
35 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] |
|
35 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] | |
36 | has_many :issue_changes, :through => :issues, :source => :journals |
|
36 | has_many :issue_changes, :through => :issues, :source => :journals | |
37 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" |
|
37 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" | |
38 | has_many :time_entries, :dependent => :delete_all |
|
38 | has_many :time_entries, :dependent => :delete_all | |
39 | has_many :queries, :dependent => :delete_all |
|
39 | has_many :queries, :dependent => :delete_all | |
40 | has_many :documents, :dependent => :destroy |
|
40 | has_many :documents, :dependent => :destroy | |
41 | has_many :news, :dependent => :delete_all, :include => :author |
|
41 | has_many :news, :dependent => :delete_all, :include => :author | |
42 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" |
|
42 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" | |
43 | has_many :boards, :dependent => :destroy, :order => "position ASC" |
|
43 | has_many :boards, :dependent => :destroy, :order => "position ASC" | |
44 | has_one :repository, :dependent => :destroy |
|
44 | has_one :repository, :dependent => :destroy | |
45 | has_many :changesets, :through => :repository |
|
45 | has_many :changesets, :through => :repository | |
46 | has_one :wiki, :dependent => :destroy |
|
46 | has_one :wiki, :dependent => :destroy | |
47 | # Custom field for the project issues |
|
47 | # Custom field for the project issues | |
48 | has_and_belongs_to_many :issue_custom_fields, |
|
48 | has_and_belongs_to_many :issue_custom_fields, | |
49 | :class_name => 'IssueCustomField', |
|
49 | :class_name => 'IssueCustomField', | |
50 | :order => "#{CustomField.table_name}.position", |
|
50 | :order => "#{CustomField.table_name}.position", | |
51 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", |
|
51 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", | |
52 | :association_foreign_key => 'custom_field_id' |
|
52 | :association_foreign_key => 'custom_field_id' | |
53 |
|
53 | |||
54 | acts_as_nested_set :order => 'name' |
|
54 | acts_as_nested_set :order => 'name' | |
55 | acts_as_attachable :view_permission => :view_files, |
|
55 | acts_as_attachable :view_permission => :view_files, | |
56 | :delete_permission => :manage_files |
|
56 | :delete_permission => :manage_files | |
57 |
|
57 | |||
58 | acts_as_customizable |
|
58 | acts_as_customizable | |
59 | acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil |
|
59 | acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil | |
60 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, |
|
60 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, | |
61 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}, |
|
61 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}, | |
62 | :author => nil |
|
62 | :author => nil | |
63 |
|
63 | |||
64 | attr_protected :status, :enabled_module_names |
|
64 | attr_protected :status, :enabled_module_names | |
65 |
|
65 | |||
66 | validates_presence_of :name, :identifier |
|
66 | validates_presence_of :name, :identifier | |
67 | validates_uniqueness_of :name, :identifier |
|
67 | validates_uniqueness_of :name, :identifier | |
68 | validates_associated :repository, :wiki |
|
68 | validates_associated :repository, :wiki | |
69 | validates_length_of :name, :maximum => 30 |
|
69 | validates_length_of :name, :maximum => 30 | |
70 | validates_length_of :homepage, :maximum => 255 |
|
70 | validates_length_of :homepage, :maximum => 255 | |
71 | validates_length_of :identifier, :in => 1..20 |
|
71 | validates_length_of :identifier, :in => 1..20 | |
72 | # donwcase letters, digits, dashes but not digits only |
|
72 | # 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? } |
|
73 | validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? } | |
74 | # reserved words |
|
74 | # reserved words | |
75 | validates_exclusion_of :identifier, :in => %w( new ) |
|
75 | validates_exclusion_of :identifier, :in => %w( new ) | |
76 |
|
76 | |||
77 | before_destroy :delete_all_members, :destroy_children |
|
77 | before_destroy :delete_all_members, :destroy_children | |
78 |
|
78 | |||
79 | 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] } } |
|
79 | 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] } } | |
80 | named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"} |
|
80 | named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"} | |
81 | named_scope :all_public, { :conditions => { :is_public => true } } |
|
81 | named_scope :all_public, { :conditions => { :is_public => true } } | |
82 | named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } |
|
82 | named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } | |
83 |
|
83 | |||
84 | def identifier=(identifier) |
|
84 | def identifier=(identifier) | |
85 | super unless identifier_frozen? |
|
85 | super unless identifier_frozen? | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | def identifier_frozen? |
|
88 | def identifier_frozen? | |
89 | errors[:identifier].nil? && !(new_record? || identifier.blank?) |
|
89 | errors[:identifier].nil? && !(new_record? || identifier.blank?) | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | # returns latest created projects |
|
92 | # returns latest created projects | |
93 | # non public projects will be returned only if user is a member of those |
|
93 | # non public projects will be returned only if user is a member of those | |
94 | def self.latest(user=nil, count=5) |
|
94 | def self.latest(user=nil, count=5) | |
95 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") |
|
95 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") | |
96 | end |
|
96 | end | |
97 |
|
97 | |||
98 | # Returns a SQL :conditions string used to find all active projects for the specified user. |
|
98 | # Returns a SQL :conditions string used to find all active projects for the specified user. | |
99 | # |
|
99 | # | |
100 | # Examples: |
|
100 | # Examples: | |
101 | # Projects.visible_by(admin) => "projects.status = 1" |
|
101 | # Projects.visible_by(admin) => "projects.status = 1" | |
102 | # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1" |
|
102 | # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1" | |
103 | def self.visible_by(user=nil) |
|
103 | def self.visible_by(user=nil) | |
104 | user ||= User.current |
|
104 | user ||= User.current | |
105 | if user && user.admin? |
|
105 | if user && user.admin? | |
106 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
106 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" | |
107 | elsif user && user.memberships.any? |
|
107 | elsif user && user.memberships.any? | |
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 | 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(',')}))" | |
109 | else |
|
109 | else | |
110 | 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}" | |
111 | end |
|
111 | end | |
112 | end |
|
112 | end | |
113 |
|
113 | |||
114 | def self.allowed_to_condition(user, permission, options={}) |
|
114 | def self.allowed_to_condition(user, permission, options={}) | |
115 | statements = [] |
|
115 | statements = [] | |
116 | base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
116 | base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" | |
117 | if perm = Redmine::AccessControl.permission(permission) |
|
117 | if perm = Redmine::AccessControl.permission(permission) | |
118 | unless perm.project_module.nil? |
|
118 | unless perm.project_module.nil? | |
119 | # If the permission belongs to a project module, make sure the module is enabled |
|
119 | # If the permission belongs to a project module, make sure the module is enabled | |
120 | base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')" |
|
120 | base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')" | |
121 | end |
|
121 | end | |
122 | end |
|
122 | end | |
123 | if options[:project] |
|
123 | if options[:project] | |
124 | project_statement = "#{Project.table_name}.id = #{options[:project].id}" |
|
124 | project_statement = "#{Project.table_name}.id = #{options[:project].id}" | |
125 | project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects] |
|
125 | project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects] | |
126 | base_statement = "(#{project_statement}) AND (#{base_statement})" |
|
126 | base_statement = "(#{project_statement}) AND (#{base_statement})" | |
127 | end |
|
127 | end | |
128 | if user.admin? |
|
128 | if user.admin? | |
129 | # no restriction |
|
129 | # no restriction | |
130 | else |
|
130 | else | |
131 | statements << "1=0" |
|
131 | statements << "1=0" | |
132 | if user.logged? |
|
132 | if user.logged? | |
133 | if Role.non_member.allowed_to?(permission) && !options[:member] |
|
133 | if Role.non_member.allowed_to?(permission) && !options[:member] | |
134 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" |
|
134 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" | |
135 | end |
|
135 | end | |
136 | allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id} |
|
136 | allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id} | |
137 | statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any? |
|
137 | statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any? | |
138 | else |
|
138 | else | |
139 | if Role.anonymous.allowed_to?(permission) && !options[:member] |
|
139 | if Role.anonymous.allowed_to?(permission) && !options[:member] | |
140 | # anonymous user allowed on public project |
|
140 | # anonymous user allowed on public project | |
141 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" |
|
141 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" | |
142 | end |
|
142 | end | |
143 | end |
|
143 | end | |
144 | end |
|
144 | end | |
145 | statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))" |
|
145 | statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))" | |
146 | end |
|
146 | end | |
147 |
|
147 | |||
148 | # Returns the Systemwide and project specific activities |
|
148 | # Returns the Systemwide and project specific activities | |
149 | def activities(include_inactive=false) |
|
149 | def activities(include_inactive=false) | |
150 | if include_inactive |
|
150 | if include_inactive | |
151 | return all_activities |
|
151 | return all_activities | |
152 | else |
|
152 | else | |
153 | return active_activities |
|
153 | return active_activities | |
154 | end |
|
154 | end | |
155 | end |
|
155 | end | |
156 |
|
156 | |||
157 | # Will create a new Project specific Activity or update an existing one |
|
157 | # Will create a new Project specific Activity or update an existing one | |
158 | # |
|
158 | # | |
159 | # This will raise a ActiveRecord::Rollback if the TimeEntryActivity |
|
159 | # This will raise a ActiveRecord::Rollback if the TimeEntryActivity | |
160 | # does not successfully save. |
|
160 | # does not successfully save. | |
161 | def update_or_create_time_entry_activity(id, activity_hash) |
|
161 | def update_or_create_time_entry_activity(id, activity_hash) | |
162 | if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id') |
|
162 | if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id') | |
163 | self.create_time_entry_activity_if_needed(activity_hash) |
|
163 | self.create_time_entry_activity_if_needed(activity_hash) | |
164 | else |
|
164 | else | |
165 | activity = project.time_entry_activities.find_by_id(id.to_i) |
|
165 | activity = project.time_entry_activities.find_by_id(id.to_i) | |
166 | activity.update_attributes(activity_hash) if activity |
|
166 | activity.update_attributes(activity_hash) if activity | |
167 | end |
|
167 | end | |
168 | end |
|
168 | end | |
169 |
|
169 | |||
170 | # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity |
|
170 | # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity | |
171 | # |
|
171 | # | |
172 | # This will raise a ActiveRecord::Rollback if the TimeEntryActivity |
|
172 | # This will raise a ActiveRecord::Rollback if the TimeEntryActivity | |
173 | # does not successfully save. |
|
173 | # does not successfully save. | |
174 | def create_time_entry_activity_if_needed(activity) |
|
174 | def create_time_entry_activity_if_needed(activity) | |
175 | if activity['parent_id'] |
|
175 | if activity['parent_id'] | |
176 |
|
176 | |||
177 | parent_activity = TimeEntryActivity.find(activity['parent_id']) |
|
177 | parent_activity = TimeEntryActivity.find(activity['parent_id']) | |
178 | activity['name'] = parent_activity.name |
|
178 | activity['name'] = parent_activity.name | |
179 | activity['position'] = parent_activity.position |
|
179 | activity['position'] = parent_activity.position | |
180 |
|
180 | |||
181 | if Enumeration.overridding_change?(activity, parent_activity) |
|
181 | if Enumeration.overridding_change?(activity, parent_activity) | |
182 | project_activity = self.time_entry_activities.create(activity) |
|
182 | project_activity = self.time_entry_activities.create(activity) | |
183 |
|
183 | |||
184 | if project_activity.new_record? |
|
184 | if project_activity.new_record? | |
185 | raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved" |
|
185 | raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved" | |
186 | else |
|
186 | else | |
187 | self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id]) |
|
187 | self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id]) | |
188 | end |
|
188 | end | |
189 | end |
|
189 | end | |
190 | end |
|
190 | end | |
191 | end |
|
191 | end | |
192 |
|
192 | |||
193 | # Returns a :conditions SQL string that can be used to find the issues associated with this project. |
|
193 | # Returns a :conditions SQL string that can be used to find the issues associated with this project. | |
194 | # |
|
194 | # | |
195 | # Examples: |
|
195 | # Examples: | |
196 | # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))" |
|
196 | # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))" | |
197 | # project.project_condition(false) => "projects.id = 1" |
|
197 | # project.project_condition(false) => "projects.id = 1" | |
198 | def project_condition(with_subprojects) |
|
198 | def project_condition(with_subprojects) | |
199 | cond = "#{Project.table_name}.id = #{id}" |
|
199 | cond = "#{Project.table_name}.id = #{id}" | |
200 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects |
|
200 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects | |
201 | cond |
|
201 | cond | |
202 | end |
|
202 | end | |
203 |
|
203 | |||
204 | def self.find(*args) |
|
204 | def self.find(*args) | |
205 | if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) |
|
205 | if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) | |
206 | project = find_by_identifier(*args) |
|
206 | project = find_by_identifier(*args) | |
207 | raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? |
|
207 | raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? | |
208 | project |
|
208 | project | |
209 | else |
|
209 | else | |
210 | super |
|
210 | super | |
211 | end |
|
211 | end | |
212 | end |
|
212 | end | |
213 |
|
213 | |||
214 | def to_param |
|
214 | def to_param | |
215 | # id is used for projects with a numeric identifier (compatibility) |
|
215 | # id is used for projects with a numeric identifier (compatibility) | |
216 | @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier) |
|
216 | @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier) | |
217 | end |
|
217 | end | |
218 |
|
218 | |||
219 | def active? |
|
219 | def active? | |
220 | self.status == STATUS_ACTIVE |
|
220 | self.status == STATUS_ACTIVE | |
221 | end |
|
221 | end | |
222 |
|
222 | |||
223 | # Archives the project and its descendants |
|
223 | # Archives the project and its descendants | |
224 | def archive |
|
224 | def archive | |
225 | # Check that there is no issue of a non descendant project that is assigned |
|
225 | # Check that there is no issue of a non descendant project that is assigned | |
226 | # to one of the project or descendant versions |
|
226 | # to one of the project or descendant versions | |
227 | v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten |
|
227 | v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten | |
228 | if v_ids.any? && Issue.find(:first, :include => :project, |
|
228 | if v_ids.any? && Issue.find(:first, :include => :project, | |
229 | :conditions => ["(#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?)" + |
|
229 | :conditions => ["(#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?)" + | |
230 | " AND #{Issue.table_name}.fixed_version_id IN (?)", lft, rgt, v_ids]) |
|
230 | " AND #{Issue.table_name}.fixed_version_id IN (?)", lft, rgt, v_ids]) | |
231 | return false |
|
231 | return false | |
232 | end |
|
232 | end | |
233 | Project.transaction do |
|
233 | Project.transaction do | |
234 | archive! |
|
234 | archive! | |
235 | end |
|
235 | end | |
236 | true |
|
236 | true | |
237 | end |
|
237 | end | |
238 |
|
238 | |||
239 | # Unarchives the project |
|
239 | # Unarchives the project | |
240 | # All its ancestors must be active |
|
240 | # All its ancestors must be active | |
241 | def unarchive |
|
241 | def unarchive | |
242 | return false if ancestors.detect {|a| !a.active?} |
|
242 | return false if ancestors.detect {|a| !a.active?} | |
243 | update_attribute :status, STATUS_ACTIVE |
|
243 | update_attribute :status, STATUS_ACTIVE | |
244 | end |
|
244 | end | |
245 |
|
245 | |||
246 | # Returns an array of projects the project can be moved to |
|
246 | # Returns an array of projects the project can be moved to | |
247 | # by the current user |
|
247 | # by the current user | |
248 | def allowed_parents |
|
248 | def allowed_parents | |
249 | return @allowed_parents if @allowed_parents |
|
249 | return @allowed_parents if @allowed_parents | |
250 | @allowed_parents = Project.find(:all, :conditions => Project.allowed_to_condition(User.current, :add_subprojects)) |
|
250 | @allowed_parents = Project.find(:all, :conditions => Project.allowed_to_condition(User.current, :add_subprojects)) | |
251 | @allowed_parents = @allowed_parents - self_and_descendants |
|
251 | @allowed_parents = @allowed_parents - self_and_descendants | |
252 | if User.current.allowed_to?(:add_project, nil, :global => true) |
|
252 | if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?) | |
253 | @allowed_parents << nil |
|
253 | @allowed_parents << nil | |
254 | end |
|
254 | end | |
255 | unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent) |
|
255 | unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent) | |
256 | @allowed_parents << parent |
|
256 | @allowed_parents << parent | |
257 | end |
|
257 | end | |
258 | @allowed_parents |
|
258 | @allowed_parents | |
259 | end |
|
259 | end | |
260 |
|
260 | |||
261 | # Sets the parent of the project with authorization check |
|
261 | # Sets the parent of the project with authorization check | |
262 | def set_allowed_parent!(p) |
|
262 | def set_allowed_parent!(p) | |
263 | unless p.nil? || p.is_a?(Project) |
|
263 | unless p.nil? || p.is_a?(Project) | |
264 | if p.to_s.blank? |
|
264 | if p.to_s.blank? | |
265 | p = nil |
|
265 | p = nil | |
266 | else |
|
266 | else | |
267 | p = Project.find_by_id(p) |
|
267 | p = Project.find_by_id(p) | |
268 | return false unless p |
|
268 | return false unless p | |
269 | end |
|
269 | end | |
270 | end |
|
270 | end | |
271 | if p.nil? |
|
271 | if p.nil? | |
272 | if !new_record? && allowed_parents.empty? |
|
272 | if !new_record? && allowed_parents.empty? | |
273 | return false |
|
273 | return false | |
274 | end |
|
274 | end | |
275 | elsif !allowed_parents.include?(p) |
|
275 | elsif !allowed_parents.include?(p) | |
276 | return false |
|
276 | return false | |
277 | end |
|
277 | end | |
278 | set_parent!(p) |
|
278 | set_parent!(p) | |
279 | end |
|
279 | end | |
280 |
|
280 | |||
281 | # Sets the parent of the project |
|
281 | # Sets the parent of the project | |
282 | # Argument can be either a Project, a String, a Fixnum or nil |
|
282 | # Argument can be either a Project, a String, a Fixnum or nil | |
283 | def set_parent!(p) |
|
283 | def set_parent!(p) | |
284 | unless p.nil? || p.is_a?(Project) |
|
284 | unless p.nil? || p.is_a?(Project) | |
285 | if p.to_s.blank? |
|
285 | if p.to_s.blank? | |
286 | p = nil |
|
286 | p = nil | |
287 | else |
|
287 | else | |
288 | p = Project.find_by_id(p) |
|
288 | p = Project.find_by_id(p) | |
289 | return false unless p |
|
289 | return false unless p | |
290 | end |
|
290 | end | |
291 | end |
|
291 | end | |
292 | if p == parent && !p.nil? |
|
292 | if p == parent && !p.nil? | |
293 | # Nothing to do |
|
293 | # Nothing to do | |
294 | true |
|
294 | true | |
295 | elsif p.nil? || (p.active? && move_possible?(p)) |
|
295 | elsif p.nil? || (p.active? && move_possible?(p)) | |
296 | # Insert the project so that target's children or root projects stay alphabetically sorted |
|
296 | # Insert the project so that target's children or root projects stay alphabetically sorted | |
297 | sibs = (p.nil? ? self.class.roots : p.children) |
|
297 | sibs = (p.nil? ? self.class.roots : p.children) | |
298 | to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase } |
|
298 | to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase } | |
299 | if to_be_inserted_before |
|
299 | if to_be_inserted_before | |
300 | move_to_left_of(to_be_inserted_before) |
|
300 | move_to_left_of(to_be_inserted_before) | |
301 | elsif p.nil? |
|
301 | elsif p.nil? | |
302 | if sibs.empty? |
|
302 | if sibs.empty? | |
303 | # move_to_root adds the project in first (ie. left) position |
|
303 | # move_to_root adds the project in first (ie. left) position | |
304 | move_to_root |
|
304 | move_to_root | |
305 | else |
|
305 | else | |
306 | move_to_right_of(sibs.last) unless self == sibs.last |
|
306 | move_to_right_of(sibs.last) unless self == sibs.last | |
307 | end |
|
307 | end | |
308 | else |
|
308 | else | |
309 | # move_to_child_of adds the project in last (ie.right) position |
|
309 | # move_to_child_of adds the project in last (ie.right) position | |
310 | move_to_child_of(p) |
|
310 | move_to_child_of(p) | |
311 | end |
|
311 | end | |
312 | Issue.update_versions_from_hierarchy_change(self) |
|
312 | Issue.update_versions_from_hierarchy_change(self) | |
313 | true |
|
313 | true | |
314 | else |
|
314 | else | |
315 | # Can not move to the given target |
|
315 | # Can not move to the given target | |
316 | false |
|
316 | false | |
317 | end |
|
317 | end | |
318 | end |
|
318 | end | |
319 |
|
319 | |||
320 | # Returns an array of the trackers used by the project and its active sub projects |
|
320 | # Returns an array of the trackers used by the project and its active sub projects | |
321 | def rolled_up_trackers |
|
321 | def rolled_up_trackers | |
322 | @rolled_up_trackers ||= |
|
322 | @rolled_up_trackers ||= | |
323 | Tracker.find(:all, :include => :projects, |
|
323 | Tracker.find(:all, :include => :projects, | |
324 | :select => "DISTINCT #{Tracker.table_name}.*", |
|
324 | :select => "DISTINCT #{Tracker.table_name}.*", | |
325 | :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt], |
|
325 | :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt], | |
326 | :order => "#{Tracker.table_name}.position") |
|
326 | :order => "#{Tracker.table_name}.position") | |
327 | end |
|
327 | end | |
328 |
|
328 | |||
329 | # Closes open and locked project versions that are completed |
|
329 | # Closes open and locked project versions that are completed | |
330 | def close_completed_versions |
|
330 | def close_completed_versions | |
331 | Version.transaction do |
|
331 | Version.transaction do | |
332 | versions.find(:all, :conditions => {:status => %w(open locked)}).each do |version| |
|
332 | versions.find(:all, :conditions => {:status => %w(open locked)}).each do |version| | |
333 | if version.completed? |
|
333 | if version.completed? | |
334 | version.update_attribute(:status, 'closed') |
|
334 | version.update_attribute(:status, 'closed') | |
335 | end |
|
335 | end | |
336 | end |
|
336 | end | |
337 | end |
|
337 | end | |
338 | end |
|
338 | end | |
339 |
|
339 | |||
340 | # Returns a scope of the Versions used by the project |
|
340 | # Returns a scope of the Versions used by the project | |
341 | def shared_versions |
|
341 | def shared_versions | |
342 | @shared_versions ||= |
|
342 | @shared_versions ||= | |
343 | Version.scoped(:include => :project, |
|
343 | Version.scoped(:include => :project, | |
344 | :conditions => "#{Project.table_name}.id = #{id}" + |
|
344 | :conditions => "#{Project.table_name}.id = #{id}" + | |
345 | " OR (#{Project.table_name}.status = #{Project::STATUS_ACTIVE} AND (" + |
|
345 | " OR (#{Project.table_name}.status = #{Project::STATUS_ACTIVE} AND (" + | |
346 | " #{Version.table_name}.sharing = 'system'" + |
|
346 | " #{Version.table_name}.sharing = 'system'" + | |
347 | " OR (#{Project.table_name}.lft >= #{root.lft} AND #{Project.table_name}.rgt <= #{root.rgt} AND #{Version.table_name}.sharing = 'tree')" + |
|
347 | " OR (#{Project.table_name}.lft >= #{root.lft} AND #{Project.table_name}.rgt <= #{root.rgt} AND #{Version.table_name}.sharing = 'tree')" + | |
348 | " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" + |
|
348 | " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" + | |
349 | " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" + |
|
349 | " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" + | |
350 | "))") |
|
350 | "))") | |
351 | end |
|
351 | end | |
352 |
|
352 | |||
353 | # Returns a hash of project users grouped by role |
|
353 | # Returns a hash of project users grouped by role | |
354 | def users_by_role |
|
354 | def users_by_role | |
355 | members.find(:all, :include => [:user, :roles]).inject({}) do |h, m| |
|
355 | members.find(:all, :include => [:user, :roles]).inject({}) do |h, m| | |
356 | m.roles.each do |r| |
|
356 | m.roles.each do |r| | |
357 | h[r] ||= [] |
|
357 | h[r] ||= [] | |
358 | h[r] << m.user |
|
358 | h[r] << m.user | |
359 | end |
|
359 | end | |
360 | h |
|
360 | h | |
361 | end |
|
361 | end | |
362 | end |
|
362 | end | |
363 |
|
363 | |||
364 | # Deletes all project's members |
|
364 | # Deletes all project's members | |
365 | def delete_all_members |
|
365 | def delete_all_members | |
366 | me, mr = Member.table_name, MemberRole.table_name |
|
366 | me, mr = Member.table_name, MemberRole.table_name | |
367 | connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})") |
|
367 | connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})") | |
368 | Member.delete_all(['project_id = ?', id]) |
|
368 | Member.delete_all(['project_id = ?', id]) | |
369 | end |
|
369 | end | |
370 |
|
370 | |||
371 | # Users issues can be assigned to |
|
371 | # Users issues can be assigned to | |
372 | def assignable_users |
|
372 | def assignable_users | |
373 | members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort |
|
373 | members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort | |
374 | end |
|
374 | end | |
375 |
|
375 | |||
376 | # Returns the mail adresses of users that should be always notified on project events |
|
376 | # Returns the mail adresses of users that should be always notified on project events | |
377 | def recipients |
|
377 | def recipients | |
378 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} |
|
378 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} | |
379 | end |
|
379 | end | |
380 |
|
380 | |||
381 | # Returns the users that should be notified on project events |
|
381 | # Returns the users that should be notified on project events | |
382 | def notified_users |
|
382 | def notified_users | |
383 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user} |
|
383 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user} | |
384 | end |
|
384 | end | |
385 |
|
385 | |||
386 | # Returns an array of all custom fields enabled for project issues |
|
386 | # Returns an array of all custom fields enabled for project issues | |
387 | # (explictly associated custom fields and custom fields enabled for all projects) |
|
387 | # (explictly associated custom fields and custom fields enabled for all projects) | |
388 | def all_issue_custom_fields |
|
388 | def all_issue_custom_fields | |
389 | @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort |
|
389 | @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort | |
390 | end |
|
390 | end | |
391 |
|
391 | |||
392 | def project |
|
392 | def project | |
393 | self |
|
393 | self | |
394 | end |
|
394 | end | |
395 |
|
395 | |||
396 | def <=>(project) |
|
396 | def <=>(project) | |
397 | name.downcase <=> project.name.downcase |
|
397 | name.downcase <=> project.name.downcase | |
398 | end |
|
398 | end | |
399 |
|
399 | |||
400 | def to_s |
|
400 | def to_s | |
401 | name |
|
401 | name | |
402 | end |
|
402 | end | |
403 |
|
403 | |||
404 | # Returns a short description of the projects (first lines) |
|
404 | # Returns a short description of the projects (first lines) | |
405 | def short_description(length = 255) |
|
405 | def short_description(length = 255) | |
406 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description |
|
406 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description | |
407 | end |
|
407 | end | |
408 |
|
408 | |||
409 | # Return true if this project is allowed to do the specified action. |
|
409 | # Return true if this project is allowed to do the specified action. | |
410 | # action can be: |
|
410 | # action can be: | |
411 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
411 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') | |
412 | # * a permission Symbol (eg. :edit_project) |
|
412 | # * a permission Symbol (eg. :edit_project) | |
413 | def allows_to?(action) |
|
413 | def allows_to?(action) | |
414 | if action.is_a? Hash |
|
414 | if action.is_a? Hash | |
415 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
415 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" | |
416 | else |
|
416 | else | |
417 | allowed_permissions.include? action |
|
417 | allowed_permissions.include? action | |
418 | end |
|
418 | end | |
419 | end |
|
419 | end | |
420 |
|
420 | |||
421 | def module_enabled?(module_name) |
|
421 | def module_enabled?(module_name) | |
422 | module_name = module_name.to_s |
|
422 | module_name = module_name.to_s | |
423 | enabled_modules.detect {|m| m.name == module_name} |
|
423 | enabled_modules.detect {|m| m.name == module_name} | |
424 | end |
|
424 | end | |
425 |
|
425 | |||
426 | def enabled_module_names=(module_names) |
|
426 | def enabled_module_names=(module_names) | |
427 | if module_names && module_names.is_a?(Array) |
|
427 | if module_names && module_names.is_a?(Array) | |
428 | module_names = module_names.collect(&:to_s) |
|
428 | module_names = module_names.collect(&:to_s) | |
429 | # remove disabled modules |
|
429 | # remove disabled modules | |
430 | enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} |
|
430 | enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} | |
431 | # add new modules |
|
431 | # add new modules | |
432 | module_names.reject {|name| module_enabled?(name)}.each {|name| enabled_modules << EnabledModule.new(:name => name)} |
|
432 | module_names.reject {|name| module_enabled?(name)}.each {|name| enabled_modules << EnabledModule.new(:name => name)} | |
433 | else |
|
433 | else | |
434 | enabled_modules.clear |
|
434 | enabled_modules.clear | |
435 | end |
|
435 | end | |
436 | end |
|
436 | end | |
437 |
|
437 | |||
438 | # Returns an auto-generated project identifier based on the last identifier used |
|
438 | # Returns an auto-generated project identifier based on the last identifier used | |
439 | def self.next_identifier |
|
439 | def self.next_identifier | |
440 | p = Project.find(:first, :order => 'created_on DESC') |
|
440 | p = Project.find(:first, :order => 'created_on DESC') | |
441 | p.nil? ? nil : p.identifier.to_s.succ |
|
441 | p.nil? ? nil : p.identifier.to_s.succ | |
442 | end |
|
442 | end | |
443 |
|
443 | |||
444 | # Copies and saves the Project instance based on the +project+. |
|
444 | # Copies and saves the Project instance based on the +project+. | |
445 | # Duplicates the source project's: |
|
445 | # Duplicates the source project's: | |
446 | # * Wiki |
|
446 | # * Wiki | |
447 | # * Versions |
|
447 | # * Versions | |
448 | # * Categories |
|
448 | # * Categories | |
449 | # * Issues |
|
449 | # * Issues | |
450 | # * Members |
|
450 | # * Members | |
451 | # * Queries |
|
451 | # * Queries | |
452 | # |
|
452 | # | |
453 | # Accepts an +options+ argument to specify what to copy |
|
453 | # Accepts an +options+ argument to specify what to copy | |
454 | # |
|
454 | # | |
455 | # Examples: |
|
455 | # Examples: | |
456 | # project.copy(1) # => copies everything |
|
456 | # project.copy(1) # => copies everything | |
457 | # project.copy(1, :only => 'members') # => copies members only |
|
457 | # project.copy(1, :only => 'members') # => copies members only | |
458 | # project.copy(1, :only => ['members', 'versions']) # => copies members and versions |
|
458 | # project.copy(1, :only => ['members', 'versions']) # => copies members and versions | |
459 | def copy(project, options={}) |
|
459 | def copy(project, options={}) | |
460 | project = project.is_a?(Project) ? project : Project.find(project) |
|
460 | project = project.is_a?(Project) ? project : Project.find(project) | |
461 |
|
461 | |||
462 | to_be_copied = %w(wiki versions issue_categories issues members queries boards) |
|
462 | to_be_copied = %w(wiki versions issue_categories issues members queries boards) | |
463 | to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil? |
|
463 | to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil? | |
464 |
|
464 | |||
465 | Project.transaction do |
|
465 | Project.transaction do | |
466 | if save |
|
466 | if save | |
467 | reload |
|
467 | reload | |
468 | to_be_copied.each do |name| |
|
468 | to_be_copied.each do |name| | |
469 | send "copy_#{name}", project |
|
469 | send "copy_#{name}", project | |
470 | end |
|
470 | end | |
471 | Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self) |
|
471 | Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self) | |
472 | save |
|
472 | save | |
473 | end |
|
473 | end | |
474 | end |
|
474 | end | |
475 | end |
|
475 | end | |
476 |
|
476 | |||
477 |
|
477 | |||
478 | # Copies +project+ and returns the new instance. This will not save |
|
478 | # Copies +project+ and returns the new instance. This will not save | |
479 | # the copy |
|
479 | # the copy | |
480 | def self.copy_from(project) |
|
480 | def self.copy_from(project) | |
481 | begin |
|
481 | begin | |
482 | project = project.is_a?(Project) ? project : Project.find(project) |
|
482 | project = project.is_a?(Project) ? project : Project.find(project) | |
483 | if project |
|
483 | if project | |
484 | # clear unique attributes |
|
484 | # clear unique attributes | |
485 | attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt') |
|
485 | attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt') | |
486 | copy = Project.new(attributes) |
|
486 | copy = Project.new(attributes) | |
487 | copy.enabled_modules = project.enabled_modules |
|
487 | copy.enabled_modules = project.enabled_modules | |
488 | copy.trackers = project.trackers |
|
488 | copy.trackers = project.trackers | |
489 | copy.custom_values = project.custom_values.collect {|v| v.clone} |
|
489 | copy.custom_values = project.custom_values.collect {|v| v.clone} | |
490 | copy.issue_custom_fields = project.issue_custom_fields |
|
490 | copy.issue_custom_fields = project.issue_custom_fields | |
491 | return copy |
|
491 | return copy | |
492 | else |
|
492 | else | |
493 | return nil |
|
493 | return nil | |
494 | end |
|
494 | end | |
495 | rescue ActiveRecord::RecordNotFound |
|
495 | rescue ActiveRecord::RecordNotFound | |
496 | return nil |
|
496 | return nil | |
497 | end |
|
497 | end | |
498 | end |
|
498 | end | |
499 |
|
499 | |||
500 | private |
|
500 | private | |
501 |
|
501 | |||
502 | # Destroys children before destroying self |
|
502 | # Destroys children before destroying self | |
503 | def destroy_children |
|
503 | def destroy_children | |
504 | children.each do |child| |
|
504 | children.each do |child| | |
505 | child.destroy |
|
505 | child.destroy | |
506 | end |
|
506 | end | |
507 | end |
|
507 | end | |
508 |
|
508 | |||
509 | # Copies wiki from +project+ |
|
509 | # Copies wiki from +project+ | |
510 | def copy_wiki(project) |
|
510 | def copy_wiki(project) | |
511 | # Check that the source project has a wiki first |
|
511 | # Check that the source project has a wiki first | |
512 | unless project.wiki.nil? |
|
512 | unless project.wiki.nil? | |
513 | self.wiki ||= Wiki.new |
|
513 | self.wiki ||= Wiki.new | |
514 | wiki.attributes = project.wiki.attributes.dup.except("id", "project_id") |
|
514 | wiki.attributes = project.wiki.attributes.dup.except("id", "project_id") | |
515 | project.wiki.pages.each do |page| |
|
515 | project.wiki.pages.each do |page| | |
516 | new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on")) |
|
516 | new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on")) | |
517 | new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id")) |
|
517 | new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id")) | |
518 | new_wiki_page.content = new_wiki_content |
|
518 | new_wiki_page.content = new_wiki_content | |
519 | wiki.pages << new_wiki_page |
|
519 | wiki.pages << new_wiki_page | |
520 | end |
|
520 | end | |
521 | end |
|
521 | end | |
522 | end |
|
522 | end | |
523 |
|
523 | |||
524 | # Copies versions from +project+ |
|
524 | # Copies versions from +project+ | |
525 | def copy_versions(project) |
|
525 | def copy_versions(project) | |
526 | project.versions.each do |version| |
|
526 | project.versions.each do |version| | |
527 | new_version = Version.new |
|
527 | new_version = Version.new | |
528 | new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on") |
|
528 | new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on") | |
529 | self.versions << new_version |
|
529 | self.versions << new_version | |
530 | end |
|
530 | end | |
531 | end |
|
531 | end | |
532 |
|
532 | |||
533 | # Copies issue categories from +project+ |
|
533 | # Copies issue categories from +project+ | |
534 | def copy_issue_categories(project) |
|
534 | def copy_issue_categories(project) | |
535 | project.issue_categories.each do |issue_category| |
|
535 | project.issue_categories.each do |issue_category| | |
536 | new_issue_category = IssueCategory.new |
|
536 | new_issue_category = IssueCategory.new | |
537 | new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id") |
|
537 | new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id") | |
538 | self.issue_categories << new_issue_category |
|
538 | self.issue_categories << new_issue_category | |
539 | end |
|
539 | end | |
540 | end |
|
540 | end | |
541 |
|
541 | |||
542 | # Copies issues from +project+ |
|
542 | # Copies issues from +project+ | |
543 | def copy_issues(project) |
|
543 | def copy_issues(project) | |
544 | # Stores the source issue id as a key and the copied issues as the |
|
544 | # Stores the source issue id as a key and the copied issues as the | |
545 | # value. Used to map the two togeather for issue relations. |
|
545 | # value. Used to map the two togeather for issue relations. | |
546 | issues_map = {} |
|
546 | issues_map = {} | |
547 |
|
547 | |||
548 | project.issues.each do |issue| |
|
548 | project.issues.each do |issue| | |
549 | new_issue = Issue.new |
|
549 | new_issue = Issue.new | |
550 | new_issue.copy_from(issue) |
|
550 | new_issue.copy_from(issue) | |
551 | # Reassign fixed_versions by name, since names are unique per |
|
551 | # Reassign fixed_versions by name, since names are unique per | |
552 | # project and the versions for self are not yet saved |
|
552 | # project and the versions for self are not yet saved | |
553 | if issue.fixed_version |
|
553 | if issue.fixed_version | |
554 | new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first |
|
554 | new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first | |
555 | end |
|
555 | end | |
556 | # Reassign the category by name, since names are unique per |
|
556 | # Reassign the category by name, since names are unique per | |
557 | # project and the categories for self are not yet saved |
|
557 | # project and the categories for self are not yet saved | |
558 | if issue.category |
|
558 | if issue.category | |
559 | new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first |
|
559 | new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first | |
560 | end |
|
560 | end | |
561 | self.issues << new_issue |
|
561 | self.issues << new_issue | |
562 | issues_map[issue.id] = new_issue |
|
562 | issues_map[issue.id] = new_issue | |
563 | end |
|
563 | end | |
564 |
|
564 | |||
565 | # Relations after in case issues related each other |
|
565 | # Relations after in case issues related each other | |
566 | project.issues.each do |issue| |
|
566 | project.issues.each do |issue| | |
567 | new_issue = issues_map[issue.id] |
|
567 | new_issue = issues_map[issue.id] | |
568 |
|
568 | |||
569 | # Relations |
|
569 | # Relations | |
570 | issue.relations_from.each do |source_relation| |
|
570 | issue.relations_from.each do |source_relation| | |
571 | new_issue_relation = IssueRelation.new |
|
571 | new_issue_relation = IssueRelation.new | |
572 | new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") |
|
572 | new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") | |
573 | new_issue_relation.issue_to = issues_map[source_relation.issue_to_id] |
|
573 | new_issue_relation.issue_to = issues_map[source_relation.issue_to_id] | |
574 | if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations? |
|
574 | if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations? | |
575 | new_issue_relation.issue_to = source_relation.issue_to |
|
575 | new_issue_relation.issue_to = source_relation.issue_to | |
576 | end |
|
576 | end | |
577 | new_issue.relations_from << new_issue_relation |
|
577 | new_issue.relations_from << new_issue_relation | |
578 | end |
|
578 | end | |
579 |
|
579 | |||
580 | issue.relations_to.each do |source_relation| |
|
580 | issue.relations_to.each do |source_relation| | |
581 | new_issue_relation = IssueRelation.new |
|
581 | new_issue_relation = IssueRelation.new | |
582 | new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") |
|
582 | new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") | |
583 | new_issue_relation.issue_from = issues_map[source_relation.issue_from_id] |
|
583 | new_issue_relation.issue_from = issues_map[source_relation.issue_from_id] | |
584 | if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations? |
|
584 | if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations? | |
585 | new_issue_relation.issue_from = source_relation.issue_from |
|
585 | new_issue_relation.issue_from = source_relation.issue_from | |
586 | end |
|
586 | end | |
587 | new_issue.relations_to << new_issue_relation |
|
587 | new_issue.relations_to << new_issue_relation | |
588 | end |
|
588 | end | |
589 | end |
|
589 | end | |
590 | end |
|
590 | end | |
591 |
|
591 | |||
592 | # Copies members from +project+ |
|
592 | # Copies members from +project+ | |
593 | def copy_members(project) |
|
593 | def copy_members(project) | |
594 | project.memberships.each do |member| |
|
594 | project.memberships.each do |member| | |
595 | new_member = Member.new |
|
595 | new_member = Member.new | |
596 | new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on") |
|
596 | new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on") | |
597 | # only copy non inherited roles |
|
597 | # only copy non inherited roles | |
598 | # inherited roles will be added when copying the group membership |
|
598 | # inherited roles will be added when copying the group membership | |
599 | role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id) |
|
599 | role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id) | |
600 | next if role_ids.empty? |
|
600 | next if role_ids.empty? | |
601 | new_member.role_ids = role_ids |
|
601 | new_member.role_ids = role_ids | |
602 | new_member.project = self |
|
602 | new_member.project = self | |
603 | self.members << new_member |
|
603 | self.members << new_member | |
604 | end |
|
604 | end | |
605 | end |
|
605 | end | |
606 |
|
606 | |||
607 | # Copies queries from +project+ |
|
607 | # Copies queries from +project+ | |
608 | def copy_queries(project) |
|
608 | def copy_queries(project) | |
609 | project.queries.each do |query| |
|
609 | project.queries.each do |query| | |
610 | new_query = Query.new |
|
610 | new_query = Query.new | |
611 | new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria") |
|
611 | new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria") | |
612 | new_query.sort_criteria = query.sort_criteria if query.sort_criteria |
|
612 | new_query.sort_criteria = query.sort_criteria if query.sort_criteria | |
613 | new_query.project = self |
|
613 | new_query.project = self | |
614 | self.queries << new_query |
|
614 | self.queries << new_query | |
615 | end |
|
615 | end | |
616 | end |
|
616 | end | |
617 |
|
617 | |||
618 | # Copies boards from +project+ |
|
618 | # Copies boards from +project+ | |
619 | def copy_boards(project) |
|
619 | def copy_boards(project) | |
620 | project.boards.each do |board| |
|
620 | project.boards.each do |board| | |
621 | new_board = Board.new |
|
621 | new_board = Board.new | |
622 | new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id") |
|
622 | new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id") | |
623 | new_board.project = self |
|
623 | new_board.project = self | |
624 | self.boards << new_board |
|
624 | self.boards << new_board | |
625 | end |
|
625 | end | |
626 | end |
|
626 | end | |
627 |
|
627 | |||
628 | def allowed_permissions |
|
628 | def allowed_permissions | |
629 | @allowed_permissions ||= begin |
|
629 | @allowed_permissions ||= begin | |
630 | module_names = enabled_modules.collect {|m| m.name} |
|
630 | module_names = enabled_modules.collect {|m| m.name} | |
631 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} |
|
631 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} | |
632 | end |
|
632 | end | |
633 | end |
|
633 | end | |
634 |
|
634 | |||
635 | def allowed_actions |
|
635 | def allowed_actions | |
636 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
636 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten | |
637 | end |
|
637 | end | |
638 |
|
638 | |||
639 | # Returns all the active Systemwide and project specific activities |
|
639 | # Returns all the active Systemwide and project specific activities | |
640 | def active_activities |
|
640 | def active_activities | |
641 | overridden_activity_ids = self.time_entry_activities.collect(&:parent_id) |
|
641 | overridden_activity_ids = self.time_entry_activities.collect(&:parent_id) | |
642 |
|
642 | |||
643 | if overridden_activity_ids.empty? |
|
643 | if overridden_activity_ids.empty? | |
644 | return TimeEntryActivity.shared.active |
|
644 | return TimeEntryActivity.shared.active | |
645 | else |
|
645 | else | |
646 | return system_activities_and_project_overrides |
|
646 | return system_activities_and_project_overrides | |
647 | end |
|
647 | end | |
648 | end |
|
648 | end | |
649 |
|
649 | |||
650 | # Returns all the Systemwide and project specific activities |
|
650 | # Returns all the Systemwide and project specific activities | |
651 | # (inactive and active) |
|
651 | # (inactive and active) | |
652 | def all_activities |
|
652 | def all_activities | |
653 | overridden_activity_ids = self.time_entry_activities.collect(&:parent_id) |
|
653 | overridden_activity_ids = self.time_entry_activities.collect(&:parent_id) | |
654 |
|
654 | |||
655 | if overridden_activity_ids.empty? |
|
655 | if overridden_activity_ids.empty? | |
656 | return TimeEntryActivity.shared |
|
656 | return TimeEntryActivity.shared | |
657 | else |
|
657 | else | |
658 | return system_activities_and_project_overrides(true) |
|
658 | return system_activities_and_project_overrides(true) | |
659 | end |
|
659 | end | |
660 | end |
|
660 | end | |
661 |
|
661 | |||
662 | # Returns the systemwide active activities merged with the project specific overrides |
|
662 | # Returns the systemwide active activities merged with the project specific overrides | |
663 | def system_activities_and_project_overrides(include_inactive=false) |
|
663 | def system_activities_and_project_overrides(include_inactive=false) | |
664 | if include_inactive |
|
664 | if include_inactive | |
665 | return TimeEntryActivity.shared. |
|
665 | return TimeEntryActivity.shared. | |
666 | find(:all, |
|
666 | find(:all, | |
667 | :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) + |
|
667 | :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) + | |
668 | self.time_entry_activities |
|
668 | self.time_entry_activities | |
669 | else |
|
669 | else | |
670 | return TimeEntryActivity.shared.active. |
|
670 | return TimeEntryActivity.shared.active. | |
671 | find(:all, |
|
671 | find(:all, | |
672 | :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) + |
|
672 | :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) + | |
673 | self.time_entry_activities.active |
|
673 | self.time_entry_activities.active | |
674 | end |
|
674 | end | |
675 | end |
|
675 | end | |
676 |
|
676 | |||
677 | # Archives subprojects recursively |
|
677 | # Archives subprojects recursively | |
678 | def archive! |
|
678 | def archive! | |
679 | children.each do |subproject| |
|
679 | children.each do |subproject| | |
680 | subproject.send :archive! |
|
680 | subproject.send :archive! | |
681 | end |
|
681 | end | |
682 | update_attribute :status, STATUS_ARCHIVED |
|
682 | update_attribute :status, STATUS_ARCHIVED | |
683 | end |
|
683 | end | |
684 | end |
|
684 | end |
@@ -1,204 +1,208 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 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 Repository < ActiveRecord::Base |
|
18 | class Repository < ActiveRecord::Base | |
19 | belongs_to :project |
|
19 | belongs_to :project | |
20 | has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" |
|
20 | has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" | |
21 | has_many :changes, :through => :changesets |
|
21 | has_many :changes, :through => :changesets | |
22 |
|
22 | |||
23 | # Raw SQL to delete changesets and changes in the database |
|
23 | # Raw SQL to delete changesets and changes in the database | |
24 | # has_many :changesets, :dependent => :destroy is too slow for big repositories |
|
24 | # has_many :changesets, :dependent => :destroy is too slow for big repositories | |
25 | before_destroy :clear_changesets |
|
25 | before_destroy :clear_changesets | |
26 |
|
26 | |||
27 | # Checks if the SCM is enabled when creating a repository |
|
27 | # Checks if the SCM is enabled when creating a repository | |
28 | validate_on_create { |r| r.errors.add(:type, :invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) } |
|
28 | validate_on_create { |r| r.errors.add(:type, :invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) } | |
29 |
|
29 | |||
30 | # Removes leading and trailing whitespace |
|
30 | # Removes leading and trailing whitespace | |
31 | def url=(arg) |
|
31 | def url=(arg) | |
32 | write_attribute(:url, arg ? arg.to_s.strip : nil) |
|
32 | write_attribute(:url, arg ? arg.to_s.strip : nil) | |
33 | end |
|
33 | end | |
34 |
|
34 | |||
35 | # Removes leading and trailing whitespace |
|
35 | # Removes leading and trailing whitespace | |
36 | def root_url=(arg) |
|
36 | def root_url=(arg) | |
37 | write_attribute(:root_url, arg ? arg.to_s.strip : nil) |
|
37 | write_attribute(:root_url, arg ? arg.to_s.strip : nil) | |
38 | end |
|
38 | end | |
39 |
|
39 | |||
40 | def scm |
|
40 | def scm | |
41 | @scm ||= self.scm_adapter.new url, root_url, login, password |
|
41 | @scm ||= self.scm_adapter.new url, root_url, login, password | |
42 | update_attribute(:root_url, @scm.root_url) if root_url.blank? |
|
42 | update_attribute(:root_url, @scm.root_url) if root_url.blank? | |
43 | @scm |
|
43 | @scm | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def scm_name |
|
46 | def scm_name | |
47 | self.class.scm_name |
|
47 | self.class.scm_name | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | def supports_cat? |
|
50 | def supports_cat? | |
51 | scm.supports_cat? |
|
51 | scm.supports_cat? | |
52 | end |
|
52 | end | |
53 |
|
53 | |||
54 | def supports_annotate? |
|
54 | def supports_annotate? | |
55 | scm.supports_annotate? |
|
55 | scm.supports_annotate? | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | def entry(path=nil, identifier=nil) |
|
58 | def entry(path=nil, identifier=nil) | |
59 | scm.entry(path, identifier) |
|
59 | scm.entry(path, identifier) | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | def entries(path=nil, identifier=nil) |
|
62 | def entries(path=nil, identifier=nil) | |
63 | scm.entries(path, identifier) |
|
63 | scm.entries(path, identifier) | |
64 | end |
|
64 | end | |
65 |
|
65 | |||
66 | def branches |
|
66 | def branches | |
67 | scm.branches |
|
67 | scm.branches | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | def tags |
|
70 | def tags | |
71 | scm.tags |
|
71 | scm.tags | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def default_branch |
|
74 | def default_branch | |
75 | scm.default_branch |
|
75 | scm.default_branch | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | def properties(path, identifier=nil) |
|
78 | def properties(path, identifier=nil) | |
79 | scm.properties(path, identifier) |
|
79 | scm.properties(path, identifier) | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | def cat(path, identifier=nil) |
|
82 | def cat(path, identifier=nil) | |
83 | scm.cat(path, identifier) |
|
83 | scm.cat(path, identifier) | |
84 | end |
|
84 | end | |
85 |
|
85 | |||
86 | def diff(path, rev, rev_to) |
|
86 | def diff(path, rev, rev_to) | |
87 | scm.diff(path, rev, rev_to) |
|
87 | scm.diff(path, rev, rev_to) | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | # Returns a path relative to the url of the repository |
|
90 | # Returns a path relative to the url of the repository | |
91 | def relative_path(path) |
|
91 | def relative_path(path) | |
92 | path |
|
92 | path | |
93 | end |
|
93 | end | |
94 |
|
94 | |||
95 | # Finds and returns a revision with a number or the beginning of a hash |
|
95 | # Finds and returns a revision with a number or the beginning of a hash | |
96 | def find_changeset_by_name(name) |
|
96 | def find_changeset_by_name(name) | |
97 | changesets.find(:first, :conditions => (name.match(/^\d*$/) ? ["revision = ?", name.to_s] : ["revision LIKE ?", name + '%'])) |
|
97 | changesets.find(:first, :conditions => (name.match(/^\d*$/) ? ["revision = ?", name.to_s] : ["revision LIKE ?", name + '%'])) | |
98 | end |
|
98 | end | |
99 |
|
99 | |||
100 | def latest_changeset |
|
100 | def latest_changeset | |
101 | @latest_changeset ||= changesets.find(:first) |
|
101 | @latest_changeset ||= changesets.find(:first) | |
102 | end |
|
102 | end | |
103 |
|
103 | |||
104 | # Returns the latest changesets for +path+ |
|
104 | # Returns the latest changesets for +path+ | |
105 | # Default behaviour is to search in cached changesets |
|
105 | # Default behaviour is to search in cached changesets | |
106 | def latest_changesets(path, rev, limit=10) |
|
106 | def latest_changesets(path, rev, limit=10) | |
107 | if path.blank? |
|
107 | if path.blank? | |
108 | changesets.find(:all, :include => :user, |
|
108 | changesets.find(:all, :include => :user, | |
109 | :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", |
|
109 | :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", | |
110 | :limit => limit) |
|
110 | :limit => limit) | |
111 | else |
|
111 | else | |
112 | changes.find(:all, :include => {:changeset => :user}, |
|
112 | changes.find(:all, :include => {:changeset => :user}, | |
113 | :conditions => ["path = ?", path.with_leading_slash], |
|
113 | :conditions => ["path = ?", path.with_leading_slash], | |
114 | :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", |
|
114 | :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", | |
115 | :limit => limit).collect(&:changeset) |
|
115 | :limit => limit).collect(&:changeset) | |
116 | end |
|
116 | end | |
117 | end |
|
117 | end | |
118 |
|
118 | |||
119 | def scan_changesets_for_issue_ids |
|
119 | def scan_changesets_for_issue_ids | |
120 | self.changesets.each(&:scan_comment_for_issue_ids) |
|
120 | self.changesets.each(&:scan_comment_for_issue_ids) | |
121 | end |
|
121 | end | |
122 |
|
122 | |||
123 | # Returns an array of committers usernames and associated user_id |
|
123 | # Returns an array of committers usernames and associated user_id | |
124 | def committers |
|
124 | def committers | |
125 | @committers ||= Changeset.connection.select_rows("SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}") |
|
125 | @committers ||= Changeset.connection.select_rows("SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}") | |
126 | end |
|
126 | end | |
127 |
|
127 | |||
128 | # Maps committers username to a user ids |
|
128 | # Maps committers username to a user ids | |
129 | def committer_ids=(h) |
|
129 | def committer_ids=(h) | |
130 | if h.is_a?(Hash) |
|
130 | if h.is_a?(Hash) | |
131 | committers.each do |committer, user_id| |
|
131 | committers.each do |committer, user_id| | |
132 | new_user_id = h[committer] |
|
132 | new_user_id = h[committer] | |
133 | if new_user_id && (new_user_id.to_i != user_id.to_i) |
|
133 | if new_user_id && (new_user_id.to_i != user_id.to_i) | |
134 | new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil) |
|
134 | new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil) | |
135 | Changeset.update_all("user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", ["repository_id = ? AND committer = ?", id, committer]) |
|
135 | Changeset.update_all("user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", ["repository_id = ? AND committer = ?", id, committer]) | |
136 | end |
|
136 | end | |
137 | end |
|
137 | end | |
138 | @committers = nil |
|
138 | @committers = nil | |
139 | true |
|
139 | true | |
140 | else |
|
140 | else | |
141 | false |
|
141 | false | |
142 | end |
|
142 | end | |
143 | end |
|
143 | end | |
144 |
|
144 | |||
145 | # Returns the Redmine User corresponding to the given +committer+ |
|
145 | # Returns the Redmine User corresponding to the given +committer+ | |
146 | # It will return nil if the committer is not yet mapped and if no User |
|
146 | # It will return nil if the committer is not yet mapped and if no User | |
147 | # with the same username or email was found |
|
147 | # with the same username or email was found | |
148 | def find_committer_user(committer) |
|
148 | def find_committer_user(committer) | |
149 | if committer |
|
149 | if committer | |
150 | c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user) |
|
150 | c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user) | |
151 | if c && c.user |
|
151 | if c && c.user | |
152 | c.user |
|
152 | c.user | |
153 | elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/ |
|
153 | elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/ | |
154 | username, email = $1.strip, $3 |
|
154 | username, email = $1.strip, $3 | |
155 | u = User.find_by_login(username) |
|
155 | u = User.find_by_login(username) | |
156 | u ||= User.find_by_mail(email) unless email.blank? |
|
156 | u ||= User.find_by_mail(email) unless email.blank? | |
157 | u |
|
157 | u | |
158 | end |
|
158 | end | |
159 | end |
|
159 | end | |
160 | end |
|
160 | end | |
161 |
|
161 | |||
162 |
# |
|
162 | # Fetches new changesets for all repositories of active projects | |
163 |
# |
|
163 | # Can be called periodically by an external script | |
164 | # eg. ruby script/runner "Repository.fetch_changesets" |
|
164 | # eg. ruby script/runner "Repository.fetch_changesets" | |
165 | def self.fetch_changesets |
|
165 | def self.fetch_changesets | |
166 | find(:all).each(&:fetch_changesets) |
|
166 | Project.active.has_module(:repository).find(:all, :include => :repository).each do |project| | |
|
167 | if project.repository | |||
|
168 | project.repository.fetch_changesets | |||
|
169 | end | |||
|
170 | end | |||
167 | end |
|
171 | end | |
168 |
|
172 | |||
169 | # scan changeset comments to find related and fixed issues for all repositories |
|
173 | # scan changeset comments to find related and fixed issues for all repositories | |
170 | def self.scan_changesets_for_issue_ids |
|
174 | def self.scan_changesets_for_issue_ids | |
171 | find(:all).each(&:scan_changesets_for_issue_ids) |
|
175 | find(:all).each(&:scan_changesets_for_issue_ids) | |
172 | end |
|
176 | end | |
173 |
|
177 | |||
174 | def self.scm_name |
|
178 | def self.scm_name | |
175 | 'Abstract' |
|
179 | 'Abstract' | |
176 | end |
|
180 | end | |
177 |
|
181 | |||
178 | def self.available_scm |
|
182 | def self.available_scm | |
179 | subclasses.collect {|klass| [klass.scm_name, klass.name]} |
|
183 | subclasses.collect {|klass| [klass.scm_name, klass.name]} | |
180 | end |
|
184 | end | |
181 |
|
185 | |||
182 | def self.factory(klass_name, *args) |
|
186 | def self.factory(klass_name, *args) | |
183 | klass = "Repository::#{klass_name}".constantize |
|
187 | klass = "Repository::#{klass_name}".constantize | |
184 | klass.new(*args) |
|
188 | klass.new(*args) | |
185 | rescue |
|
189 | rescue | |
186 | nil |
|
190 | nil | |
187 | end |
|
191 | end | |
188 |
|
192 | |||
189 | private |
|
193 | private | |
190 |
|
194 | |||
191 | def before_save |
|
195 | def before_save | |
192 | # Strips url and root_url |
|
196 | # Strips url and root_url | |
193 | url.strip! |
|
197 | url.strip! | |
194 | root_url.strip! |
|
198 | root_url.strip! | |
195 | true |
|
199 | true | |
196 | end |
|
200 | end | |
197 |
|
201 | |||
198 | def clear_changesets |
|
202 | def clear_changesets | |
199 | cs, ch, ci = Changeset.table_name, Change.table_name, "#{table_name_prefix}changesets_issues#{table_name_suffix}" |
|
203 | cs, ch, ci = Changeset.table_name, Change.table_name, "#{table_name_prefix}changesets_issues#{table_name_suffix}" | |
200 | connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") |
|
204 | connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") | |
201 | connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") |
|
205 | connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") | |
202 | connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}") |
|
206 | connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}") | |
203 | end |
|
207 | end | |
204 | end |
|
208 | end |
@@ -1,251 +1,249 | |||||
1 | <h2><%= l(:label_gantt) %></h2> |
|
1 | <h2><%= l(:label_gantt) %></h2> | |
2 |
|
2 | |||
3 | <% form_tag(params.merge(:month => nil, :year => nil, :months => nil), :id => 'query_form') do %> |
|
3 | <% form_tag(params.merge(:month => nil, :year => nil, :months => nil), :id => 'query_form') do %> | |
4 | <fieldset id="filters" class="collapsible"> |
|
4 | <fieldset id="filters" class="collapsible"> | |
5 | <legend onclick="toggleFieldset(this);"><%= l(:label_filter_plural) %></legend> |
|
5 | <legend onclick="toggleFieldset(this);"><%= l(:label_filter_plural) %></legend> | |
6 | <div> |
|
6 | <div> | |
7 | <%= render :partial => 'queries/filters', :locals => {:query => @query} %> |
|
7 | <%= render :partial => 'queries/filters', :locals => {:query => @query} %> | |
8 | </div> |
|
8 | </div> | |
9 | </fieldset> |
|
9 | </fieldset> | |
10 |
|
10 | |||
11 | <p style="float:right;"> |
|
11 | <p style="float:right;"> | |
12 | <%= if @gantt.zoom < 4 |
|
12 | <%= if @gantt.zoom < 4 | |
13 | link_to_remote image_tag('zoom_in.png'), {:url => @gantt.params.merge(:zoom => (@gantt.zoom+1)), :update => 'content'}, {:href => url_for(@gantt.params.merge(:zoom => (@gantt.zoom+1)))} |
|
13 | link_to_remote image_tag('zoom_in.png'), {:url => @gantt.params.merge(:zoom => (@gantt.zoom+1)), :update => 'content'}, {:href => url_for(@gantt.params.merge(:zoom => (@gantt.zoom+1)))} | |
14 | else |
|
14 | else | |
15 | image_tag 'zoom_in_g.png' |
|
15 | image_tag 'zoom_in_g.png' | |
16 | end %> |
|
16 | end %> | |
17 | <%= if @gantt.zoom > 1 |
|
17 | <%= if @gantt.zoom > 1 | |
18 | link_to_remote image_tag('zoom_out.png'), {:url => @gantt.params.merge(:zoom => (@gantt.zoom-1)), :update => 'content'}, {:href => url_for(@gantt.params.merge(:zoom => (@gantt.zoom-1)))} |
|
18 | link_to_remote image_tag('zoom_out.png'), {:url => @gantt.params.merge(:zoom => (@gantt.zoom-1)), :update => 'content'}, {:href => url_for(@gantt.params.merge(:zoom => (@gantt.zoom-1)))} | |
19 | else |
|
19 | else | |
20 | image_tag 'zoom_out_g.png' |
|
20 | image_tag 'zoom_out_g.png' | |
21 | end %> |
|
21 | end %> | |
22 | </p> |
|
22 | </p> | |
23 |
|
23 | |||
24 | <p class="buttons"> |
|
24 | <p class="buttons"> | |
25 | <%= text_field_tag 'months', @gantt.months, :size => 2 %> |
|
25 | <%= text_field_tag 'months', @gantt.months, :size => 2 %> | |
26 | <%= l(:label_months_from) %> |
|
26 | <%= l(:label_months_from) %> | |
27 | <%= select_month(@gantt.month_from, :prefix => "month", :discard_type => true) %> |
|
27 | <%= select_month(@gantt.month_from, :prefix => "month", :discard_type => true) %> | |
28 | <%= select_year(@gantt.year_from, :prefix => "year", :discard_type => true) %> |
|
28 | <%= select_year(@gantt.year_from, :prefix => "year", :discard_type => true) %> | |
29 | <%= hidden_field_tag 'zoom', @gantt.zoom %> |
|
29 | <%= hidden_field_tag 'zoom', @gantt.zoom %> | |
30 |
|
30 | |||
31 | <%= link_to_remote l(:button_apply), |
|
31 | <%= link_to_remote l(:button_apply), | |
32 | { :url => { :set_filter => (@query.new_record? ? 1 : nil) }, |
|
32 | { :url => { :set_filter => (@query.new_record? ? 1 : nil) }, | |
33 | :update => "content", |
|
33 | :update => "content", | |
34 | :with => "Form.serialize('query_form')" |
|
34 | :with => "Form.serialize('query_form')" | |
35 | }, :class => 'icon icon-checked' %> |
|
35 | }, :class => 'icon icon-checked' %> | |
36 |
|
36 | |||
37 | <%= link_to_remote l(:button_clear), |
|
37 | <%= link_to_remote l(:button_clear), | |
38 | { :url => { :set_filter => (@query.new_record? ? 1 : nil) }, |
|
38 | { :url => { :set_filter => (@query.new_record? ? 1 : nil) }, | |
39 | :update => "content", |
|
39 | :update => "content", | |
40 | }, :class => 'icon icon-reload' if @query.new_record? %> |
|
40 | }, :class => 'icon icon-reload' if @query.new_record? %> | |
41 | </p> |
|
41 | </p> | |
42 | <% end %> |
|
42 | <% end %> | |
43 |
|
43 | |||
44 | <%= error_messages_for 'query' %> |
|
44 | <%= error_messages_for 'query' %> | |
45 | <% if @query.valid? %> |
|
45 | <% if @query.valid? %> | |
46 | <% zoom = 1 |
|
46 | <% zoom = 1 | |
47 | @gantt.zoom.times { zoom = zoom * 2 } |
|
47 | @gantt.zoom.times { zoom = zoom * 2 } | |
48 |
|
48 | |||
49 | subject_width = 330 |
|
49 | subject_width = 330 | |
50 | header_heigth = 18 |
|
50 | header_heigth = 18 | |
51 |
|
51 | |||
52 | headers_height = header_heigth |
|
52 | headers_height = header_heigth | |
53 | show_weeks = false |
|
53 | show_weeks = false | |
54 | show_days = false |
|
54 | show_days = false | |
55 |
|
55 | |||
56 | if @gantt.zoom >1 |
|
56 | if @gantt.zoom >1 | |
57 | show_weeks = true |
|
57 | show_weeks = true | |
58 | headers_height = 2*header_heigth |
|
58 | headers_height = 2*header_heigth | |
59 | if @gantt.zoom > 2 |
|
59 | if @gantt.zoom > 2 | |
60 | show_days = true |
|
60 | show_days = true | |
61 | headers_height = 3*header_heigth |
|
61 | headers_height = 3*header_heigth | |
62 | end |
|
62 | end | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | g_width = (@gantt.date_to - @gantt.date_from + 1)*zoom |
|
65 | g_width = (@gantt.date_to - @gantt.date_from + 1)*zoom | |
66 | g_height = [(20 * @gantt.events.length + 6)+150, 206].max |
|
66 | g_height = [(20 * @gantt.events.length + 6)+150, 206].max | |
67 | t_height = g_height + headers_height |
|
67 | t_height = g_height + headers_height | |
68 | %> |
|
68 | %> | |
69 |
|
69 | |||
70 | <table width="100%" style="border:0; border-collapse: collapse;"> |
|
70 | <table width="100%" style="border:0; border-collapse: collapse;"> | |
71 | <tr> |
|
71 | <tr> | |
72 | <td style="width:<%= subject_width %>px; padding:0px;"> |
|
72 | <td style="width:<%= subject_width %>px; padding:0px;"> | |
73 |
|
73 | |||
74 | <div style="position:relative;height:<%= t_height + 24 %>px;width:<%= subject_width + 1 %>px;"> |
|
74 | <div style="position:relative;height:<%= t_height + 24 %>px;width:<%= subject_width + 1 %>px;"> | |
75 | <div style="right:-2px;width:<%= subject_width %>px;height:<%= headers_height %>px;background: #eee;" class="gantt_hdr"></div> |
|
75 | <div style="right:-2px;width:<%= subject_width %>px;height:<%= headers_height %>px;background: #eee;" class="gantt_hdr"></div> | |
76 | <div style="right:-2px;width:<%= subject_width %>px;height:<%= t_height %>px;border-left: 1px solid #c0c0c0;overflow:hidden;" class="gantt_hdr"></div> |
|
76 | <div style="right:-2px;width:<%= subject_width %>px;height:<%= t_height %>px;border-left: 1px solid #c0c0c0;overflow:hidden;" class="gantt_hdr"></div> | |
77 | <% |
|
77 | <% | |
78 | # |
|
78 | # | |
79 | # Tasks subjects |
|
79 | # Tasks subjects | |
80 | # |
|
80 | # | |
81 | top = headers_height + 8 |
|
81 | top = headers_height + 8 | |
82 | @gantt.events.each do |i| %> |
|
82 | @gantt.events.each do |i| %> | |
83 | <div style="position: absolute;line-height:1.2em;height:16px;top:<%= top %>px;left:4px;overflow:hidden;"><small> |
|
83 | <div style="position: absolute;line-height:1.2em;height:16px;top:<%= top %>px;left:4px;overflow:hidden;"><small> | |
84 | <% if i.is_a? Issue %> |
|
84 | <% if i.is_a? Issue %> | |
85 | <%= h("#{i.project} -") unless @project && @project == i.project %> |
|
85 | <%= h("#{i.project} -") unless @project && @project == i.project %> | |
86 | <%= link_to_issue i %> |
|
86 | <%= link_to_issue i %> | |
87 | <% else %> |
|
87 | <% else %> | |
88 | <span class="icon icon-package"> |
|
88 | <span class="icon icon-package"> | |
89 | <%= h("#{i.project} -") unless @project && @project == i.project %> |
|
|||
90 | <%= link_to_version i %> |
|
89 | <%= link_to_version i %> | |
91 | </span> |
|
90 | </span> | |
92 | <% end %> |
|
91 | <% end %> | |
93 | </small></div> |
|
92 | </small></div> | |
94 | <% top = top + 20 |
|
93 | <% top = top + 20 | |
95 | end %> |
|
94 | end %> | |
96 | </div> |
|
95 | </div> | |
97 | </td> |
|
96 | </td> | |
98 | <td> |
|
97 | <td> | |
99 |
|
98 | |||
100 | <div style="position:relative;height:<%= t_height + 24 %>px;overflow:auto;"> |
|
99 | <div style="position:relative;height:<%= t_height + 24 %>px;overflow:auto;"> | |
101 | <div style="width:<%= g_width-1 %>px;height:<%= headers_height %>px;background: #eee;" class="gantt_hdr"> </div> |
|
100 | <div style="width:<%= g_width-1 %>px;height:<%= headers_height %>px;background: #eee;" class="gantt_hdr"> </div> | |
102 | <% |
|
101 | <% | |
103 | # |
|
102 | # | |
104 | # Months headers |
|
103 | # Months headers | |
105 | # |
|
104 | # | |
106 | month_f = @gantt.date_from |
|
105 | month_f = @gantt.date_from | |
107 | left = 0 |
|
106 | left = 0 | |
108 | height = (show_weeks ? header_heigth : header_heigth + g_height) |
|
107 | height = (show_weeks ? header_heigth : header_heigth + g_height) | |
109 | @gantt.months.times do |
|
108 | @gantt.months.times do | |
110 | width = ((month_f >> 1) - month_f) * zoom - 1 |
|
109 | width = ((month_f >> 1) - month_f) * zoom - 1 | |
111 | %> |
|
110 | %> | |
112 | <div style="left:<%= left %>px;width:<%= width %>px;height:<%= height %>px;" class="gantt_hdr"> |
|
111 | <div style="left:<%= left %>px;width:<%= width %>px;height:<%= height %>px;" class="gantt_hdr"> | |
113 | <%= link_to "#{month_f.year}-#{month_f.month}", @gantt.params.merge(:year => month_f.year, :month => month_f.month), :title => "#{month_name(month_f.month)} #{month_f.year}"%> |
|
112 | <%= link_to "#{month_f.year}-#{month_f.month}", @gantt.params.merge(:year => month_f.year, :month => month_f.month), :title => "#{month_name(month_f.month)} #{month_f.year}"%> | |
114 | </div> |
|
113 | </div> | |
115 | <% |
|
114 | <% | |
116 | left = left + width + 1 |
|
115 | left = left + width + 1 | |
117 | month_f = month_f >> 1 |
|
116 | month_f = month_f >> 1 | |
118 | end %> |
|
117 | end %> | |
119 |
|
118 | |||
120 | <% |
|
119 | <% | |
121 | # |
|
120 | # | |
122 | # Weeks headers |
|
121 | # Weeks headers | |
123 | # |
|
122 | # | |
124 | if show_weeks |
|
123 | if show_weeks | |
125 | left = 0 |
|
124 | left = 0 | |
126 | height = (show_days ? header_heigth-1 : header_heigth-1 + g_height) |
|
125 | height = (show_days ? header_heigth-1 : header_heigth-1 + g_height) | |
127 | if @gantt.date_from.cwday == 1 |
|
126 | if @gantt.date_from.cwday == 1 | |
128 | # @date_from is monday |
|
127 | # @date_from is monday | |
129 | week_f = @gantt.date_from |
|
128 | week_f = @gantt.date_from | |
130 | else |
|
129 | else | |
131 | # find next monday after @date_from |
|
130 | # find next monday after @date_from | |
132 | week_f = @gantt.date_from + (7 - @gantt.date_from.cwday + 1) |
|
131 | week_f = @gantt.date_from + (7 - @gantt.date_from.cwday + 1) | |
133 | width = (7 - @gantt.date_from.cwday + 1) * zoom-1 |
|
132 | width = (7 - @gantt.date_from.cwday + 1) * zoom-1 | |
134 | %> |
|
133 | %> | |
135 | <div style="left:<%= left %>px;top:19px;width:<%= width %>px;height:<%= height %>px;" class="gantt_hdr"> </div> |
|
134 | <div style="left:<%= left %>px;top:19px;width:<%= width %>px;height:<%= height %>px;" class="gantt_hdr"> </div> | |
136 | <% |
|
135 | <% | |
137 | left = left + width+1 |
|
136 | left = left + width+1 | |
138 | end %> |
|
137 | end %> | |
139 | <% |
|
138 | <% | |
140 | while week_f <= @gantt.date_to |
|
139 | while week_f <= @gantt.date_to | |
141 | width = (week_f + 6 <= @gantt.date_to) ? 7 * zoom -1 : (@gantt.date_to - week_f + 1) * zoom-1 |
|
140 | width = (week_f + 6 <= @gantt.date_to) ? 7 * zoom -1 : (@gantt.date_to - week_f + 1) * zoom-1 | |
142 | %> |
|
141 | %> | |
143 | <div style="left:<%= left %>px;top:19px;width:<%= width %>px;height:<%= height %>px;" class="gantt_hdr"> |
|
142 | <div style="left:<%= left %>px;top:19px;width:<%= width %>px;height:<%= height %>px;" class="gantt_hdr"> | |
144 | <small><%= week_f.cweek if width >= 16 %></small> |
|
143 | <small><%= week_f.cweek if width >= 16 %></small> | |
145 | </div> |
|
144 | </div> | |
146 | <% |
|
145 | <% | |
147 | left = left + width+1 |
|
146 | left = left + width+1 | |
148 | week_f = week_f+7 |
|
147 | week_f = week_f+7 | |
149 | end |
|
148 | end | |
150 | end %> |
|
149 | end %> | |
151 |
|
150 | |||
152 | <% |
|
151 | <% | |
153 | # |
|
152 | # | |
154 | # Days headers |
|
153 | # Days headers | |
155 | # |
|
154 | # | |
156 | if show_days |
|
155 | if show_days | |
157 | left = 0 |
|
156 | left = 0 | |
158 | height = g_height + header_heigth - 1 |
|
157 | height = g_height + header_heigth - 1 | |
159 | wday = @gantt.date_from.cwday |
|
158 | wday = @gantt.date_from.cwday | |
160 | (@gantt.date_to - @gantt.date_from + 1).to_i.times do |
|
159 | (@gantt.date_to - @gantt.date_from + 1).to_i.times do | |
161 | width = zoom - 1 |
|
160 | width = zoom - 1 | |
162 | %> |
|
161 | %> | |
163 | <div style="left:<%= left %>px;top:37px;width:<%= width %>px;height:<%= height %>px;font-size:0.7em;<%= "background:#f1f1f1;" if wday > 5 %>" class="gantt_hdr"> |
|
162 | <div style="left:<%= left %>px;top:37px;width:<%= width %>px;height:<%= height %>px;font-size:0.7em;<%= "background:#f1f1f1;" if wday > 5 %>" class="gantt_hdr"> | |
164 | <%= day_name(wday).first %> |
|
163 | <%= day_name(wday).first %> | |
165 | </div> |
|
164 | </div> | |
166 | <% |
|
165 | <% | |
167 | left = left + width+1 |
|
166 | left = left + width+1 | |
168 | wday = wday + 1 |
|
167 | wday = wday + 1 | |
169 | wday = 1 if wday > 7 |
|
168 | wday = 1 if wday > 7 | |
170 | end |
|
169 | end | |
171 | end %> |
|
170 | end %> | |
172 |
|
171 | |||
173 | <% |
|
172 | <% | |
174 | # |
|
173 | # | |
175 | # Tasks |
|
174 | # Tasks | |
176 | # |
|
175 | # | |
177 | top = headers_height + 10 |
|
176 | top = headers_height + 10 | |
178 | @gantt.events.each do |i| |
|
177 | @gantt.events.each do |i| | |
179 | if i.is_a? Issue |
|
178 | if i.is_a? Issue | |
180 | i_start_date = (i.start_date >= @gantt.date_from ? i.start_date : @gantt.date_from ) |
|
179 | i_start_date = (i.start_date >= @gantt.date_from ? i.start_date : @gantt.date_from ) | |
181 | i_end_date = (i.due_before <= @gantt.date_to ? i.due_before : @gantt.date_to ) |
|
180 | i_end_date = (i.due_before <= @gantt.date_to ? i.due_before : @gantt.date_to ) | |
182 |
|
181 | |||
183 | i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor |
|
182 | i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor | |
184 | i_done_date = (i_done_date <= @gantt.date_from ? @gantt.date_from : i_done_date ) |
|
183 | i_done_date = (i_done_date <= @gantt.date_from ? @gantt.date_from : i_done_date ) | |
185 | i_done_date = (i_done_date >= @gantt.date_to ? @gantt.date_to : i_done_date ) |
|
184 | i_done_date = (i_done_date >= @gantt.date_to ? @gantt.date_to : i_done_date ) | |
186 |
|
185 | |||
187 | i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today |
|
186 | i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today | |
188 |
|
187 | |||
189 | i_left = ((i_start_date - @gantt.date_from)*zoom).floor |
|
188 | i_left = ((i_start_date - @gantt.date_from)*zoom).floor | |
190 | i_width = ((i_end_date - i_start_date + 1)*zoom).floor - 2 # total width of the issue (- 2 for left and right borders) |
|
189 | i_width = ((i_end_date - i_start_date + 1)*zoom).floor - 2 # total width of the issue (- 2 for left and right borders) | |
191 | d_width = ((i_done_date - i_start_date)*zoom).floor - 2 # done width |
|
190 | d_width = ((i_done_date - i_start_date)*zoom).floor - 2 # done width | |
192 | l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor - 2 : 0 # delay width |
|
191 | l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor - 2 : 0 # delay width | |
193 | %> |
|
192 | %> | |
194 | <div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= i_width %>px;" class="task task_todo"> </div> |
|
193 | <div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= i_width %>px;" class="task task_todo"> </div> | |
195 | <% if l_width > 0 %> |
|
194 | <% if l_width > 0 %> | |
196 | <div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= l_width %>px;" class="task task_late"> </div> |
|
195 | <div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= l_width %>px;" class="task task_late"> </div> | |
197 | <% end %> |
|
196 | <% end %> | |
198 | <% if d_width > 0 %> |
|
197 | <% if d_width > 0 %> | |
199 | <div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= d_width %>px;" class="task task_done"> </div> |
|
198 | <div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= d_width %>px;" class="task task_done"> </div> | |
200 | <% end %> |
|
199 | <% end %> | |
201 | <div style="top:<%= top %>px;left:<%= i_left + i_width + 5 %>px;background:#fff;" class="task"> |
|
200 | <div style="top:<%= top %>px;left:<%= i_left + i_width + 5 %>px;background:#fff;" class="task"> | |
202 | <%= i.status.name %> |
|
201 | <%= i.status.name %> | |
203 | <%= (i.done_ratio).to_i %>% |
|
202 | <%= (i.done_ratio).to_i %>% | |
204 | </div> |
|
203 | </div> | |
205 | <div class="tooltip" style="position: absolute;top:<%= top %>px;left:<%= i_left %>px;width:<%= i_width %>px;height:12px;"> |
|
204 | <div class="tooltip" style="position: absolute;top:<%= top %>px;left:<%= i_left %>px;width:<%= i_width %>px;height:12px;"> | |
206 | <span class="tip"> |
|
205 | <span class="tip"> | |
207 | <%= render_issue_tooltip i %> |
|
206 | <%= render_issue_tooltip i %> | |
208 | </span></div> |
|
207 | </span></div> | |
209 | <% else |
|
208 | <% else | |
210 | i_left = ((i.start_date - @gantt.date_from)*zoom).floor |
|
209 | i_left = ((i.start_date - @gantt.date_from)*zoom).floor | |
211 | %> |
|
210 | %> | |
212 | <div style="top:<%= top %>px;left:<%= i_left %>px;width:15px;" class="task milestone"> </div> |
|
211 | <div style="top:<%= top %>px;left:<%= i_left %>px;width:15px;" class="task milestone"> </div> | |
213 | <div style="top:<%= top %>px;left:<%= i_left + 12 %>px;background:#fff;" class="task"> |
|
212 | <div style="top:<%= top %>px;left:<%= i_left + 12 %>px;background:#fff;" class="task"> | |
214 | <%= h("#{i.project} -") unless @project && @project == i.project %> |
|
213 | <strong><%= format_version_name i %></strong> | |
215 | <strong><%=h i %></strong> |
|
|||
216 | </div> |
|
214 | </div> | |
217 | <% end %> |
|
215 | <% end %> | |
218 | <% top = top + 20 |
|
216 | <% top = top + 20 | |
219 | end %> |
|
217 | end %> | |
220 |
|
218 | |||
221 | <% |
|
219 | <% | |
222 | # |
|
220 | # | |
223 | # Today red line (excluded from cache) |
|
221 | # Today red line (excluded from cache) | |
224 | # |
|
222 | # | |
225 | if Date.today >= @gantt.date_from and Date.today <= @gantt.date_to %> |
|
223 | if Date.today >= @gantt.date_from and Date.today <= @gantt.date_to %> | |
226 | <div style="position: absolute;height:<%= g_height %>px;top:<%= headers_height + 1 %>px;left:<%= ((Date.today-@gantt.date_from+1)*zoom).floor()-1 %>px;width:10px;border-left: 1px dashed red;"> </div> |
|
224 | <div style="position: absolute;height:<%= g_height %>px;top:<%= headers_height + 1 %>px;left:<%= ((Date.today-@gantt.date_from+1)*zoom).floor()-1 %>px;width:10px;border-left: 1px dashed red;"> </div> | |
227 | <% end %> |
|
225 | <% end %> | |
228 |
|
226 | |||
229 | </div> |
|
227 | </div> | |
230 | </td> |
|
228 | </td> | |
231 | </tr> |
|
229 | </tr> | |
232 | </table> |
|
230 | </table> | |
233 |
|
231 | |||
234 | <table width="100%"> |
|
232 | <table width="100%"> | |
235 | <tr> |
|
233 | <tr> | |
236 | <td align="left"><%= link_to_remote ('« ' + l(:label_previous)), {:url => @gantt.params_previous, :update => 'content', :complete => 'window.scrollTo(0,0)'}, {:href => url_for(@gantt.params_previous)} %></td> |
|
234 | <td align="left"><%= link_to_remote ('« ' + l(:label_previous)), {:url => @gantt.params_previous, :update => 'content', :complete => 'window.scrollTo(0,0)'}, {:href => url_for(@gantt.params_previous)} %></td> | |
237 | <td align="right"><%= link_to_remote (l(:label_next) + ' »'), {:url => @gantt.params_next, :update => 'content', :complete => 'window.scrollTo(0,0)'}, {:href => url_for(@gantt.params_next)} %></td> |
|
235 | <td align="right"><%= link_to_remote (l(:label_next) + ' »'), {:url => @gantt.params_next, :update => 'content', :complete => 'window.scrollTo(0,0)'}, {:href => url_for(@gantt.params_next)} %></td> | |
238 | </tr> |
|
236 | </tr> | |
239 | </table> |
|
237 | </table> | |
240 |
|
238 | |||
241 | <% other_formats_links do |f| %> |
|
239 | <% other_formats_links do |f| %> | |
242 | <%= f.link_to 'PDF', :url => @gantt.params %> |
|
240 | <%= f.link_to 'PDF', :url => @gantt.params %> | |
243 | <%= f.link_to('PNG', :url => @gantt.params) if @gantt.respond_to?('to_image') %> |
|
241 | <%= f.link_to('PNG', :url => @gantt.params) if @gantt.respond_to?('to_image') %> | |
244 | <% end %> |
|
242 | <% end %> | |
245 | <% end # query.valid? %> |
|
243 | <% end # query.valid? %> | |
246 |
|
244 | |||
247 | <% content_for :sidebar do %> |
|
245 | <% content_for :sidebar do %> | |
248 | <%= render :partial => 'issues/sidebar' %> |
|
246 | <%= render :partial => 'issues/sidebar' %> | |
249 | <% end %> |
|
247 | <% end %> | |
250 |
|
248 | |||
251 | <% html_title(l(:label_gantt)) -%> |
|
249 | <% html_title(l(:label_gantt)) -%> |
@@ -1,741 +1,783 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 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 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 |
|
19 | |||
20 | class ProjectTest < ActiveSupport::TestCase |
|
20 | class ProjectTest < ActiveSupport::TestCase | |
21 | fixtures :all |
|
21 | fixtures :all | |
22 |
|
22 | |||
23 | def setup |
|
23 | def setup | |
24 | @ecookbook = Project.find(1) |
|
24 | @ecookbook = Project.find(1) | |
25 | @ecookbook_sub1 = Project.find(3) |
|
25 | @ecookbook_sub1 = Project.find(3) | |
26 | User.current = nil |
|
26 | User.current = nil | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
29 | should_validate_presence_of :name |
|
29 | should_validate_presence_of :name | |
30 | should_validate_presence_of :identifier |
|
30 | should_validate_presence_of :identifier | |
31 |
|
31 | |||
32 | should_validate_uniqueness_of :name |
|
32 | should_validate_uniqueness_of :name | |
33 | should_validate_uniqueness_of :identifier |
|
33 | should_validate_uniqueness_of :identifier | |
34 |
|
34 | |||
35 | context "associations" do |
|
35 | context "associations" do | |
36 | should_have_many :members |
|
36 | should_have_many :members | |
37 | should_have_many :users, :through => :members |
|
37 | should_have_many :users, :through => :members | |
38 | should_have_many :member_principals |
|
38 | should_have_many :member_principals | |
39 | should_have_many :principals, :through => :member_principals |
|
39 | should_have_many :principals, :through => :member_principals | |
40 | should_have_many :enabled_modules |
|
40 | should_have_many :enabled_modules | |
41 | should_have_many :issues |
|
41 | should_have_many :issues | |
42 | should_have_many :issue_changes, :through => :issues |
|
42 | should_have_many :issue_changes, :through => :issues | |
43 | should_have_many :versions |
|
43 | should_have_many :versions | |
44 | should_have_many :time_entries |
|
44 | should_have_many :time_entries | |
45 | should_have_many :queries |
|
45 | should_have_many :queries | |
46 | should_have_many :documents |
|
46 | should_have_many :documents | |
47 | should_have_many :news |
|
47 | should_have_many :news | |
48 | should_have_many :issue_categories |
|
48 | should_have_many :issue_categories | |
49 | should_have_many :boards |
|
49 | should_have_many :boards | |
50 | should_have_many :changesets, :through => :repository |
|
50 | should_have_many :changesets, :through => :repository | |
51 |
|
51 | |||
52 | should_have_one :repository |
|
52 | should_have_one :repository | |
53 | should_have_one :wiki |
|
53 | should_have_one :wiki | |
54 |
|
54 | |||
55 | should_have_and_belong_to_many :trackers |
|
55 | should_have_and_belong_to_many :trackers | |
56 | should_have_and_belong_to_many :issue_custom_fields |
|
56 | should_have_and_belong_to_many :issue_custom_fields | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | def test_truth |
|
59 | def test_truth | |
60 | assert_kind_of Project, @ecookbook |
|
60 | assert_kind_of Project, @ecookbook | |
61 | assert_equal "eCookbook", @ecookbook.name |
|
61 | assert_equal "eCookbook", @ecookbook.name | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def test_update |
|
64 | def test_update | |
65 | assert_equal "eCookbook", @ecookbook.name |
|
65 | assert_equal "eCookbook", @ecookbook.name | |
66 | @ecookbook.name = "eCook" |
|
66 | @ecookbook.name = "eCook" | |
67 | assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ") |
|
67 | assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ") | |
68 | @ecookbook.reload |
|
68 | @ecookbook.reload | |
69 | assert_equal "eCook", @ecookbook.name |
|
69 | assert_equal "eCook", @ecookbook.name | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def test_validate_identifier |
|
72 | def test_validate_identifier | |
73 | to_test = {"abc" => true, |
|
73 | to_test = {"abc" => true, | |
74 | "ab12" => true, |
|
74 | "ab12" => true, | |
75 | "ab-12" => true, |
|
75 | "ab-12" => true, | |
76 | "12" => false, |
|
76 | "12" => false, | |
77 | "new" => false} |
|
77 | "new" => false} | |
78 |
|
78 | |||
79 | to_test.each do |identifier, valid| |
|
79 | to_test.each do |identifier, valid| | |
80 | p = Project.new |
|
80 | p = Project.new | |
81 | p.identifier = identifier |
|
81 | p.identifier = identifier | |
82 | p.valid? |
|
82 | p.valid? | |
83 | assert_equal valid, p.errors.on('identifier').nil? |
|
83 | assert_equal valid, p.errors.on('identifier').nil? | |
84 | end |
|
84 | end | |
85 | end |
|
85 | end | |
86 |
|
86 | |||
87 | def test_members_should_be_active_users |
|
87 | def test_members_should_be_active_users | |
88 | Project.all.each do |project| |
|
88 | Project.all.each do |project| | |
89 | assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) } |
|
89 | assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) } | |
90 | end |
|
90 | end | |
91 | end |
|
91 | end | |
92 |
|
92 | |||
93 | def test_users_should_be_active_users |
|
93 | def test_users_should_be_active_users | |
94 | Project.all.each do |project| |
|
94 | Project.all.each do |project| | |
95 | assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) } |
|
95 | assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) } | |
96 | end |
|
96 | end | |
97 | end |
|
97 | end | |
98 |
|
98 | |||
99 | def test_archive |
|
99 | def test_archive | |
100 | user = @ecookbook.members.first.user |
|
100 | user = @ecookbook.members.first.user | |
101 | @ecookbook.archive |
|
101 | @ecookbook.archive | |
102 | @ecookbook.reload |
|
102 | @ecookbook.reload | |
103 |
|
103 | |||
104 | assert !@ecookbook.active? |
|
104 | assert !@ecookbook.active? | |
105 | assert !user.projects.include?(@ecookbook) |
|
105 | assert !user.projects.include?(@ecookbook) | |
106 | # Subproject are also archived |
|
106 | # Subproject are also archived | |
107 | assert !@ecookbook.children.empty? |
|
107 | assert !@ecookbook.children.empty? | |
108 | assert @ecookbook.descendants.active.empty? |
|
108 | assert @ecookbook.descendants.active.empty? | |
109 | end |
|
109 | end | |
110 |
|
110 | |||
111 | def test_archive_should_fail_if_versions_are_used_by_non_descendant_projects |
|
111 | def test_archive_should_fail_if_versions_are_used_by_non_descendant_projects | |
112 | # Assign an issue of a project to a version of a child project |
|
112 | # Assign an issue of a project to a version of a child project | |
113 | Issue.find(4).update_attribute :fixed_version_id, 4 |
|
113 | Issue.find(4).update_attribute :fixed_version_id, 4 | |
114 |
|
114 | |||
115 | assert_no_difference "Project.count(:all, :conditions => 'status = #{Project::STATUS_ARCHIVED}')" do |
|
115 | assert_no_difference "Project.count(:all, :conditions => 'status = #{Project::STATUS_ARCHIVED}')" do | |
116 | assert_equal false, @ecookbook.archive |
|
116 | assert_equal false, @ecookbook.archive | |
117 | end |
|
117 | end | |
118 | @ecookbook.reload |
|
118 | @ecookbook.reload | |
119 | assert @ecookbook.active? |
|
119 | assert @ecookbook.active? | |
120 | end |
|
120 | end | |
121 |
|
121 | |||
122 | def test_unarchive |
|
122 | def test_unarchive | |
123 | user = @ecookbook.members.first.user |
|
123 | user = @ecookbook.members.first.user | |
124 | @ecookbook.archive |
|
124 | @ecookbook.archive | |
125 | # A subproject of an archived project can not be unarchived |
|
125 | # A subproject of an archived project can not be unarchived | |
126 | assert !@ecookbook_sub1.unarchive |
|
126 | assert !@ecookbook_sub1.unarchive | |
127 |
|
127 | |||
128 | # Unarchive project |
|
128 | # Unarchive project | |
129 | assert @ecookbook.unarchive |
|
129 | assert @ecookbook.unarchive | |
130 | @ecookbook.reload |
|
130 | @ecookbook.reload | |
131 | assert @ecookbook.active? |
|
131 | assert @ecookbook.active? | |
132 | assert user.projects.include?(@ecookbook) |
|
132 | assert user.projects.include?(@ecookbook) | |
133 | # Subproject can now be unarchived |
|
133 | # Subproject can now be unarchived | |
134 | @ecookbook_sub1.reload |
|
134 | @ecookbook_sub1.reload | |
135 | assert @ecookbook_sub1.unarchive |
|
135 | assert @ecookbook_sub1.unarchive | |
136 | end |
|
136 | end | |
137 |
|
137 | |||
138 | def test_destroy |
|
138 | def test_destroy | |
139 | # 2 active members |
|
139 | # 2 active members | |
140 | assert_equal 2, @ecookbook.members.size |
|
140 | assert_equal 2, @ecookbook.members.size | |
141 | # and 1 is locked |
|
141 | # and 1 is locked | |
142 | assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size |
|
142 | assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size | |
143 | # some boards |
|
143 | # some boards | |
144 | assert @ecookbook.boards.any? |
|
144 | assert @ecookbook.boards.any? | |
145 |
|
145 | |||
146 | @ecookbook.destroy |
|
146 | @ecookbook.destroy | |
147 | # make sure that the project non longer exists |
|
147 | # make sure that the project non longer exists | |
148 | assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) } |
|
148 | assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) } | |
149 | # make sure related data was removed |
|
149 | # make sure related data was removed | |
150 | assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? |
|
150 | assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? | |
151 | assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? |
|
151 | assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? | |
152 | end |
|
152 | end | |
153 |
|
153 | |||
154 | def test_move_an_orphan_project_to_a_root_project |
|
154 | def test_move_an_orphan_project_to_a_root_project | |
155 | sub = Project.find(2) |
|
155 | sub = Project.find(2) | |
156 | sub.set_parent! @ecookbook |
|
156 | sub.set_parent! @ecookbook | |
157 | assert_equal @ecookbook.id, sub.parent.id |
|
157 | assert_equal @ecookbook.id, sub.parent.id | |
158 | @ecookbook.reload |
|
158 | @ecookbook.reload | |
159 | assert_equal 4, @ecookbook.children.size |
|
159 | assert_equal 4, @ecookbook.children.size | |
160 | end |
|
160 | end | |
161 |
|
161 | |||
162 | def test_move_an_orphan_project_to_a_subproject |
|
162 | def test_move_an_orphan_project_to_a_subproject | |
163 | sub = Project.find(2) |
|
163 | sub = Project.find(2) | |
164 | assert sub.set_parent!(@ecookbook_sub1) |
|
164 | assert sub.set_parent!(@ecookbook_sub1) | |
165 | end |
|
165 | end | |
166 |
|
166 | |||
167 | def test_move_a_root_project_to_a_project |
|
167 | def test_move_a_root_project_to_a_project | |
168 | sub = @ecookbook |
|
168 | sub = @ecookbook | |
169 | assert sub.set_parent!(Project.find(2)) |
|
169 | assert sub.set_parent!(Project.find(2)) | |
170 | end |
|
170 | end | |
171 |
|
171 | |||
172 | def test_should_not_move_a_project_to_its_children |
|
172 | def test_should_not_move_a_project_to_its_children | |
173 | sub = @ecookbook |
|
173 | sub = @ecookbook | |
174 | assert !(sub.set_parent!(Project.find(3))) |
|
174 | assert !(sub.set_parent!(Project.find(3))) | |
175 | end |
|
175 | end | |
176 |
|
176 | |||
177 | def test_set_parent_should_add_roots_in_alphabetical_order |
|
177 | def test_set_parent_should_add_roots_in_alphabetical_order | |
178 | ProjectCustomField.delete_all |
|
178 | ProjectCustomField.delete_all | |
179 | Project.delete_all |
|
179 | Project.delete_all | |
180 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil) |
|
180 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil) | |
181 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil) |
|
181 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil) | |
182 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil) |
|
182 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil) | |
183 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil) |
|
183 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil) | |
184 |
|
184 | |||
185 | assert_equal 4, Project.count |
|
185 | assert_equal 4, Project.count | |
186 | assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft) |
|
186 | assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft) | |
187 | end |
|
187 | end | |
188 |
|
188 | |||
189 | def test_set_parent_should_add_children_in_alphabetical_order |
|
189 | def test_set_parent_should_add_children_in_alphabetical_order | |
190 | ProjectCustomField.delete_all |
|
190 | ProjectCustomField.delete_all | |
191 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') |
|
191 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') | |
192 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent) |
|
192 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent) | |
193 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent) |
|
193 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent) | |
194 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent) |
|
194 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent) | |
195 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent) |
|
195 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent) | |
196 |
|
196 | |||
197 | parent.reload |
|
197 | parent.reload | |
198 | assert_equal 4, parent.children.size |
|
198 | assert_equal 4, parent.children.size | |
199 | assert_equal parent.children.sort_by(&:name), parent.children |
|
199 | assert_equal parent.children.sort_by(&:name), parent.children | |
200 | end |
|
200 | end | |
201 |
|
201 | |||
202 | def test_rebuild_should_sort_children_alphabetically |
|
202 | def test_rebuild_should_sort_children_alphabetically | |
203 | ProjectCustomField.delete_all |
|
203 | ProjectCustomField.delete_all | |
204 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') |
|
204 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') | |
205 | Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent) |
|
205 | Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent) | |
206 | Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent) |
|
206 | Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent) | |
207 | Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent) |
|
207 | Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent) | |
208 | Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent) |
|
208 | Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent) | |
209 |
|
209 | |||
210 | Project.update_all("lft = NULL, rgt = NULL") |
|
210 | Project.update_all("lft = NULL, rgt = NULL") | |
211 | Project.rebuild! |
|
211 | Project.rebuild! | |
212 |
|
212 | |||
213 | parent.reload |
|
213 | parent.reload | |
214 | assert_equal 4, parent.children.size |
|
214 | assert_equal 4, parent.children.size | |
215 | assert_equal parent.children.sort_by(&:name), parent.children |
|
215 | assert_equal parent.children.sort_by(&:name), parent.children | |
216 | end |
|
216 | end | |
217 |
|
217 | |||
218 |
|
218 | |||
219 | def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy |
|
219 | def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy | |
220 | # Parent issue with a hierarchy project's fixed version |
|
220 | # Parent issue with a hierarchy project's fixed version | |
221 | parent_issue = Issue.find(1) |
|
221 | parent_issue = Issue.find(1) | |
222 | parent_issue.update_attribute(:fixed_version_id, 4) |
|
222 | parent_issue.update_attribute(:fixed_version_id, 4) | |
223 | parent_issue.reload |
|
223 | parent_issue.reload | |
224 | assert_equal 4, parent_issue.fixed_version_id |
|
224 | assert_equal 4, parent_issue.fixed_version_id | |
225 |
|
225 | |||
226 | # Should keep fixed versions for the issues |
|
226 | # Should keep fixed versions for the issues | |
227 | issue_with_local_fixed_version = Issue.find(5) |
|
227 | issue_with_local_fixed_version = Issue.find(5) | |
228 | issue_with_local_fixed_version.update_attribute(:fixed_version_id, 4) |
|
228 | issue_with_local_fixed_version.update_attribute(:fixed_version_id, 4) | |
229 | issue_with_local_fixed_version.reload |
|
229 | issue_with_local_fixed_version.reload | |
230 | assert_equal 4, issue_with_local_fixed_version.fixed_version_id |
|
230 | assert_equal 4, issue_with_local_fixed_version.fixed_version_id | |
231 |
|
231 | |||
232 | # Local issue with hierarchy fixed_version |
|
232 | # Local issue with hierarchy fixed_version | |
233 | issue_with_hierarchy_fixed_version = Issue.find(13) |
|
233 | issue_with_hierarchy_fixed_version = Issue.find(13) | |
234 | issue_with_hierarchy_fixed_version.update_attribute(:fixed_version_id, 6) |
|
234 | issue_with_hierarchy_fixed_version.update_attribute(:fixed_version_id, 6) | |
235 | issue_with_hierarchy_fixed_version.reload |
|
235 | issue_with_hierarchy_fixed_version.reload | |
236 | assert_equal 6, issue_with_hierarchy_fixed_version.fixed_version_id |
|
236 | assert_equal 6, issue_with_hierarchy_fixed_version.fixed_version_id | |
237 |
|
237 | |||
238 | # Move project out of the issue's hierarchy |
|
238 | # Move project out of the issue's hierarchy | |
239 | moved_project = Project.find(3) |
|
239 | moved_project = Project.find(3) | |
240 | moved_project.set_parent!(Project.find(2)) |
|
240 | moved_project.set_parent!(Project.find(2)) | |
241 | parent_issue.reload |
|
241 | parent_issue.reload | |
242 | issue_with_local_fixed_version.reload |
|
242 | issue_with_local_fixed_version.reload | |
243 | issue_with_hierarchy_fixed_version.reload |
|
243 | issue_with_hierarchy_fixed_version.reload | |
244 |
|
244 | |||
245 | assert_equal 4, issue_with_local_fixed_version.fixed_version_id, "Fixed version was not keep on an issue local to the moved project" |
|
245 | assert_equal 4, issue_with_local_fixed_version.fixed_version_id, "Fixed version was not keep on an issue local to the moved project" | |
246 | assert_equal nil, issue_with_hierarchy_fixed_version.fixed_version_id, "Fixed version is still set after moving the Project out of the hierarchy where the version is defined in" |
|
246 | assert_equal nil, issue_with_hierarchy_fixed_version.fixed_version_id, "Fixed version is still set after moving the Project out of the hierarchy where the version is defined in" | |
247 | assert_equal nil, parent_issue.fixed_version_id, "Fixed version is still set after moving the Version out of the hierarchy for the issue." |
|
247 | assert_equal nil, parent_issue.fixed_version_id, "Fixed version is still set after moving the Version out of the hierarchy for the issue." | |
248 | end |
|
248 | end | |
249 |
|
249 | |||
250 | def test_parent |
|
250 | def test_parent | |
251 | p = Project.find(6).parent |
|
251 | p = Project.find(6).parent | |
252 | assert p.is_a?(Project) |
|
252 | assert p.is_a?(Project) | |
253 | assert_equal 5, p.id |
|
253 | assert_equal 5, p.id | |
254 | end |
|
254 | end | |
255 |
|
255 | |||
256 | def test_ancestors |
|
256 | def test_ancestors | |
257 | a = Project.find(6).ancestors |
|
257 | a = Project.find(6).ancestors | |
258 | assert a.first.is_a?(Project) |
|
258 | assert a.first.is_a?(Project) | |
259 | assert_equal [1, 5], a.collect(&:id) |
|
259 | assert_equal [1, 5], a.collect(&:id) | |
260 | end |
|
260 | end | |
261 |
|
261 | |||
262 | def test_root |
|
262 | def test_root | |
263 | r = Project.find(6).root |
|
263 | r = Project.find(6).root | |
264 | assert r.is_a?(Project) |
|
264 | assert r.is_a?(Project) | |
265 | assert_equal 1, r.id |
|
265 | assert_equal 1, r.id | |
266 | end |
|
266 | end | |
267 |
|
267 | |||
268 | def test_children |
|
268 | def test_children | |
269 | c = Project.find(1).children |
|
269 | c = Project.find(1).children | |
270 | assert c.first.is_a?(Project) |
|
270 | assert c.first.is_a?(Project) | |
271 | assert_equal [5, 3, 4], c.collect(&:id) |
|
271 | assert_equal [5, 3, 4], c.collect(&:id) | |
272 | end |
|
272 | end | |
273 |
|
273 | |||
274 | def test_descendants |
|
274 | def test_descendants | |
275 | d = Project.find(1).descendants |
|
275 | d = Project.find(1).descendants | |
276 | assert d.first.is_a?(Project) |
|
276 | assert d.first.is_a?(Project) | |
277 | assert_equal [5, 6, 3, 4], d.collect(&:id) |
|
277 | assert_equal [5, 6, 3, 4], d.collect(&:id) | |
278 | end |
|
278 | end | |
279 |
|
279 | |||
280 | def test_allowed_parents_should_be_empty_for_non_member_user |
|
280 | def test_allowed_parents_should_be_empty_for_non_member_user | |
281 | Role.non_member.add_permission!(:add_project) |
|
281 | Role.non_member.add_permission!(:add_project) | |
282 | user = User.find(9) |
|
282 | user = User.find(9) | |
283 | assert user.memberships.empty? |
|
283 | assert user.memberships.empty? | |
284 | User.current = user |
|
284 | User.current = user | |
285 | assert Project.new.allowed_parents.compact.empty? |
|
285 | assert Project.new.allowed_parents.compact.empty? | |
286 | end |
|
286 | end | |
287 |
|
287 | |||
|
288 | def test_allowed_parents_with_add_subprojects_permission | |||
|
289 | Role.find(1).remove_permission!(:add_project) | |||
|
290 | Role.find(1).add_permission!(:add_subprojects) | |||
|
291 | User.current = User.find(2) | |||
|
292 | # new project | |||
|
293 | assert !Project.new.allowed_parents.include?(nil) | |||
|
294 | assert Project.new.allowed_parents.include?(Project.find(1)) | |||
|
295 | # existing root project | |||
|
296 | assert Project.find(1).allowed_parents.include?(nil) | |||
|
297 | # existing child | |||
|
298 | assert Project.find(3).allowed_parents.include?(Project.find(1)) | |||
|
299 | assert !Project.find(3).allowed_parents.include?(nil) | |||
|
300 | end | |||
|
301 | ||||
|
302 | def test_allowed_parents_with_add_project_permission | |||
|
303 | Role.find(1).add_permission!(:add_project) | |||
|
304 | Role.find(1).remove_permission!(:add_subprojects) | |||
|
305 | User.current = User.find(2) | |||
|
306 | # new project | |||
|
307 | assert Project.new.allowed_parents.include?(nil) | |||
|
308 | assert !Project.new.allowed_parents.include?(Project.find(1)) | |||
|
309 | # existing root project | |||
|
310 | assert Project.find(1).allowed_parents.include?(nil) | |||
|
311 | # existing child | |||
|
312 | assert Project.find(3).allowed_parents.include?(Project.find(1)) | |||
|
313 | assert Project.find(3).allowed_parents.include?(nil) | |||
|
314 | end | |||
|
315 | ||||
|
316 | def test_allowed_parents_with_add_project_and_subprojects_permission | |||
|
317 | Role.find(1).add_permission!(:add_project) | |||
|
318 | Role.find(1).add_permission!(:add_subprojects) | |||
|
319 | User.current = User.find(2) | |||
|
320 | # new project | |||
|
321 | assert Project.new.allowed_parents.include?(nil) | |||
|
322 | assert Project.new.allowed_parents.include?(Project.find(1)) | |||
|
323 | # existing root project | |||
|
324 | assert Project.find(1).allowed_parents.include?(nil) | |||
|
325 | # existing child | |||
|
326 | assert Project.find(3).allowed_parents.include?(Project.find(1)) | |||
|
327 | assert Project.find(3).allowed_parents.include?(nil) | |||
|
328 | end | |||
|
329 | ||||
288 | def test_users_by_role |
|
330 | def test_users_by_role | |
289 | users_by_role = Project.find(1).users_by_role |
|
331 | users_by_role = Project.find(1).users_by_role | |
290 | assert_kind_of Hash, users_by_role |
|
332 | assert_kind_of Hash, users_by_role | |
291 | role = Role.find(1) |
|
333 | role = Role.find(1) | |
292 | assert_kind_of Array, users_by_role[role] |
|
334 | assert_kind_of Array, users_by_role[role] | |
293 | assert users_by_role[role].include?(User.find(2)) |
|
335 | assert users_by_role[role].include?(User.find(2)) | |
294 | end |
|
336 | end | |
295 |
|
337 | |||
296 | def test_rolled_up_trackers |
|
338 | def test_rolled_up_trackers | |
297 | parent = Project.find(1) |
|
339 | parent = Project.find(1) | |
298 | parent.trackers = Tracker.find([1,2]) |
|
340 | parent.trackers = Tracker.find([1,2]) | |
299 | child = parent.children.find(3) |
|
341 | child = parent.children.find(3) | |
300 |
|
342 | |||
301 | assert_equal [1, 2], parent.tracker_ids |
|
343 | assert_equal [1, 2], parent.tracker_ids | |
302 | assert_equal [2, 3], child.trackers.collect(&:id) |
|
344 | assert_equal [2, 3], child.trackers.collect(&:id) | |
303 |
|
345 | |||
304 | assert_kind_of Tracker, parent.rolled_up_trackers.first |
|
346 | assert_kind_of Tracker, parent.rolled_up_trackers.first | |
305 | assert_equal Tracker.find(1), parent.rolled_up_trackers.first |
|
347 | assert_equal Tracker.find(1), parent.rolled_up_trackers.first | |
306 |
|
348 | |||
307 | assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id) |
|
349 | assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id) | |
308 | assert_equal [2, 3], child.rolled_up_trackers.collect(&:id) |
|
350 | assert_equal [2, 3], child.rolled_up_trackers.collect(&:id) | |
309 | end |
|
351 | end | |
310 |
|
352 | |||
311 | def test_rolled_up_trackers_should_ignore_archived_subprojects |
|
353 | def test_rolled_up_trackers_should_ignore_archived_subprojects | |
312 | parent = Project.find(1) |
|
354 | parent = Project.find(1) | |
313 | parent.trackers = Tracker.find([1,2]) |
|
355 | parent.trackers = Tracker.find([1,2]) | |
314 | child = parent.children.find(3) |
|
356 | child = parent.children.find(3) | |
315 | child.trackers = Tracker.find([1,3]) |
|
357 | child.trackers = Tracker.find([1,3]) | |
316 | parent.children.each(&:archive) |
|
358 | parent.children.each(&:archive) | |
317 |
|
359 | |||
318 | assert_equal [1,2], parent.rolled_up_trackers.collect(&:id) |
|
360 | assert_equal [1,2], parent.rolled_up_trackers.collect(&:id) | |
319 | end |
|
361 | end | |
320 |
|
362 | |||
321 | def test_shared_versions_none_sharing |
|
363 | def test_shared_versions_none_sharing | |
322 | p = Project.find(5) |
|
364 | p = Project.find(5) | |
323 | v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none') |
|
365 | v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none') | |
324 | assert p.shared_versions.include?(v) |
|
366 | assert p.shared_versions.include?(v) | |
325 | assert !p.children.first.shared_versions.include?(v) |
|
367 | assert !p.children.first.shared_versions.include?(v) | |
326 | assert !p.root.shared_versions.include?(v) |
|
368 | assert !p.root.shared_versions.include?(v) | |
327 | assert !p.siblings.first.shared_versions.include?(v) |
|
369 | assert !p.siblings.first.shared_versions.include?(v) | |
328 | assert !p.root.siblings.first.shared_versions.include?(v) |
|
370 | assert !p.root.siblings.first.shared_versions.include?(v) | |
329 | end |
|
371 | end | |
330 |
|
372 | |||
331 | def test_shared_versions_descendants_sharing |
|
373 | def test_shared_versions_descendants_sharing | |
332 | p = Project.find(5) |
|
374 | p = Project.find(5) | |
333 | v = Version.create!(:name => 'descendants_sharing', :project => p, :sharing => 'descendants') |
|
375 | v = Version.create!(:name => 'descendants_sharing', :project => p, :sharing => 'descendants') | |
334 | assert p.shared_versions.include?(v) |
|
376 | assert p.shared_versions.include?(v) | |
335 | assert p.children.first.shared_versions.include?(v) |
|
377 | assert p.children.first.shared_versions.include?(v) | |
336 | assert !p.root.shared_versions.include?(v) |
|
378 | assert !p.root.shared_versions.include?(v) | |
337 | assert !p.siblings.first.shared_versions.include?(v) |
|
379 | assert !p.siblings.first.shared_versions.include?(v) | |
338 | assert !p.root.siblings.first.shared_versions.include?(v) |
|
380 | assert !p.root.siblings.first.shared_versions.include?(v) | |
339 | end |
|
381 | end | |
340 |
|
382 | |||
341 | def test_shared_versions_hierarchy_sharing |
|
383 | def test_shared_versions_hierarchy_sharing | |
342 | p = Project.find(5) |
|
384 | p = Project.find(5) | |
343 | v = Version.create!(:name => 'hierarchy_sharing', :project => p, :sharing => 'hierarchy') |
|
385 | v = Version.create!(:name => 'hierarchy_sharing', :project => p, :sharing => 'hierarchy') | |
344 | assert p.shared_versions.include?(v) |
|
386 | assert p.shared_versions.include?(v) | |
345 | assert p.children.first.shared_versions.include?(v) |
|
387 | assert p.children.first.shared_versions.include?(v) | |
346 | assert p.root.shared_versions.include?(v) |
|
388 | assert p.root.shared_versions.include?(v) | |
347 | assert !p.siblings.first.shared_versions.include?(v) |
|
389 | assert !p.siblings.first.shared_versions.include?(v) | |
348 | assert !p.root.siblings.first.shared_versions.include?(v) |
|
390 | assert !p.root.siblings.first.shared_versions.include?(v) | |
349 | end |
|
391 | end | |
350 |
|
392 | |||
351 | def test_shared_versions_tree_sharing |
|
393 | def test_shared_versions_tree_sharing | |
352 | p = Project.find(5) |
|
394 | p = Project.find(5) | |
353 | v = Version.create!(:name => 'tree_sharing', :project => p, :sharing => 'tree') |
|
395 | v = Version.create!(:name => 'tree_sharing', :project => p, :sharing => 'tree') | |
354 | assert p.shared_versions.include?(v) |
|
396 | assert p.shared_versions.include?(v) | |
355 | assert p.children.first.shared_versions.include?(v) |
|
397 | assert p.children.first.shared_versions.include?(v) | |
356 | assert p.root.shared_versions.include?(v) |
|
398 | assert p.root.shared_versions.include?(v) | |
357 | assert p.siblings.first.shared_versions.include?(v) |
|
399 | assert p.siblings.first.shared_versions.include?(v) | |
358 | assert !p.root.siblings.first.shared_versions.include?(v) |
|
400 | assert !p.root.siblings.first.shared_versions.include?(v) | |
359 | end |
|
401 | end | |
360 |
|
402 | |||
361 | def test_shared_versions_system_sharing |
|
403 | def test_shared_versions_system_sharing | |
362 | p = Project.find(5) |
|
404 | p = Project.find(5) | |
363 | v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system') |
|
405 | v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system') | |
364 | assert p.shared_versions.include?(v) |
|
406 | assert p.shared_versions.include?(v) | |
365 | assert p.children.first.shared_versions.include?(v) |
|
407 | assert p.children.first.shared_versions.include?(v) | |
366 | assert p.root.shared_versions.include?(v) |
|
408 | assert p.root.shared_versions.include?(v) | |
367 | assert p.siblings.first.shared_versions.include?(v) |
|
409 | assert p.siblings.first.shared_versions.include?(v) | |
368 | assert p.root.siblings.first.shared_versions.include?(v) |
|
410 | assert p.root.siblings.first.shared_versions.include?(v) | |
369 | end |
|
411 | end | |
370 |
|
412 | |||
371 | def test_shared_versions |
|
413 | def test_shared_versions | |
372 | parent = Project.find(1) |
|
414 | parent = Project.find(1) | |
373 | child = parent.children.find(3) |
|
415 | child = parent.children.find(3) | |
374 | private_child = parent.children.find(5) |
|
416 | private_child = parent.children.find(5) | |
375 |
|
417 | |||
376 | assert_equal [1,2,3], parent.version_ids.sort |
|
418 | assert_equal [1,2,3], parent.version_ids.sort | |
377 | assert_equal [4], child.version_ids |
|
419 | assert_equal [4], child.version_ids | |
378 | assert_equal [6], private_child.version_ids |
|
420 | assert_equal [6], private_child.version_ids | |
379 | assert_equal [7], Version.find_all_by_sharing('system').collect(&:id) |
|
421 | assert_equal [7], Version.find_all_by_sharing('system').collect(&:id) | |
380 |
|
422 | |||
381 | assert_equal 6, parent.shared_versions.size |
|
423 | assert_equal 6, parent.shared_versions.size | |
382 | parent.shared_versions.each do |version| |
|
424 | parent.shared_versions.each do |version| | |
383 | assert_kind_of Version, version |
|
425 | assert_kind_of Version, version | |
384 | end |
|
426 | end | |
385 |
|
427 | |||
386 | assert_equal [1,2,3,4,6,7], parent.shared_versions.collect(&:id).sort |
|
428 | assert_equal [1,2,3,4,6,7], parent.shared_versions.collect(&:id).sort | |
387 | end |
|
429 | end | |
388 |
|
430 | |||
389 | def test_shared_versions_should_ignore_archived_subprojects |
|
431 | def test_shared_versions_should_ignore_archived_subprojects | |
390 | parent = Project.find(1) |
|
432 | parent = Project.find(1) | |
391 | child = parent.children.find(3) |
|
433 | child = parent.children.find(3) | |
392 | child.archive |
|
434 | child.archive | |
393 | parent.reload |
|
435 | parent.reload | |
394 |
|
436 | |||
395 | assert_equal [1,2,3], parent.version_ids.sort |
|
437 | assert_equal [1,2,3], parent.version_ids.sort | |
396 | assert_equal [4], child.version_ids |
|
438 | assert_equal [4], child.version_ids | |
397 | assert !parent.shared_versions.collect(&:id).include?(4) |
|
439 | assert !parent.shared_versions.collect(&:id).include?(4) | |
398 | end |
|
440 | end | |
399 |
|
441 | |||
400 | def test_shared_versions_visible_to_user |
|
442 | def test_shared_versions_visible_to_user | |
401 | user = User.find(3) |
|
443 | user = User.find(3) | |
402 | parent = Project.find(1) |
|
444 | parent = Project.find(1) | |
403 | child = parent.children.find(5) |
|
445 | child = parent.children.find(5) | |
404 |
|
446 | |||
405 | assert_equal [1,2,3], parent.version_ids.sort |
|
447 | assert_equal [1,2,3], parent.version_ids.sort | |
406 | assert_equal [6], child.version_ids |
|
448 | assert_equal [6], child.version_ids | |
407 |
|
449 | |||
408 | versions = parent.shared_versions.visible(user) |
|
450 | versions = parent.shared_versions.visible(user) | |
409 |
|
451 | |||
410 | assert_equal 4, versions.size |
|
452 | assert_equal 4, versions.size | |
411 | versions.each do |version| |
|
453 | versions.each do |version| | |
412 | assert_kind_of Version, version |
|
454 | assert_kind_of Version, version | |
413 | end |
|
455 | end | |
414 |
|
456 | |||
415 | assert !versions.collect(&:id).include?(6) |
|
457 | assert !versions.collect(&:id).include?(6) | |
416 | end |
|
458 | end | |
417 |
|
459 | |||
418 |
|
460 | |||
419 | def test_next_identifier |
|
461 | def test_next_identifier | |
420 | ProjectCustomField.delete_all |
|
462 | ProjectCustomField.delete_all | |
421 | Project.create!(:name => 'last', :identifier => 'p2008040') |
|
463 | Project.create!(:name => 'last', :identifier => 'p2008040') | |
422 | assert_equal 'p2008041', Project.next_identifier |
|
464 | assert_equal 'p2008041', Project.next_identifier | |
423 | end |
|
465 | end | |
424 |
|
466 | |||
425 | def test_next_identifier_first_project |
|
467 | def test_next_identifier_first_project | |
426 | Project.delete_all |
|
468 | Project.delete_all | |
427 | assert_nil Project.next_identifier |
|
469 | assert_nil Project.next_identifier | |
428 | end |
|
470 | end | |
429 |
|
471 | |||
430 |
|
472 | |||
431 | def test_enabled_module_names_should_not_recreate_enabled_modules |
|
473 | def test_enabled_module_names_should_not_recreate_enabled_modules | |
432 | project = Project.find(1) |
|
474 | project = Project.find(1) | |
433 | # Remove one module |
|
475 | # Remove one module | |
434 | modules = project.enabled_modules.slice(0..-2) |
|
476 | modules = project.enabled_modules.slice(0..-2) | |
435 | assert modules.any? |
|
477 | assert modules.any? | |
436 | assert_difference 'EnabledModule.count', -1 do |
|
478 | assert_difference 'EnabledModule.count', -1 do | |
437 | project.enabled_module_names = modules.collect(&:name) |
|
479 | project.enabled_module_names = modules.collect(&:name) | |
438 | end |
|
480 | end | |
439 | project.reload |
|
481 | project.reload | |
440 | # Ids should be preserved |
|
482 | # Ids should be preserved | |
441 | assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort |
|
483 | assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort | |
442 | end |
|
484 | end | |
443 |
|
485 | |||
444 | def test_copy_from_existing_project |
|
486 | def test_copy_from_existing_project | |
445 | source_project = Project.find(1) |
|
487 | source_project = Project.find(1) | |
446 | copied_project = Project.copy_from(1) |
|
488 | copied_project = Project.copy_from(1) | |
447 |
|
489 | |||
448 | assert copied_project |
|
490 | assert copied_project | |
449 | # Cleared attributes |
|
491 | # Cleared attributes | |
450 | assert copied_project.id.blank? |
|
492 | assert copied_project.id.blank? | |
451 | assert copied_project.name.blank? |
|
493 | assert copied_project.name.blank? | |
452 | assert copied_project.identifier.blank? |
|
494 | assert copied_project.identifier.blank? | |
453 |
|
495 | |||
454 | # Duplicated attributes |
|
496 | # Duplicated attributes | |
455 | assert_equal source_project.description, copied_project.description |
|
497 | assert_equal source_project.description, copied_project.description | |
456 | assert_equal source_project.enabled_modules, copied_project.enabled_modules |
|
498 | assert_equal source_project.enabled_modules, copied_project.enabled_modules | |
457 | assert_equal source_project.trackers, copied_project.trackers |
|
499 | assert_equal source_project.trackers, copied_project.trackers | |
458 |
|
500 | |||
459 | # Default attributes |
|
501 | # Default attributes | |
460 | assert_equal 1, copied_project.status |
|
502 | assert_equal 1, copied_project.status | |
461 | end |
|
503 | end | |
462 |
|
504 | |||
463 | def test_activities_should_use_the_system_activities |
|
505 | def test_activities_should_use_the_system_activities | |
464 | project = Project.find(1) |
|
506 | project = Project.find(1) | |
465 | assert_equal project.activities, TimeEntryActivity.find(:all, :conditions => {:active => true} ) |
|
507 | assert_equal project.activities, TimeEntryActivity.find(:all, :conditions => {:active => true} ) | |
466 | end |
|
508 | end | |
467 |
|
509 | |||
468 |
|
510 | |||
469 | def test_activities_should_use_the_project_specific_activities |
|
511 | def test_activities_should_use_the_project_specific_activities | |
470 | project = Project.find(1) |
|
512 | project = Project.find(1) | |
471 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project}) |
|
513 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project}) | |
472 | assert overridden_activity.save! |
|
514 | assert overridden_activity.save! | |
473 |
|
515 | |||
474 | assert project.activities.include?(overridden_activity), "Project specific Activity not found" |
|
516 | assert project.activities.include?(overridden_activity), "Project specific Activity not found" | |
475 | end |
|
517 | end | |
476 |
|
518 | |||
477 | def test_activities_should_not_include_the_inactive_project_specific_activities |
|
519 | def test_activities_should_not_include_the_inactive_project_specific_activities | |
478 | project = Project.find(1) |
|
520 | project = Project.find(1) | |
479 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false}) |
|
521 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false}) | |
480 | assert overridden_activity.save! |
|
522 | assert overridden_activity.save! | |
481 |
|
523 | |||
482 | assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found" |
|
524 | assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found" | |
483 | end |
|
525 | end | |
484 |
|
526 | |||
485 | def test_activities_should_not_include_project_specific_activities_from_other_projects |
|
527 | def test_activities_should_not_include_project_specific_activities_from_other_projects | |
486 | project = Project.find(1) |
|
528 | project = Project.find(1) | |
487 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)}) |
|
529 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)}) | |
488 | assert overridden_activity.save! |
|
530 | assert overridden_activity.save! | |
489 |
|
531 | |||
490 | assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project" |
|
532 | assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project" | |
491 | end |
|
533 | end | |
492 |
|
534 | |||
493 | def test_activities_should_handle_nils |
|
535 | def test_activities_should_handle_nils | |
494 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(1), :parent => TimeEntryActivity.find(:first)}) |
|
536 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(1), :parent => TimeEntryActivity.find(:first)}) | |
495 | TimeEntryActivity.delete_all |
|
537 | TimeEntryActivity.delete_all | |
496 |
|
538 | |||
497 | # No activities |
|
539 | # No activities | |
498 | project = Project.find(1) |
|
540 | project = Project.find(1) | |
499 | assert project.activities.empty? |
|
541 | assert project.activities.empty? | |
500 |
|
542 | |||
501 | # No system, one overridden |
|
543 | # No system, one overridden | |
502 | assert overridden_activity.save! |
|
544 | assert overridden_activity.save! | |
503 | project.reload |
|
545 | project.reload | |
504 | assert_equal [overridden_activity], project.activities |
|
546 | assert_equal [overridden_activity], project.activities | |
505 | end |
|
547 | end | |
506 |
|
548 | |||
507 | def test_activities_should_override_system_activities_with_project_activities |
|
549 | def test_activities_should_override_system_activities_with_project_activities | |
508 | project = Project.find(1) |
|
550 | project = Project.find(1) | |
509 | parent_activity = TimeEntryActivity.find(:first) |
|
551 | parent_activity = TimeEntryActivity.find(:first) | |
510 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity}) |
|
552 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity}) | |
511 | assert overridden_activity.save! |
|
553 | assert overridden_activity.save! | |
512 |
|
554 | |||
513 | assert project.activities.include?(overridden_activity), "Project specific Activity not found" |
|
555 | assert project.activities.include?(overridden_activity), "Project specific Activity not found" | |
514 | assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden" |
|
556 | assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden" | |
515 | end |
|
557 | end | |
516 |
|
558 | |||
517 | def test_activities_should_include_inactive_activities_if_specified |
|
559 | def test_activities_should_include_inactive_activities_if_specified | |
518 | project = Project.find(1) |
|
560 | project = Project.find(1) | |
519 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false}) |
|
561 | overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false}) | |
520 | assert overridden_activity.save! |
|
562 | assert overridden_activity.save! | |
521 |
|
563 | |||
522 | assert project.activities(true).include?(overridden_activity), "Inactive Project specific Activity not found" |
|
564 | assert project.activities(true).include?(overridden_activity), "Inactive Project specific Activity not found" | |
523 | end |
|
565 | end | |
524 |
|
566 | |||
525 | test 'activities should not include active System activities if the project has an override that is inactive' do |
|
567 | test 'activities should not include active System activities if the project has an override that is inactive' do | |
526 | project = Project.find(1) |
|
568 | project = Project.find(1) | |
527 | system_activity = TimeEntryActivity.find_by_name('Design') |
|
569 | system_activity = TimeEntryActivity.find_by_name('Design') | |
528 | assert system_activity.active? |
|
570 | assert system_activity.active? | |
529 | overridden_activity = TimeEntryActivity.generate!(:project => project, :parent => system_activity, :active => false) |
|
571 | overridden_activity = TimeEntryActivity.generate!(:project => project, :parent => system_activity, :active => false) | |
530 | assert overridden_activity.save! |
|
572 | assert overridden_activity.save! | |
531 |
|
573 | |||
532 | assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found" |
|
574 | assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found" | |
533 | assert !project.activities.include?(system_activity), "System activity found when the project has an inactive override" |
|
575 | assert !project.activities.include?(system_activity), "System activity found when the project has an inactive override" | |
534 | end |
|
576 | end | |
535 |
|
577 | |||
536 | def test_close_completed_versions |
|
578 | def test_close_completed_versions | |
537 | Version.update_all("status = 'open'") |
|
579 | Version.update_all("status = 'open'") | |
538 | project = Project.find(1) |
|
580 | project = Project.find(1) | |
539 | assert_not_nil project.versions.detect {|v| v.completed? && v.status == 'open'} |
|
581 | assert_not_nil project.versions.detect {|v| v.completed? && v.status == 'open'} | |
540 | assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'} |
|
582 | assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'} | |
541 | project.close_completed_versions |
|
583 | project.close_completed_versions | |
542 | project.reload |
|
584 | project.reload | |
543 | assert_nil project.versions.detect {|v| v.completed? && v.status != 'closed'} |
|
585 | assert_nil project.versions.detect {|v| v.completed? && v.status != 'closed'} | |
544 | assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'} |
|
586 | assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'} | |
545 | end |
|
587 | end | |
546 |
|
588 | |||
547 | context "Project#copy" do |
|
589 | context "Project#copy" do | |
548 | setup do |
|
590 | setup do | |
549 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests |
|
591 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests | |
550 | Project.destroy_all :identifier => "copy-test" |
|
592 | Project.destroy_all :identifier => "copy-test" | |
551 | @source_project = Project.find(2) |
|
593 | @source_project = Project.find(2) | |
552 | @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') |
|
594 | @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') | |
553 | @project.trackers = @source_project.trackers |
|
595 | @project.trackers = @source_project.trackers | |
554 | @project.enabled_module_names = @source_project.enabled_modules.collect(&:name) |
|
596 | @project.enabled_module_names = @source_project.enabled_modules.collect(&:name) | |
555 | end |
|
597 | end | |
556 |
|
598 | |||
557 | should "copy issues" do |
|
599 | should "copy issues" do | |
558 | @source_project.issues << Issue.generate!(:status_id => 5, |
|
600 | @source_project.issues << Issue.generate!(:status_id => 5, | |
559 | :subject => "copy issue status", |
|
601 | :subject => "copy issue status", | |
560 | :tracker_id => 1, |
|
602 | :tracker_id => 1, | |
561 | :assigned_to_id => 2, |
|
603 | :assigned_to_id => 2, | |
562 | :project_id => @source_project.id) |
|
604 | :project_id => @source_project.id) | |
563 | assert @project.valid? |
|
605 | assert @project.valid? | |
564 | assert @project.issues.empty? |
|
606 | assert @project.issues.empty? | |
565 | assert @project.copy(@source_project) |
|
607 | assert @project.copy(@source_project) | |
566 |
|
608 | |||
567 | assert_equal @source_project.issues.size, @project.issues.size |
|
609 | assert_equal @source_project.issues.size, @project.issues.size | |
568 | @project.issues.each do |issue| |
|
610 | @project.issues.each do |issue| | |
569 | assert issue.valid? |
|
611 | assert issue.valid? | |
570 | assert ! issue.assigned_to.blank? |
|
612 | assert ! issue.assigned_to.blank? | |
571 | assert_equal @project, issue.project |
|
613 | assert_equal @project, issue.project | |
572 | end |
|
614 | end | |
573 |
|
615 | |||
574 | copied_issue = @project.issues.first(:conditions => {:subject => "copy issue status"}) |
|
616 | copied_issue = @project.issues.first(:conditions => {:subject => "copy issue status"}) | |
575 | assert copied_issue |
|
617 | assert copied_issue | |
576 | assert copied_issue.status |
|
618 | assert copied_issue.status | |
577 | assert_equal "Closed", copied_issue.status.name |
|
619 | assert_equal "Closed", copied_issue.status.name | |
578 | end |
|
620 | end | |
579 |
|
621 | |||
580 | should "change the new issues to use the copied version" do |
|
622 | should "change the new issues to use the copied version" do | |
581 | User.current = User.find(1) |
|
623 | User.current = User.find(1) | |
582 | assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open') |
|
624 | assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open') | |
583 | @source_project.versions << assigned_version |
|
625 | @source_project.versions << assigned_version | |
584 | assert_equal 3, @source_project.versions.size |
|
626 | assert_equal 3, @source_project.versions.size | |
585 | Issue.generate_for_project!(@source_project, |
|
627 | Issue.generate_for_project!(@source_project, | |
586 | :fixed_version_id => assigned_version.id, |
|
628 | :fixed_version_id => assigned_version.id, | |
587 | :subject => "change the new issues to use the copied version", |
|
629 | :subject => "change the new issues to use the copied version", | |
588 | :tracker_id => 1, |
|
630 | :tracker_id => 1, | |
589 | :project_id => @source_project.id) |
|
631 | :project_id => @source_project.id) | |
590 |
|
632 | |||
591 | assert @project.copy(@source_project) |
|
633 | assert @project.copy(@source_project) | |
592 | @project.reload |
|
634 | @project.reload | |
593 | copied_issue = @project.issues.first(:conditions => {:subject => "change the new issues to use the copied version"}) |
|
635 | copied_issue = @project.issues.first(:conditions => {:subject => "change the new issues to use the copied version"}) | |
594 |
|
636 | |||
595 | assert copied_issue |
|
637 | assert copied_issue | |
596 | assert copied_issue.fixed_version |
|
638 | assert copied_issue.fixed_version | |
597 | assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name |
|
639 | assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name | |
598 | assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record |
|
640 | assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record | |
599 | end |
|
641 | end | |
600 |
|
642 | |||
601 | should "copy issue relations" do |
|
643 | should "copy issue relations" do | |
602 | Setting.cross_project_issue_relations = '1' |
|
644 | Setting.cross_project_issue_relations = '1' | |
603 |
|
645 | |||
604 | second_issue = Issue.generate!(:status_id => 5, |
|
646 | second_issue = Issue.generate!(:status_id => 5, | |
605 | :subject => "copy issue relation", |
|
647 | :subject => "copy issue relation", | |
606 | :tracker_id => 1, |
|
648 | :tracker_id => 1, | |
607 | :assigned_to_id => 2, |
|
649 | :assigned_to_id => 2, | |
608 | :project_id => @source_project.id) |
|
650 | :project_id => @source_project.id) | |
609 | source_relation = IssueRelation.generate!(:issue_from => Issue.find(4), |
|
651 | source_relation = IssueRelation.generate!(:issue_from => Issue.find(4), | |
610 | :issue_to => second_issue, |
|
652 | :issue_to => second_issue, | |
611 | :relation_type => "relates") |
|
653 | :relation_type => "relates") | |
612 | source_relation_cross_project = IssueRelation.generate!(:issue_from => Issue.find(1), |
|
654 | source_relation_cross_project = IssueRelation.generate!(:issue_from => Issue.find(1), | |
613 | :issue_to => second_issue, |
|
655 | :issue_to => second_issue, | |
614 | :relation_type => "duplicates") |
|
656 | :relation_type => "duplicates") | |
615 |
|
657 | |||
616 | assert @project.copy(@source_project) |
|
658 | assert @project.copy(@source_project) | |
617 | assert_equal @source_project.issues.count, @project.issues.count |
|
659 | assert_equal @source_project.issues.count, @project.issues.count | |
618 | copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4 |
|
660 | copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4 | |
619 | copied_second_issue = @project.issues.find_by_subject("copy issue relation") |
|
661 | copied_second_issue = @project.issues.find_by_subject("copy issue relation") | |
620 |
|
662 | |||
621 | # First issue with a relation on project |
|
663 | # First issue with a relation on project | |
622 | assert_equal 1, copied_issue.relations.size, "Relation not copied" |
|
664 | assert_equal 1, copied_issue.relations.size, "Relation not copied" | |
623 | copied_relation = copied_issue.relations.first |
|
665 | copied_relation = copied_issue.relations.first | |
624 | assert_equal "relates", copied_relation.relation_type |
|
666 | assert_equal "relates", copied_relation.relation_type | |
625 | assert_equal copied_second_issue.id, copied_relation.issue_to_id |
|
667 | assert_equal copied_second_issue.id, copied_relation.issue_to_id | |
626 | assert_not_equal source_relation.id, copied_relation.id |
|
668 | assert_not_equal source_relation.id, copied_relation.id | |
627 |
|
669 | |||
628 | # Second issue with a cross project relation |
|
670 | # Second issue with a cross project relation | |
629 | assert_equal 2, copied_second_issue.relations.size, "Relation not copied" |
|
671 | assert_equal 2, copied_second_issue.relations.size, "Relation not copied" | |
630 | copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first |
|
672 | copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first | |
631 | assert_equal "duplicates", copied_relation.relation_type |
|
673 | assert_equal "duplicates", copied_relation.relation_type | |
632 | assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept" |
|
674 | assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept" | |
633 | assert_not_equal source_relation_cross_project.id, copied_relation.id |
|
675 | assert_not_equal source_relation_cross_project.id, copied_relation.id | |
634 | end |
|
676 | end | |
635 |
|
677 | |||
636 | should "copy memberships" do |
|
678 | should "copy memberships" do | |
637 | assert @project.valid? |
|
679 | assert @project.valid? | |
638 | assert @project.members.empty? |
|
680 | assert @project.members.empty? | |
639 | assert @project.copy(@source_project) |
|
681 | assert @project.copy(@source_project) | |
640 |
|
682 | |||
641 | assert_equal @source_project.memberships.size, @project.memberships.size |
|
683 | assert_equal @source_project.memberships.size, @project.memberships.size | |
642 | @project.memberships.each do |membership| |
|
684 | @project.memberships.each do |membership| | |
643 | assert membership |
|
685 | assert membership | |
644 | assert_equal @project, membership.project |
|
686 | assert_equal @project, membership.project | |
645 | end |
|
687 | end | |
646 | end |
|
688 | end | |
647 |
|
689 | |||
648 | should "copy project specific queries" do |
|
690 | should "copy project specific queries" do | |
649 | assert @project.valid? |
|
691 | assert @project.valid? | |
650 | assert @project.queries.empty? |
|
692 | assert @project.queries.empty? | |
651 | assert @project.copy(@source_project) |
|
693 | assert @project.copy(@source_project) | |
652 |
|
694 | |||
653 | assert_equal @source_project.queries.size, @project.queries.size |
|
695 | assert_equal @source_project.queries.size, @project.queries.size | |
654 | @project.queries.each do |query| |
|
696 | @project.queries.each do |query| | |
655 | assert query |
|
697 | assert query | |
656 | assert_equal @project, query.project |
|
698 | assert_equal @project, query.project | |
657 | end |
|
699 | end | |
658 | end |
|
700 | end | |
659 |
|
701 | |||
660 | should "copy versions" do |
|
702 | should "copy versions" do | |
661 | @source_project.versions << Version.generate! |
|
703 | @source_project.versions << Version.generate! | |
662 | @source_project.versions << Version.generate! |
|
704 | @source_project.versions << Version.generate! | |
663 |
|
705 | |||
664 | assert @project.versions.empty? |
|
706 | assert @project.versions.empty? | |
665 | assert @project.copy(@source_project) |
|
707 | assert @project.copy(@source_project) | |
666 |
|
708 | |||
667 | assert_equal @source_project.versions.size, @project.versions.size |
|
709 | assert_equal @source_project.versions.size, @project.versions.size | |
668 | @project.versions.each do |version| |
|
710 | @project.versions.each do |version| | |
669 | assert version |
|
711 | assert version | |
670 | assert_equal @project, version.project |
|
712 | assert_equal @project, version.project | |
671 | end |
|
713 | end | |
672 | end |
|
714 | end | |
673 |
|
715 | |||
674 | should "copy wiki" do |
|
716 | should "copy wiki" do | |
675 | assert_difference 'Wiki.count' do |
|
717 | assert_difference 'Wiki.count' do | |
676 | assert @project.copy(@source_project) |
|
718 | assert @project.copy(@source_project) | |
677 | end |
|
719 | end | |
678 |
|
720 | |||
679 | assert @project.wiki |
|
721 | assert @project.wiki | |
680 | assert_not_equal @source_project.wiki, @project.wiki |
|
722 | assert_not_equal @source_project.wiki, @project.wiki | |
681 | assert_equal "Start page", @project.wiki.start_page |
|
723 | assert_equal "Start page", @project.wiki.start_page | |
682 | end |
|
724 | end | |
683 |
|
725 | |||
684 | should "copy wiki pages and content" do |
|
726 | should "copy wiki pages and content" do | |
685 | assert @project.copy(@source_project) |
|
727 | assert @project.copy(@source_project) | |
686 |
|
728 | |||
687 | assert @project.wiki |
|
729 | assert @project.wiki | |
688 | assert_equal 1, @project.wiki.pages.length |
|
730 | assert_equal 1, @project.wiki.pages.length | |
689 |
|
731 | |||
690 | @project.wiki.pages.each do |wiki_page| |
|
732 | @project.wiki.pages.each do |wiki_page| | |
691 | assert wiki_page.content |
|
733 | assert wiki_page.content | |
692 | assert !@source_project.wiki.pages.include?(wiki_page) |
|
734 | assert !@source_project.wiki.pages.include?(wiki_page) | |
693 | end |
|
735 | end | |
694 | end |
|
736 | end | |
695 |
|
737 | |||
696 | should "copy issue categories" do |
|
738 | should "copy issue categories" do | |
697 | assert @project.copy(@source_project) |
|
739 | assert @project.copy(@source_project) | |
698 |
|
740 | |||
699 | assert_equal 2, @project.issue_categories.size |
|
741 | assert_equal 2, @project.issue_categories.size | |
700 | @project.issue_categories.each do |issue_category| |
|
742 | @project.issue_categories.each do |issue_category| | |
701 | assert !@source_project.issue_categories.include?(issue_category) |
|
743 | assert !@source_project.issue_categories.include?(issue_category) | |
702 | end |
|
744 | end | |
703 | end |
|
745 | end | |
704 |
|
746 | |||
705 | should "copy boards" do |
|
747 | should "copy boards" do | |
706 | assert @project.copy(@source_project) |
|
748 | assert @project.copy(@source_project) | |
707 |
|
749 | |||
708 | assert_equal 1, @project.boards.size |
|
750 | assert_equal 1, @project.boards.size | |
709 | @project.boards.each do |board| |
|
751 | @project.boards.each do |board| | |
710 | assert !@source_project.boards.include?(board) |
|
752 | assert !@source_project.boards.include?(board) | |
711 | end |
|
753 | end | |
712 | end |
|
754 | end | |
713 |
|
755 | |||
714 | should "change the new issues to use the copied issue categories" do |
|
756 | should "change the new issues to use the copied issue categories" do | |
715 | issue = Issue.find(4) |
|
757 | issue = Issue.find(4) | |
716 | issue.update_attribute(:category_id, 3) |
|
758 | issue.update_attribute(:category_id, 3) | |
717 |
|
759 | |||
718 | assert @project.copy(@source_project) |
|
760 | assert @project.copy(@source_project) | |
719 |
|
761 | |||
720 | @project.issues.each do |issue| |
|
762 | @project.issues.each do |issue| | |
721 | assert issue.category |
|
763 | assert issue.category | |
722 | assert_equal "Stock management", issue.category.name # Same name |
|
764 | assert_equal "Stock management", issue.category.name # Same name | |
723 | assert_not_equal IssueCategory.find(3), issue.category # Different record |
|
765 | assert_not_equal IssueCategory.find(3), issue.category # Different record | |
724 | end |
|
766 | end | |
725 | end |
|
767 | end | |
726 |
|
768 | |||
727 | should "limit copy with :only option" do |
|
769 | should "limit copy with :only option" do | |
728 | assert @project.members.empty? |
|
770 | assert @project.members.empty? | |
729 | assert @project.issue_categories.empty? |
|
771 | assert @project.issue_categories.empty? | |
730 | assert @source_project.issues.any? |
|
772 | assert @source_project.issues.any? | |
731 |
|
773 | |||
732 | assert @project.copy(@source_project, :only => ['members', 'issue_categories']) |
|
774 | assert @project.copy(@source_project, :only => ['members', 'issue_categories']) | |
733 |
|
775 | |||
734 | assert @project.members.any? |
|
776 | assert @project.members.any? | |
735 | assert @project.issue_categories.any? |
|
777 | assert @project.issue_categories.any? | |
736 | assert @project.issues.empty? |
|
778 | assert @project.issues.empty? | |
737 | end |
|
779 | end | |
738 |
|
780 | |||
739 | end |
|
781 | end | |
740 |
|
782 | |||
741 | end |
|
783 | end |
General Comments 0
You need to be logged in to leave comments.
Login now