##// END OF EJS Templates
Removes Issue.visible_by...
Jean-Philippe Lang -
r2340:2679150ed45b
parent child
Show More
@@ -1,296 +1,296
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 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 menu_item :issues, :only => [:changelog]
24 menu_item :issues, :only => [:changelog]
25
25
26 before_filter :find_project, :except => [ :index, :list, :add, :activity ]
26 before_filter :find_project, :except => [ :index, :list, :add, :activity ]
27 before_filter :find_optional_project, :only => :activity
27 before_filter :find_optional_project, :only => :activity
28 before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy, :activity ]
28 before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy, :activity ]
29 before_filter :require_admin, :only => [ :add, :archive, :unarchive, :destroy ]
29 before_filter :require_admin, :only => [ :add, :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 = Redmine::AccessControl.available_project_modules
73 @project.enabled_module_names = Redmine::AccessControl.available_project_modules
74 else
74 else
75 @project.enabled_module_names = params[:enabled_modules]
75 @project.enabled_module_names = params[:enabled_modules]
76 if @project.save
76 if @project.save
77 @project.set_parent!(params[:project]['parent_id']) if User.current.admin? && params[:project].has_key?('parent_id')
77 @project.set_parent!(params[:project]['parent_id']) if User.current.admin? && params[:project].has_key?('parent_id')
78 flash[:notice] = l(:notice_successful_create)
78 flash[:notice] = l(:notice_successful_create)
79 redirect_to :controller => 'admin', :action => 'projects'
79 redirect_to :controller => 'admin', :action => 'projects'
80 end
80 end
81 end
81 end
82 end
82 end
83
83
84 # Show @project
84 # Show @project
85 def show
85 def show
86 if params[:jump]
86 if params[:jump]
87 # try to redirect to the requested menu item
87 # try to redirect to the requested menu item
88 redirect_to_project_menu_item(@project, params[:jump]) && return
88 redirect_to_project_menu_item(@project, params[:jump]) && return
89 end
89 end
90
90
91 @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role}
91 @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role}
92 @subprojects = @project.children.visible
92 @subprojects = @project.children.visible
93 @ancestors = @project.ancestors.visible
93 @ancestors = @project.ancestors.visible
94 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
94 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
95 @trackers = @project.rolled_up_trackers
95 @trackers = @project.rolled_up_trackers
96
96
97 cond = @project.project_condition(Setting.display_subprojects_issues?)
97 cond = @project.project_condition(Setting.display_subprojects_issues?)
98 Issue.visible_by(User.current) do
98
99 @open_issues_by_tracker = Issue.count(:group => :tracker,
99 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
100 :include => [:project, :status, :tracker],
100 :include => [:project, :status, :tracker],
101 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
101 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
102 @total_issues_by_tracker = Issue.count(:group => :tracker,
102 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
103 :include => [:project, :status, :tracker],
103 :include => [:project, :status, :tracker],
104 :conditions => cond)
104 :conditions => cond)
105 end
105
106 TimeEntry.visible_by(User.current) do
106 TimeEntry.visible_by(User.current) do
107 @total_hours = TimeEntry.sum(:hours,
107 @total_hours = TimeEntry.sum(:hours,
108 :include => :project,
108 :include => :project,
109 :conditions => cond).to_f
109 :conditions => cond).to_f
110 end
110 end
111 @key = User.current.rss_key
111 @key = User.current.rss_key
112 end
112 end
113
113
114 def settings
114 def settings
115 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
115 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
116 @issue_category ||= IssueCategory.new
116 @issue_category ||= IssueCategory.new
117 @member ||= @project.members.new
117 @member ||= @project.members.new
118 @trackers = Tracker.all
118 @trackers = Tracker.all
119 @repository ||= @project.repository
119 @repository ||= @project.repository
120 @wiki ||= @project.wiki
120 @wiki ||= @project.wiki
121 end
121 end
122
122
123 # Edit @project
123 # Edit @project
124 def edit
124 def edit
125 if request.post?
125 if request.post?
126 @project.attributes = params[:project]
126 @project.attributes = params[:project]
127 if @project.save
127 if @project.save
128 @project.set_parent!(params[:project]['parent_id']) if User.current.admin? && params[:project].has_key?('parent_id')
128 @project.set_parent!(params[:project]['parent_id']) if User.current.admin? && params[:project].has_key?('parent_id')
129 flash[:notice] = l(:notice_successful_update)
129 flash[:notice] = l(:notice_successful_update)
130 redirect_to :action => 'settings', :id => @project
130 redirect_to :action => 'settings', :id => @project
131 else
131 else
132 settings
132 settings
133 render :action => 'settings'
133 render :action => 'settings'
134 end
134 end
135 end
135 end
136 end
136 end
137
137
138 def modules
138 def modules
139 @project.enabled_module_names = params[:enabled_modules]
139 @project.enabled_module_names = params[:enabled_modules]
140 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
140 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
141 end
141 end
142
142
143 def archive
143 def archive
144 @project.archive if request.post? && @project.active?
144 @project.archive if request.post? && @project.active?
145 redirect_to :controller => 'admin', :action => 'projects'
145 redirect_to :controller => 'admin', :action => 'projects'
146 end
146 end
147
147
148 def unarchive
148 def unarchive
149 @project.unarchive if request.post? && !@project.active?
149 @project.unarchive if request.post? && !@project.active?
150 redirect_to :controller => 'admin', :action => 'projects'
150 redirect_to :controller => 'admin', :action => 'projects'
151 end
151 end
152
152
153 # Delete @project
153 # Delete @project
154 def destroy
154 def destroy
155 @project_to_destroy = @project
155 @project_to_destroy = @project
156 if request.post? and params[:confirm]
156 if request.post? and params[:confirm]
157 @project_to_destroy.destroy
157 @project_to_destroy.destroy
158 redirect_to :controller => 'admin', :action => 'projects'
158 redirect_to :controller => 'admin', :action => 'projects'
159 end
159 end
160 # hide project in layout
160 # hide project in layout
161 @project = nil
161 @project = nil
162 end
162 end
163
163
164 # Add a new issue category to @project
164 # Add a new issue category to @project
165 def add_issue_category
165 def add_issue_category
166 @category = @project.issue_categories.build(params[:category])
166 @category = @project.issue_categories.build(params[:category])
167 if request.post? and @category.save
167 if request.post? and @category.save
168 respond_to do |format|
168 respond_to do |format|
169 format.html do
169 format.html do
170 flash[:notice] = l(:notice_successful_create)
170 flash[:notice] = l(:notice_successful_create)
171 redirect_to :action => 'settings', :tab => 'categories', :id => @project
171 redirect_to :action => 'settings', :tab => 'categories', :id => @project
172 end
172 end
173 format.js do
173 format.js do
174 # IE doesn't support the replace_html rjs method for select box options
174 # IE doesn't support the replace_html rjs method for select box options
175 render(:update) {|page| page.replace "issue_category_id",
175 render(:update) {|page| page.replace "issue_category_id",
176 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]')
176 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]')
177 }
177 }
178 end
178 end
179 end
179 end
180 end
180 end
181 end
181 end
182
182
183 # Add a new version to @project
183 # Add a new version to @project
184 def add_version
184 def add_version
185 @version = @project.versions.build(params[:version])
185 @version = @project.versions.build(params[:version])
186 if request.post? and @version.save
186 if request.post? and @version.save
187 flash[:notice] = l(:notice_successful_create)
187 flash[:notice] = l(:notice_successful_create)
188 redirect_to :action => 'settings', :tab => 'versions', :id => @project
188 redirect_to :action => 'settings', :tab => 'versions', :id => @project
189 end
189 end
190 end
190 end
191
191
192 def add_file
192 def add_file
193 if request.post?
193 if request.post?
194 container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
194 container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
195 attachments = attach_files(container, params[:attachments])
195 attachments = attach_files(container, params[:attachments])
196 if !attachments.empty? && Setting.notified_events.include?('file_added')
196 if !attachments.empty? && Setting.notified_events.include?('file_added')
197 Mailer.deliver_attachments_added(attachments)
197 Mailer.deliver_attachments_added(attachments)
198 end
198 end
199 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
199 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
200 return
200 return
201 end
201 end
202 @versions = @project.versions.sort
202 @versions = @project.versions.sort
203 end
203 end
204
204
205 def list_files
205 def list_files
206 sort_init 'filename', 'asc'
206 sort_init 'filename', 'asc'
207 sort_update 'filename' => "#{Attachment.table_name}.filename",
207 sort_update 'filename' => "#{Attachment.table_name}.filename",
208 'created_on' => "#{Attachment.table_name}.created_on",
208 'created_on' => "#{Attachment.table_name}.created_on",
209 'size' => "#{Attachment.table_name}.filesize",
209 'size' => "#{Attachment.table_name}.filesize",
210 'downloads' => "#{Attachment.table_name}.downloads"
210 'downloads' => "#{Attachment.table_name}.downloads"
211
211
212 @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)]
212 @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)]
213 @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse
213 @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse
214 render :layout => !request.xhr?
214 render :layout => !request.xhr?
215 end
215 end
216
216
217 # Show changelog for @project
217 # Show changelog for @project
218 def changelog
218 def changelog
219 @trackers = @project.trackers.find(:all, :conditions => ["is_in_chlog=?", true], :order => 'position')
219 @trackers = @project.trackers.find(:all, :conditions => ["is_in_chlog=?", true], :order => 'position')
220 retrieve_selected_tracker_ids(@trackers)
220 retrieve_selected_tracker_ids(@trackers)
221 @versions = @project.versions.sort
221 @versions = @project.versions.sort
222 end
222 end
223
223
224 def roadmap
224 def roadmap
225 @trackers = @project.trackers.find(:all, :conditions => ["is_in_roadmap=?", true])
225 @trackers = @project.trackers.find(:all, :conditions => ["is_in_roadmap=?", true])
226 retrieve_selected_tracker_ids(@trackers)
226 retrieve_selected_tracker_ids(@trackers)
227 @versions = @project.versions.sort
227 @versions = @project.versions.sort
228 @versions = @versions.select {|v| !v.completed? } unless params[:completed]
228 @versions = @versions.select {|v| !v.completed? } unless params[:completed]
229 end
229 end
230
230
231 def activity
231 def activity
232 @days = Setting.activity_days_default.to_i
232 @days = Setting.activity_days_default.to_i
233
233
234 if params[:from]
234 if params[:from]
235 begin; @date_to = params[:from].to_date + 1; rescue; end
235 begin; @date_to = params[:from].to_date + 1; rescue; end
236 end
236 end
237
237
238 @date_to ||= Date.today + 1
238 @date_to ||= Date.today + 1
239 @date_from = @date_to - @days
239 @date_from = @date_to - @days
240 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
240 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
241 @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id]))
241 @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id]))
242
242
243 @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project,
243 @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project,
244 :with_subprojects => @with_subprojects,
244 :with_subprojects => @with_subprojects,
245 :author => @author)
245 :author => @author)
246 @activity.scope_select {|t| !params["show_#{t}"].nil?}
246 @activity.scope_select {|t| !params["show_#{t}"].nil?}
247 @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty?
247 @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty?
248
248
249 events = @activity.events(@date_from, @date_to)
249 events = @activity.events(@date_from, @date_to)
250
250
251 respond_to do |format|
251 respond_to do |format|
252 format.html {
252 format.html {
253 @events_by_day = events.group_by(&:event_date)
253 @events_by_day = events.group_by(&:event_date)
254 render :layout => false if request.xhr?
254 render :layout => false if request.xhr?
255 }
255 }
256 format.atom {
256 format.atom {
257 title = l(:label_activity)
257 title = l(:label_activity)
258 if @author
258 if @author
259 title = @author.name
259 title = @author.name
260 elsif @activity.scope.size == 1
260 elsif @activity.scope.size == 1
261 title = l("label_#{@activity.scope.first.singularize}_plural")
261 title = l("label_#{@activity.scope.first.singularize}_plural")
262 end
262 end
263 render_feed(events, :title => "#{@project || Setting.app_title}: #{title}")
263 render_feed(events, :title => "#{@project || Setting.app_title}: #{title}")
264 }
264 }
265 end
265 end
266
266
267 rescue ActiveRecord::RecordNotFound
267 rescue ActiveRecord::RecordNotFound
268 render_404
268 render_404
269 end
269 end
270
270
271 private
271 private
272 # Find project of id params[:id]
272 # Find project of id params[:id]
273 # if not found, redirect to project list
273 # if not found, redirect to project list
274 # Used as a before_filter
274 # Used as a before_filter
275 def find_project
275 def find_project
276 @project = Project.find(params[:id])
276 @project = Project.find(params[:id])
277 rescue ActiveRecord::RecordNotFound
277 rescue ActiveRecord::RecordNotFound
278 render_404
278 render_404
279 end
279 end
280
280
281 def find_optional_project
281 def find_optional_project
282 return true unless params[:id]
282 return true unless params[:id]
283 @project = Project.find(params[:id])
283 @project = Project.find(params[:id])
284 authorize
284 authorize
285 rescue ActiveRecord::RecordNotFound
285 rescue ActiveRecord::RecordNotFound
286 render_404
286 render_404
287 end
287 end
288
288
289 def retrieve_selected_tracker_ids(selectable_trackers)
289 def retrieve_selected_tracker_ids(selectable_trackers)
290 if ids = params[:tracker_ids]
290 if ids = params[:tracker_ids]
291 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
291 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
292 else
292 else
293 @selected_tracker_ids = selectable_trackers.collect {|t| t.id.to_s }
293 @selected_tracker_ids = selectable_trackers.collect {|t| t.id.to_s }
294 end
294 end
295 end
295 end
296 end
296 end
@@ -1,293 +1,287
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 Issue < ActiveRecord::Base
18 class Issue < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 belongs_to :tracker
20 belongs_to :tracker
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27
27
28 has_many :journals, :as => :journalized, :dependent => :destroy
28 has_many :journals, :as => :journalized, :dependent => :destroy
29 has_many :time_entries, :dependent => :delete_all
29 has_many :time_entries, :dependent => :delete_all
30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
31
31
32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
34
34
35 acts_as_attachable :after_remove => :attachment_removed
35 acts_as_attachable :after_remove => :attachment_removed
36 acts_as_customizable
36 acts_as_customizable
37 acts_as_watchable
37 acts_as_watchable
38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
39 :include => [:project, :journals],
39 :include => [:project, :journals],
40 # sort by id so that limited eager loading doesn't break with postgresql
40 # sort by id so that limited eager loading doesn't break with postgresql
41 :order_column => "#{table_name}.id"
41 :order_column => "#{table_name}.id"
42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
45
45
46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
47 :author_key => :author_id
47 :author_key => :author_id
48
48
49 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
49 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
50 validates_length_of :subject, :maximum => 255
50 validates_length_of :subject, :maximum => 255
51 validates_inclusion_of :done_ratio, :in => 0..100
51 validates_inclusion_of :done_ratio, :in => 0..100
52 validates_numericality_of :estimated_hours, :allow_nil => true
52 validates_numericality_of :estimated_hours, :allow_nil => true
53
53
54 named_scope :visible, lambda {|*args| { :include => :project,
54 named_scope :visible, lambda {|*args| { :include => :project,
55 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
55 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
56
56
57 def after_initialize
57 def after_initialize
58 if new_record?
58 if new_record?
59 # set default values for new records only
59 # set default values for new records only
60 self.status ||= IssueStatus.default
60 self.status ||= IssueStatus.default
61 self.priority ||= Enumeration.default('IPRI')
61 self.priority ||= Enumeration.default('IPRI')
62 end
62 end
63 end
63 end
64
64
65 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
65 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
66 def available_custom_fields
66 def available_custom_fields
67 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
67 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
68 end
68 end
69
69
70 def copy_from(arg)
70 def copy_from(arg)
71 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
71 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
72 self.attributes = issue.attributes.dup
72 self.attributes = issue.attributes.dup
73 self.custom_values = issue.custom_values.collect {|v| v.clone}
73 self.custom_values = issue.custom_values.collect {|v| v.clone}
74 self
74 self
75 end
75 end
76
76
77 # Moves/copies an issue to a new project and tracker
77 # Moves/copies an issue to a new project and tracker
78 # Returns the moved/copied issue on success, false on failure
78 # Returns the moved/copied issue on success, false on failure
79 def move_to(new_project, new_tracker = nil, options = {})
79 def move_to(new_project, new_tracker = nil, options = {})
80 options ||= {}
80 options ||= {}
81 issue = options[:copy] ? self.clone : self
81 issue = options[:copy] ? self.clone : self
82 transaction do
82 transaction do
83 if new_project && issue.project_id != new_project.id
83 if new_project && issue.project_id != new_project.id
84 # delete issue relations
84 # delete issue relations
85 unless Setting.cross_project_issue_relations?
85 unless Setting.cross_project_issue_relations?
86 issue.relations_from.clear
86 issue.relations_from.clear
87 issue.relations_to.clear
87 issue.relations_to.clear
88 end
88 end
89 # issue is moved to another project
89 # issue is moved to another project
90 # reassign to the category with same name if any
90 # reassign to the category with same name if any
91 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
91 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
92 issue.category = new_category
92 issue.category = new_category
93 issue.fixed_version = nil
93 issue.fixed_version = nil
94 issue.project = new_project
94 issue.project = new_project
95 end
95 end
96 if new_tracker
96 if new_tracker
97 issue.tracker = new_tracker
97 issue.tracker = new_tracker
98 end
98 end
99 if options[:copy]
99 if options[:copy]
100 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
100 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
101 issue.status = self.status
101 issue.status = self.status
102 end
102 end
103 if issue.save
103 if issue.save
104 unless options[:copy]
104 unless options[:copy]
105 # Manually update project_id on related time entries
105 # Manually update project_id on related time entries
106 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
106 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
107 end
107 end
108 else
108 else
109 Issue.connection.rollback_db_transaction
109 Issue.connection.rollback_db_transaction
110 return false
110 return false
111 end
111 end
112 end
112 end
113 return issue
113 return issue
114 end
114 end
115
115
116 def priority_id=(pid)
116 def priority_id=(pid)
117 self.priority = nil
117 self.priority = nil
118 write_attribute(:priority_id, pid)
118 write_attribute(:priority_id, pid)
119 end
119 end
120
120
121 def estimated_hours=(h)
121 def estimated_hours=(h)
122 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
122 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
123 end
123 end
124
124
125 def validate
125 def validate
126 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
126 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
127 errors.add :due_date, :activerecord_error_not_a_date
127 errors.add :due_date, :activerecord_error_not_a_date
128 end
128 end
129
129
130 if self.due_date and self.start_date and self.due_date < self.start_date
130 if self.due_date and self.start_date and self.due_date < self.start_date
131 errors.add :due_date, :activerecord_error_greater_than_start_date
131 errors.add :due_date, :activerecord_error_greater_than_start_date
132 end
132 end
133
133
134 if start_date && soonest_start && start_date < soonest_start
134 if start_date && soonest_start && start_date < soonest_start
135 errors.add :start_date, :activerecord_error_invalid
135 errors.add :start_date, :activerecord_error_invalid
136 end
136 end
137 end
137 end
138
138
139 def validate_on_create
139 def validate_on_create
140 errors.add :tracker_id, :activerecord_error_invalid unless project.trackers.include?(tracker)
140 errors.add :tracker_id, :activerecord_error_invalid unless project.trackers.include?(tracker)
141 end
141 end
142
142
143 def before_create
143 def before_create
144 # default assignment based on category
144 # default assignment based on category
145 if assigned_to.nil? && category && category.assigned_to
145 if assigned_to.nil? && category && category.assigned_to
146 self.assigned_to = category.assigned_to
146 self.assigned_to = category.assigned_to
147 end
147 end
148 end
148 end
149
149
150 def before_save
150 def before_save
151 if @current_journal
151 if @current_journal
152 # attributes changes
152 # attributes changes
153 (Issue.column_names - %w(id description)).each {|c|
153 (Issue.column_names - %w(id description)).each {|c|
154 @current_journal.details << JournalDetail.new(:property => 'attr',
154 @current_journal.details << JournalDetail.new(:property => 'attr',
155 :prop_key => c,
155 :prop_key => c,
156 :old_value => @issue_before_change.send(c),
156 :old_value => @issue_before_change.send(c),
157 :value => send(c)) unless send(c)==@issue_before_change.send(c)
157 :value => send(c)) unless send(c)==@issue_before_change.send(c)
158 }
158 }
159 # custom fields changes
159 # custom fields changes
160 custom_values.each {|c|
160 custom_values.each {|c|
161 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
161 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
162 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
162 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
163 @current_journal.details << JournalDetail.new(:property => 'cf',
163 @current_journal.details << JournalDetail.new(:property => 'cf',
164 :prop_key => c.custom_field_id,
164 :prop_key => c.custom_field_id,
165 :old_value => @custom_values_before_change[c.custom_field_id],
165 :old_value => @custom_values_before_change[c.custom_field_id],
166 :value => c.value)
166 :value => c.value)
167 }
167 }
168 @current_journal.save
168 @current_journal.save
169 end
169 end
170 # Save the issue even if the journal is not saved (because empty)
170 # Save the issue even if the journal is not saved (because empty)
171 true
171 true
172 end
172 end
173
173
174 def after_save
174 def after_save
175 # Reload is needed in order to get the right status
175 # Reload is needed in order to get the right status
176 reload
176 reload
177
177
178 # Update start/due dates of following issues
178 # Update start/due dates of following issues
179 relations_from.each(&:set_issue_to_dates)
179 relations_from.each(&:set_issue_to_dates)
180
180
181 # Close duplicates if the issue was closed
181 # Close duplicates if the issue was closed
182 if @issue_before_change && !@issue_before_change.closed? && self.closed?
182 if @issue_before_change && !@issue_before_change.closed? && self.closed?
183 duplicates.each do |duplicate|
183 duplicates.each do |duplicate|
184 # Reload is need in case the duplicate was updated by a previous duplicate
184 # Reload is need in case the duplicate was updated by a previous duplicate
185 duplicate.reload
185 duplicate.reload
186 # Don't re-close it if it's already closed
186 # Don't re-close it if it's already closed
187 next if duplicate.closed?
187 next if duplicate.closed?
188 # Same user and notes
188 # Same user and notes
189 duplicate.init_journal(@current_journal.user, @current_journal.notes)
189 duplicate.init_journal(@current_journal.user, @current_journal.notes)
190 duplicate.update_attribute :status, self.status
190 duplicate.update_attribute :status, self.status
191 end
191 end
192 end
192 end
193 end
193 end
194
194
195 def init_journal(user, notes = "")
195 def init_journal(user, notes = "")
196 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
196 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
197 @issue_before_change = self.clone
197 @issue_before_change = self.clone
198 @issue_before_change.status = self.status
198 @issue_before_change.status = self.status
199 @custom_values_before_change = {}
199 @custom_values_before_change = {}
200 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
200 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
201 # Make sure updated_on is updated when adding a note.
201 # Make sure updated_on is updated when adding a note.
202 updated_on_will_change!
202 updated_on_will_change!
203 @current_journal
203 @current_journal
204 end
204 end
205
205
206 # Return true if the issue is closed, otherwise false
206 # Return true if the issue is closed, otherwise false
207 def closed?
207 def closed?
208 self.status.is_closed?
208 self.status.is_closed?
209 end
209 end
210
210
211 # Returns true if the issue is overdue
211 # Returns true if the issue is overdue
212 def overdue?
212 def overdue?
213 !due_date.nil? && (due_date < Date.today)
213 !due_date.nil? && (due_date < Date.today)
214 end
214 end
215
215
216 # Users the issue can be assigned to
216 # Users the issue can be assigned to
217 def assignable_users
217 def assignable_users
218 project.assignable_users
218 project.assignable_users
219 end
219 end
220
220
221 # Returns an array of status that user is able to apply
221 # Returns an array of status that user is able to apply
222 def new_statuses_allowed_to(user)
222 def new_statuses_allowed_to(user)
223 statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
223 statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
224 statuses << status unless statuses.empty?
224 statuses << status unless statuses.empty?
225 statuses.uniq.sort
225 statuses.uniq.sort
226 end
226 end
227
227
228 # Returns the mail adresses of users that should be notified for the issue
228 # Returns the mail adresses of users that should be notified for the issue
229 def recipients
229 def recipients
230 recipients = project.recipients
230 recipients = project.recipients
231 # Author and assignee are always notified unless they have been locked
231 # Author and assignee are always notified unless they have been locked
232 recipients << author.mail if author && author.active?
232 recipients << author.mail if author && author.active?
233 recipients << assigned_to.mail if assigned_to && assigned_to.active?
233 recipients << assigned_to.mail if assigned_to && assigned_to.active?
234 recipients.compact.uniq
234 recipients.compact.uniq
235 end
235 end
236
236
237 def spent_hours
237 def spent_hours
238 @spent_hours ||= time_entries.sum(:hours) || 0
238 @spent_hours ||= time_entries.sum(:hours) || 0
239 end
239 end
240
240
241 def relations
241 def relations
242 (relations_from + relations_to).sort
242 (relations_from + relations_to).sort
243 end
243 end
244
244
245 def all_dependent_issues
245 def all_dependent_issues
246 dependencies = []
246 dependencies = []
247 relations_from.each do |relation|
247 relations_from.each do |relation|
248 dependencies << relation.issue_to
248 dependencies << relation.issue_to
249 dependencies += relation.issue_to.all_dependent_issues
249 dependencies += relation.issue_to.all_dependent_issues
250 end
250 end
251 dependencies
251 dependencies
252 end
252 end
253
253
254 # Returns an array of issues that duplicate this one
254 # Returns an array of issues that duplicate this one
255 def duplicates
255 def duplicates
256 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
256 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
257 end
257 end
258
258
259 # Returns the due date or the target due date if any
259 # Returns the due date or the target due date if any
260 # Used on gantt chart
260 # Used on gantt chart
261 def due_before
261 def due_before
262 due_date || (fixed_version ? fixed_version.effective_date : nil)
262 due_date || (fixed_version ? fixed_version.effective_date : nil)
263 end
263 end
264
264
265 def duration
265 def duration
266 (start_date && due_date) ? due_date - start_date : 0
266 (start_date && due_date) ? due_date - start_date : 0
267 end
267 end
268
268
269 def soonest_start
269 def soonest_start
270 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
270 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
271 end
271 end
272
272
273 def self.visible_by(usr)
274 with_scope(:find => { :conditions => Project.visible_by(usr) }) do
275 yield
276 end
277 end
278
279 def to_s
273 def to_s
280 "#{tracker} ##{id}: #{subject}"
274 "#{tracker} ##{id}: #{subject}"
281 end
275 end
282
276
283 private
277 private
284
278
285 # Callback on attachment deletion
279 # Callback on attachment deletion
286 def attachment_removed(obj)
280 def attachment_removed(obj)
287 journal = init_journal(User.current)
281 journal = init_journal(User.current)
288 journal.details << JournalDetail.new(:property => 'attachment',
282 journal.details << JournalDetail.new(:property => 'attachment',
289 :prop_key => obj.id,
283 :prop_key => obj.id,
290 :old_value => obj.filename)
284 :old_value => obj.filename)
291 journal.save
285 journal.save
292 end
286 end
293 end
287 end
General Comments 0
You need to be logged in to leave comments. Login now