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