@@ -1,72 +1,66 | |||
|
1 | 1 | class ContextMenusController < ApplicationController |
|
2 | 2 | helper :watchers |
|
3 | 3 | helper :issues |
|
4 | 4 | |
|
5 | 5 | def issues |
|
6 | 6 | @issues = Issue.visible.all(:conditions => {:id => params[:ids]}, :include => :project) |
|
7 | ||
|
8 | 7 | if (@issues.size == 1) |
|
9 | 8 | @issue = @issues.first |
|
10 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) | |
|
11 | else | |
|
12 | @allowed_statuses = @issues.map do |i| | |
|
13 | i.new_statuses_allowed_to(User.current) | |
|
14 | end.inject do |memo,s| | |
|
15 | memo & s | |
|
16 | end | |
|
17 | 9 | end |
|
10 | ||
|
11 | @allowed_statuses = @issues.map(&:new_statuses_allowed_to).inject{|memo,a| memo & a} | |
|
18 | 12 | @projects = @issues.collect(&:project).compact.uniq |
|
19 | 13 | @project = @projects.first if @projects.size == 1 |
|
20 | 14 | |
|
21 | 15 | @can = {:edit => User.current.allowed_to?(:edit_issues, @projects), |
|
22 | 16 | :log_time => (@project && User.current.allowed_to?(:log_time, @project)), |
|
23 | 17 | :update => (User.current.allowed_to?(:edit_issues, @projects) || (User.current.allowed_to?(:change_status, @projects) && !@allowed_statuses.blank?)), |
|
24 | 18 | :move => (@project && User.current.allowed_to?(:move_issues, @project)), |
|
25 | 19 | :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)), |
|
26 | 20 | :delete => User.current.allowed_to?(:delete_issues, @projects) |
|
27 | 21 | } |
|
28 | 22 | if @project |
|
29 | 23 | if @issue |
|
30 | 24 | @assignables = @issue.assignable_users |
|
31 | 25 | else |
|
32 | 26 | @assignables = @project.assignable_users |
|
33 | 27 | end |
|
34 | 28 | @trackers = @project.trackers |
|
35 | 29 | else |
|
36 | 30 | #when multiple projects, we only keep the intersection of each set |
|
37 | 31 | @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a} |
|
38 | 32 | @trackers = @projects.map(&:trackers).inject{|memo,t| memo & t} |
|
39 | 33 | end |
|
40 | 34 | |
|
41 | 35 | @priorities = IssuePriority.active.reverse |
|
42 | 36 | @back = back_url |
|
43 | 37 | |
|
44 | 38 | @options_by_custom_field = {} |
|
45 | 39 | if @can[:edit] |
|
46 | 40 | custom_fields = @issues.map(&:available_custom_fields).inject {|memo, f| memo & f}.select do |f| |
|
47 | 41 | %w(bool list user version).include?(f.field_format) && !f.multiple? |
|
48 | 42 | end |
|
49 | 43 | custom_fields.each do |field| |
|
50 | 44 | values = field.possible_values_options(@projects) |
|
51 | 45 | if values.any? |
|
52 | 46 | @options_by_custom_field[field] = values |
|
53 | 47 | end |
|
54 | 48 | end |
|
55 | 49 | end |
|
56 | 50 | |
|
57 | 51 | render :layout => false |
|
58 | 52 | end |
|
59 | 53 | |
|
60 | 54 | def time_entries |
|
61 | 55 | @time_entries = TimeEntry.all( |
|
62 | 56 | :conditions => {:id => params[:ids]}, :include => :project) |
|
63 | 57 | @projects = @time_entries.collect(&:project).compact.uniq |
|
64 | 58 | @project = @projects.first if @projects.size == 1 |
|
65 | 59 | @activities = TimeEntryActivity.shared.active |
|
66 | 60 | @can = {:edit => User.current.allowed_to?(:edit_time_entries, @projects), |
|
67 | 61 | :delete => User.current.allowed_to?(:edit_time_entries, @projects) |
|
68 | 62 | } |
|
69 | 63 | @back = back_url |
|
70 | 64 | render :layout => false |
|
71 | 65 | end |
|
72 | 66 | end |
@@ -1,1081 +1,1081 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | include Redmine::SafeAttributes |
|
20 | 20 | |
|
21 | 21 | belongs_to :project |
|
22 | 22 | belongs_to :tracker |
|
23 | 23 | belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' |
|
24 | 24 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
|
25 | 25 | belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id' |
|
26 | 26 | belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id' |
|
27 | 27 | belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id' |
|
28 | 28 | belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id' |
|
29 | 29 | |
|
30 | 30 | has_many :journals, :as => :journalized, :dependent => :destroy |
|
31 | 31 | has_many :time_entries, :dependent => :delete_all |
|
32 | 32 | has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC" |
|
33 | 33 | |
|
34 | 34 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all |
|
35 | 35 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all |
|
36 | 36 | |
|
37 | 37 | acts_as_nested_set :scope => 'root_id', :dependent => :destroy |
|
38 | 38 | acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed |
|
39 | 39 | acts_as_customizable |
|
40 | 40 | acts_as_watchable |
|
41 | 41 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], |
|
42 | 42 | :include => [:project, :journals], |
|
43 | 43 | # sort by id so that limited eager loading doesn't break with postgresql |
|
44 | 44 | :order_column => "#{table_name}.id" |
|
45 | 45 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"}, |
|
46 | 46 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}, |
|
47 | 47 | :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') } |
|
48 | 48 | |
|
49 | 49 | acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]}, |
|
50 | 50 | :author_key => :author_id |
|
51 | 51 | |
|
52 | 52 | DONE_RATIO_OPTIONS = %w(issue_field issue_status) |
|
53 | 53 | |
|
54 | 54 | attr_reader :current_journal |
|
55 | 55 | |
|
56 | 56 | validates_presence_of :subject, :priority, :project, :tracker, :author, :status |
|
57 | 57 | |
|
58 | 58 | validates_length_of :subject, :maximum => 255 |
|
59 | 59 | validates_inclusion_of :done_ratio, :in => 0..100 |
|
60 | 60 | validates_numericality_of :estimated_hours, :allow_nil => true |
|
61 | 61 | validate :validate_issue |
|
62 | 62 | |
|
63 | 63 | named_scope :visible, lambda {|*args| { :include => :project, |
|
64 | 64 | :conditions => Issue.visible_condition(args.shift || User.current, *args) } } |
|
65 | 65 | |
|
66 | 66 | named_scope :open, lambda {|*args| |
|
67 | 67 | is_closed = args.size > 0 ? !args.first : false |
|
68 | 68 | {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status} |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC" |
|
72 | 72 | named_scope :with_limit, lambda { |limit| { :limit => limit} } |
|
73 | 73 | named_scope :on_active_project, :include => [:status, :project, :tracker], |
|
74 | 74 | :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"] |
|
75 | 75 | |
|
76 | 76 | before_create :default_assign |
|
77 | 77 | before_save :close_duplicates, :update_done_ratio_from_issue_status |
|
78 | 78 | after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?} |
|
79 | 79 | after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal |
|
80 | 80 | after_destroy :update_parent_attributes |
|
81 | 81 | |
|
82 | 82 | # Returns a SQL conditions string used to find all issues visible by the specified user |
|
83 | 83 | def self.visible_condition(user, options={}) |
|
84 | 84 | Project.allowed_to_condition(user, :view_issues, options) do |role, user| |
|
85 | 85 | case role.issues_visibility |
|
86 | 86 | when 'all' |
|
87 | 87 | nil |
|
88 | 88 | when 'default' |
|
89 | 89 | user_ids = [user.id] + user.groups.map(&:id) |
|
90 | 90 | "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
91 | 91 | when 'own' |
|
92 | 92 | user_ids = [user.id] + user.groups.map(&:id) |
|
93 | 93 | "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
94 | 94 | else |
|
95 | 95 | '1=0' |
|
96 | 96 | end |
|
97 | 97 | end |
|
98 | 98 | end |
|
99 | 99 | |
|
100 | 100 | # Returns true if usr or current user is allowed to view the issue |
|
101 | 101 | def visible?(usr=nil) |
|
102 | 102 | (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user| |
|
103 | 103 | case role.issues_visibility |
|
104 | 104 | when 'all' |
|
105 | 105 | true |
|
106 | 106 | when 'default' |
|
107 | 107 | !self.is_private? || self.author == user || user.is_or_belongs_to?(assigned_to) |
|
108 | 108 | when 'own' |
|
109 | 109 | self.author == user || user.is_or_belongs_to?(assigned_to) |
|
110 | 110 | else |
|
111 | 111 | false |
|
112 | 112 | end |
|
113 | 113 | end |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | 116 | def initialize(attributes=nil, *args) |
|
117 | 117 | super |
|
118 | 118 | if new_record? |
|
119 | 119 | # set default values for new records only |
|
120 | 120 | self.status ||= IssueStatus.default |
|
121 | 121 | self.priority ||= IssuePriority.default |
|
122 | 122 | self.watcher_user_ids = [] |
|
123 | 123 | end |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields |
|
127 | 127 | def available_custom_fields |
|
128 | 128 | (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : [] |
|
129 | 129 | end |
|
130 | 130 | |
|
131 | 131 | # Copies attributes from another issue, arg can be an id or an Issue |
|
132 | 132 | def copy_from(arg, options={}) |
|
133 | 133 | issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg) |
|
134 | 134 | self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on") |
|
135 | 135 | self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} |
|
136 | 136 | self.status = issue.status |
|
137 | 137 | self.author = User.current |
|
138 | 138 | unless options[:attachments] == false |
|
139 | 139 | self.attachments = issue.attachments.map do |attachement| |
|
140 | 140 | attachement.copy(:container => self) |
|
141 | 141 | end |
|
142 | 142 | end |
|
143 | 143 | @copied_from = issue |
|
144 | 144 | self |
|
145 | 145 | end |
|
146 | 146 | |
|
147 | 147 | # Returns an unsaved copy of the issue |
|
148 | 148 | def copy(attributes=nil) |
|
149 | 149 | copy = self.class.new.copy_from(self) |
|
150 | 150 | copy.attributes = attributes if attributes |
|
151 | 151 | copy |
|
152 | 152 | end |
|
153 | 153 | |
|
154 | 154 | # Returns true if the issue is a copy |
|
155 | 155 | def copy? |
|
156 | 156 | @copied_from.present? |
|
157 | 157 | end |
|
158 | 158 | |
|
159 | 159 | # Moves/copies an issue to a new project and tracker |
|
160 | 160 | # Returns the moved/copied issue on success, false on failure |
|
161 | 161 | def move_to_project(new_project, new_tracker=nil, options={}) |
|
162 | 162 | ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead." |
|
163 | 163 | |
|
164 | 164 | if options[:copy] |
|
165 | 165 | issue = self.copy |
|
166 | 166 | else |
|
167 | 167 | issue = self |
|
168 | 168 | end |
|
169 | 169 | |
|
170 | 170 | issue.init_journal(User.current, options[:notes]) |
|
171 | 171 | |
|
172 | 172 | # Preserve previous behaviour |
|
173 | 173 | # #move_to_project doesn't change tracker automatically |
|
174 | 174 | issue.send :project=, new_project, true |
|
175 | 175 | if new_tracker |
|
176 | 176 | issue.tracker = new_tracker |
|
177 | 177 | end |
|
178 | 178 | # Allow bulk setting of attributes on the issue |
|
179 | 179 | if options[:attributes] |
|
180 | 180 | issue.attributes = options[:attributes] |
|
181 | 181 | end |
|
182 | 182 | |
|
183 | 183 | issue.save ? issue : false |
|
184 | 184 | end |
|
185 | 185 | |
|
186 | 186 | def status_id=(sid) |
|
187 | 187 | self.status = nil |
|
188 | 188 | write_attribute(:status_id, sid) |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | def priority_id=(pid) |
|
192 | 192 | self.priority = nil |
|
193 | 193 | write_attribute(:priority_id, pid) |
|
194 | 194 | end |
|
195 | 195 | |
|
196 | 196 | def category_id=(cid) |
|
197 | 197 | self.category = nil |
|
198 | 198 | write_attribute(:category_id, cid) |
|
199 | 199 | end |
|
200 | 200 | |
|
201 | 201 | def fixed_version_id=(vid) |
|
202 | 202 | self.fixed_version = nil |
|
203 | 203 | write_attribute(:fixed_version_id, vid) |
|
204 | 204 | end |
|
205 | 205 | |
|
206 | 206 | def tracker_id=(tid) |
|
207 | 207 | self.tracker = nil |
|
208 | 208 | result = write_attribute(:tracker_id, tid) |
|
209 | 209 | @custom_field_values = nil |
|
210 | 210 | result |
|
211 | 211 | end |
|
212 | 212 | |
|
213 | 213 | def project_id=(project_id) |
|
214 | 214 | if project_id.to_s != self.project_id.to_s |
|
215 | 215 | self.project = (project_id.present? ? Project.find_by_id(project_id) : nil) |
|
216 | 216 | end |
|
217 | 217 | end |
|
218 | 218 | |
|
219 | 219 | def project=(project, keep_tracker=false) |
|
220 | 220 | project_was = self.project |
|
221 | 221 | write_attribute(:project_id, project ? project.id : nil) |
|
222 | 222 | association_instance_set('project', project) |
|
223 | 223 | if project_was && project && project_was != project |
|
224 | 224 | unless keep_tracker || project.trackers.include?(tracker) |
|
225 | 225 | self.tracker = project.trackers.first |
|
226 | 226 | end |
|
227 | 227 | # Reassign to the category with same name if any |
|
228 | 228 | if category |
|
229 | 229 | self.category = project.issue_categories.find_by_name(category.name) |
|
230 | 230 | end |
|
231 | 231 | # Keep the fixed_version if it's still valid in the new_project |
|
232 | 232 | if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version) |
|
233 | 233 | self.fixed_version = nil |
|
234 | 234 | end |
|
235 | 235 | if parent && parent.project_id != project_id |
|
236 | 236 | self.parent_issue_id = nil |
|
237 | 237 | end |
|
238 | 238 | @custom_field_values = nil |
|
239 | 239 | end |
|
240 | 240 | end |
|
241 | 241 | |
|
242 | 242 | def description=(arg) |
|
243 | 243 | if arg.is_a?(String) |
|
244 | 244 | arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n") |
|
245 | 245 | end |
|
246 | 246 | write_attribute(:description, arg) |
|
247 | 247 | end |
|
248 | 248 | |
|
249 | 249 | # Overrides attributes= so that project and tracker get assigned first |
|
250 | 250 | def attributes_with_project_and_tracker_first=(new_attributes, *args) |
|
251 | 251 | return if new_attributes.nil? |
|
252 | 252 | attrs = new_attributes.dup |
|
253 | 253 | attrs.stringify_keys! |
|
254 | 254 | |
|
255 | 255 | %w(project project_id tracker tracker_id).each do |attr| |
|
256 | 256 | if attrs.has_key?(attr) |
|
257 | 257 | send "#{attr}=", attrs.delete(attr) |
|
258 | 258 | end |
|
259 | 259 | end |
|
260 | 260 | send :attributes_without_project_and_tracker_first=, attrs, *args |
|
261 | 261 | end |
|
262 | 262 | # Do not redefine alias chain on reload (see #4838) |
|
263 | 263 | alias_method_chain(:attributes=, :project_and_tracker_first) unless method_defined?(:attributes_without_project_and_tracker_first=) |
|
264 | 264 | |
|
265 | 265 | def estimated_hours=(h) |
|
266 | 266 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) |
|
267 | 267 | end |
|
268 | 268 | |
|
269 | 269 | safe_attributes 'project_id', |
|
270 | 270 | :if => lambda {|issue, user| |
|
271 | 271 | if issue.new_record? |
|
272 | 272 | issue.copy? |
|
273 | 273 | elsif user.allowed_to?(:move_issues, issue.project) |
|
274 | 274 | projects = Issue.allowed_target_projects_on_move(user) |
|
275 | 275 | projects.include?(issue.project) && projects.size > 1 |
|
276 | 276 | end |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | safe_attributes 'tracker_id', |
|
280 | 280 | 'status_id', |
|
281 | 281 | 'category_id', |
|
282 | 282 | 'assigned_to_id', |
|
283 | 283 | 'priority_id', |
|
284 | 284 | 'fixed_version_id', |
|
285 | 285 | 'subject', |
|
286 | 286 | 'description', |
|
287 | 287 | 'start_date', |
|
288 | 288 | 'due_date', |
|
289 | 289 | 'done_ratio', |
|
290 | 290 | 'estimated_hours', |
|
291 | 291 | 'custom_field_values', |
|
292 | 292 | 'custom_fields', |
|
293 | 293 | 'lock_version', |
|
294 | 294 | :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) } |
|
295 | 295 | |
|
296 | 296 | safe_attributes 'status_id', |
|
297 | 297 | 'assigned_to_id', |
|
298 | 298 | 'fixed_version_id', |
|
299 | 299 | 'done_ratio', |
|
300 | 300 | 'lock_version', |
|
301 | 301 | :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? } |
|
302 | 302 | |
|
303 | 303 | safe_attributes 'watcher_user_ids', |
|
304 | 304 | :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)} |
|
305 | 305 | |
|
306 | 306 | safe_attributes 'is_private', |
|
307 | 307 | :if => lambda {|issue, user| |
|
308 | 308 | user.allowed_to?(:set_issues_private, issue.project) || |
|
309 | 309 | (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project)) |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | safe_attributes 'parent_issue_id', |
|
313 | 313 | :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) && |
|
314 | 314 | user.allowed_to?(:manage_subtasks, issue.project)} |
|
315 | 315 | |
|
316 | 316 | # Safely sets attributes |
|
317 | 317 | # Should be called from controllers instead of #attributes= |
|
318 | 318 | # attr_accessible is too rough because we still want things like |
|
319 | 319 | # Issue.new(:project => foo) to work |
|
320 | 320 | def safe_attributes=(attrs, user=User.current) |
|
321 | 321 | return unless attrs.is_a?(Hash) |
|
322 | 322 | |
|
323 | 323 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed |
|
324 | 324 | attrs = delete_unsafe_attributes(attrs, user) |
|
325 | 325 | return if attrs.empty? |
|
326 | 326 | |
|
327 | 327 | # Project and Tracker must be set before since new_statuses_allowed_to depends on it. |
|
328 | 328 | if p = attrs.delete('project_id') |
|
329 | 329 | if allowed_target_projects(user).collect(&:id).include?(p.to_i) |
|
330 | 330 | self.project_id = p |
|
331 | 331 | end |
|
332 | 332 | end |
|
333 | 333 | |
|
334 | 334 | if t = attrs.delete('tracker_id') |
|
335 | 335 | self.tracker_id = t |
|
336 | 336 | end |
|
337 | 337 | |
|
338 | 338 | if attrs['status_id'] |
|
339 | 339 | unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i) |
|
340 | 340 | attrs.delete('status_id') |
|
341 | 341 | end |
|
342 | 342 | end |
|
343 | 343 | |
|
344 | 344 | unless leaf? |
|
345 | 345 | attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)} |
|
346 | 346 | end |
|
347 | 347 | |
|
348 | 348 | if attrs['parent_issue_id'].present? |
|
349 | 349 | attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i) |
|
350 | 350 | end |
|
351 | 351 | |
|
352 | 352 | # mass-assignment security bypass |
|
353 | 353 | self.send :attributes=, attrs, false |
|
354 | 354 | end |
|
355 | 355 | |
|
356 | 356 | def done_ratio |
|
357 | 357 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
358 | 358 | status.default_done_ratio |
|
359 | 359 | else |
|
360 | 360 | read_attribute(:done_ratio) |
|
361 | 361 | end |
|
362 | 362 | end |
|
363 | 363 | |
|
364 | 364 | def self.use_status_for_done_ratio? |
|
365 | 365 | Setting.issue_done_ratio == 'issue_status' |
|
366 | 366 | end |
|
367 | 367 | |
|
368 | 368 | def self.use_field_for_done_ratio? |
|
369 | 369 | Setting.issue_done_ratio == 'issue_field' |
|
370 | 370 | end |
|
371 | 371 | |
|
372 | 372 | def validate_issue |
|
373 | 373 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? |
|
374 | 374 | errors.add :due_date, :not_a_date |
|
375 | 375 | end |
|
376 | 376 | |
|
377 | 377 | if self.due_date and self.start_date and self.due_date < self.start_date |
|
378 | 378 | errors.add :due_date, :greater_than_start_date |
|
379 | 379 | end |
|
380 | 380 | |
|
381 | 381 | if start_date && soonest_start && start_date < soonest_start |
|
382 | 382 | errors.add :start_date, :invalid |
|
383 | 383 | end |
|
384 | 384 | |
|
385 | 385 | if fixed_version |
|
386 | 386 | if !assignable_versions.include?(fixed_version) |
|
387 | 387 | errors.add :fixed_version_id, :inclusion |
|
388 | 388 | elsif reopened? && fixed_version.closed? |
|
389 | 389 | errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version) |
|
390 | 390 | end |
|
391 | 391 | end |
|
392 | 392 | |
|
393 | 393 | # Checks that the issue can not be added/moved to a disabled tracker |
|
394 | 394 | if project && (tracker_id_changed? || project_id_changed?) |
|
395 | 395 | unless project.trackers.include?(tracker) |
|
396 | 396 | errors.add :tracker_id, :inclusion |
|
397 | 397 | end |
|
398 | 398 | end |
|
399 | 399 | |
|
400 | 400 | # Checks parent issue assignment |
|
401 | 401 | if @parent_issue |
|
402 | 402 | if @parent_issue.project_id != project_id |
|
403 | 403 | errors.add :parent_issue_id, :not_same_project |
|
404 | 404 | elsif !new_record? |
|
405 | 405 | # moving an existing issue |
|
406 | 406 | if @parent_issue.root_id != root_id |
|
407 | 407 | # we can always move to another tree |
|
408 | 408 | elsif move_possible?(@parent_issue) |
|
409 | 409 | # move accepted inside tree |
|
410 | 410 | else |
|
411 | 411 | errors.add :parent_issue_id, :not_a_valid_parent |
|
412 | 412 | end |
|
413 | 413 | end |
|
414 | 414 | end |
|
415 | 415 | end |
|
416 | 416 | |
|
417 | 417 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios |
|
418 | 418 | # even if the user turns off the setting later |
|
419 | 419 | def update_done_ratio_from_issue_status |
|
420 | 420 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
421 | 421 | self.done_ratio = status.default_done_ratio |
|
422 | 422 | end |
|
423 | 423 | end |
|
424 | 424 | |
|
425 | 425 | def init_journal(user, notes = "") |
|
426 | 426 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
427 | 427 | if new_record? |
|
428 | 428 | @current_journal.notify = false |
|
429 | 429 | else |
|
430 | 430 | @attributes_before_change = attributes.dup |
|
431 | 431 | @custom_values_before_change = {} |
|
432 | 432 | self.custom_field_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } |
|
433 | 433 | end |
|
434 | 434 | # Make sure updated_on is updated when adding a note. |
|
435 | 435 | updated_on_will_change! |
|
436 | 436 | @current_journal |
|
437 | 437 | end |
|
438 | 438 | |
|
439 | 439 | # Returns the id of the last journal or nil |
|
440 | 440 | def last_journal_id |
|
441 | 441 | if new_record? |
|
442 | 442 | nil |
|
443 | 443 | else |
|
444 | 444 | journals.first(:order => "#{Journal.table_name}.id DESC").try(:id) |
|
445 | 445 | end |
|
446 | 446 | end |
|
447 | 447 | |
|
448 | 448 | # Return true if the issue is closed, otherwise false |
|
449 | 449 | def closed? |
|
450 | 450 | self.status.is_closed? |
|
451 | 451 | end |
|
452 | 452 | |
|
453 | 453 | # Return true if the issue is being reopened |
|
454 | 454 | def reopened? |
|
455 | 455 | if !new_record? && status_id_changed? |
|
456 | 456 | status_was = IssueStatus.find_by_id(status_id_was) |
|
457 | 457 | status_new = IssueStatus.find_by_id(status_id) |
|
458 | 458 | if status_was && status_new && status_was.is_closed? && !status_new.is_closed? |
|
459 | 459 | return true |
|
460 | 460 | end |
|
461 | 461 | end |
|
462 | 462 | false |
|
463 | 463 | end |
|
464 | 464 | |
|
465 | 465 | # Return true if the issue is being closed |
|
466 | 466 | def closing? |
|
467 | 467 | if !new_record? && status_id_changed? |
|
468 | 468 | status_was = IssueStatus.find_by_id(status_id_was) |
|
469 | 469 | status_new = IssueStatus.find_by_id(status_id) |
|
470 | 470 | if status_was && status_new && !status_was.is_closed? && status_new.is_closed? |
|
471 | 471 | return true |
|
472 | 472 | end |
|
473 | 473 | end |
|
474 | 474 | false |
|
475 | 475 | end |
|
476 | 476 | |
|
477 | 477 | # Returns true if the issue is overdue |
|
478 | 478 | def overdue? |
|
479 | 479 | !due_date.nil? && (due_date < Date.today) && !status.is_closed? |
|
480 | 480 | end |
|
481 | 481 | |
|
482 | 482 | # Is the amount of work done less than it should for the due date |
|
483 | 483 | def behind_schedule? |
|
484 | 484 | return false if start_date.nil? || due_date.nil? |
|
485 | 485 | done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor |
|
486 | 486 | return done_date <= Date.today |
|
487 | 487 | end |
|
488 | 488 | |
|
489 | 489 | # Does this issue have children? |
|
490 | 490 | def children? |
|
491 | 491 | !leaf? |
|
492 | 492 | end |
|
493 | 493 | |
|
494 | 494 | # Users the issue can be assigned to |
|
495 | 495 | def assignable_users |
|
496 | 496 | users = project.assignable_users |
|
497 | 497 | users << author if author |
|
498 | 498 | users << assigned_to if assigned_to |
|
499 | 499 | users.uniq.sort |
|
500 | 500 | end |
|
501 | 501 | |
|
502 | 502 | # Versions that the issue can be assigned to |
|
503 | 503 | def assignable_versions |
|
504 | 504 | @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort |
|
505 | 505 | end |
|
506 | 506 | |
|
507 | 507 | # Returns true if this issue is blocked by another issue that is still open |
|
508 | 508 | def blocked? |
|
509 | 509 | !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil? |
|
510 | 510 | end |
|
511 | 511 | |
|
512 | 512 | # Returns an array of status that user is able to apply |
|
513 | def new_statuses_allowed_to(user, include_default=false) | |
|
513 | def new_statuses_allowed_to(user=User.current, include_default=false) | |
|
514 | 514 | statuses = status.find_new_statuses_allowed_to( |
|
515 | 515 | user.admin ? Role.all : user.roles_for_project(project), |
|
516 | 516 | tracker, |
|
517 | 517 | author == user, |
|
518 | 518 | assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id |
|
519 | 519 | ) |
|
520 | 520 | statuses << status unless statuses.empty? |
|
521 | 521 | statuses << IssueStatus.default if include_default |
|
522 | 522 | statuses = statuses.uniq.sort |
|
523 | 523 | blocked? ? statuses.reject {|s| s.is_closed?} : statuses |
|
524 | 524 | end |
|
525 | 525 | |
|
526 | 526 | def assigned_to_was |
|
527 | 527 | if assigned_to_id_changed? && assigned_to_id_was.present? |
|
528 | 528 | @assigned_to_was ||= User.find_by_id(assigned_to_id_was) |
|
529 | 529 | end |
|
530 | 530 | end |
|
531 | 531 | |
|
532 | 532 | # Returns the mail adresses of users that should be notified |
|
533 | 533 | def recipients |
|
534 | 534 | notified = [] |
|
535 | 535 | # Author and assignee are always notified unless they have been |
|
536 | 536 | # locked or don't want to be notified |
|
537 | 537 | notified << author if author |
|
538 | 538 | if assigned_to |
|
539 | 539 | notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to]) |
|
540 | 540 | end |
|
541 | 541 | if assigned_to_was |
|
542 | 542 | notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was]) |
|
543 | 543 | end |
|
544 | 544 | notified = notified.select {|u| u.active? && u.notify_about?(self)} |
|
545 | 545 | |
|
546 | 546 | notified += project.notified_users |
|
547 | 547 | notified.uniq! |
|
548 | 548 | # Remove users that can not view the issue |
|
549 | 549 | notified.reject! {|user| !visible?(user)} |
|
550 | 550 | notified.collect(&:mail) |
|
551 | 551 | end |
|
552 | 552 | |
|
553 | 553 | # Returns the number of hours spent on this issue |
|
554 | 554 | def spent_hours |
|
555 | 555 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
556 | 556 | end |
|
557 | 557 | |
|
558 | 558 | # Returns the total number of hours spent on this issue and its descendants |
|
559 | 559 | # |
|
560 | 560 | # Example: |
|
561 | 561 | # spent_hours => 0.0 |
|
562 | 562 | # spent_hours => 50.2 |
|
563 | 563 | def total_spent_hours |
|
564 | 564 | @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours", |
|
565 | 565 | :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0 |
|
566 | 566 | end |
|
567 | 567 | |
|
568 | 568 | def relations |
|
569 | 569 | @relations ||= (relations_from + relations_to).sort |
|
570 | 570 | end |
|
571 | 571 | |
|
572 | 572 | # Preloads relations for a collection of issues |
|
573 | 573 | def self.load_relations(issues) |
|
574 | 574 | if issues.any? |
|
575 | 575 | relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}]) |
|
576 | 576 | issues.each do |issue| |
|
577 | 577 | issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id} |
|
578 | 578 | end |
|
579 | 579 | end |
|
580 | 580 | end |
|
581 | 581 | |
|
582 | 582 | # Preloads visible spent time for a collection of issues |
|
583 | 583 | def self.load_visible_spent_hours(issues, user=User.current) |
|
584 | 584 | if issues.any? |
|
585 | 585 | hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id) |
|
586 | 586 | issues.each do |issue| |
|
587 | 587 | issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0) |
|
588 | 588 | end |
|
589 | 589 | end |
|
590 | 590 | end |
|
591 | 591 | |
|
592 | 592 | # Finds an issue relation given its id. |
|
593 | 593 | def find_relation(relation_id) |
|
594 | 594 | IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id]) |
|
595 | 595 | end |
|
596 | 596 | |
|
597 | 597 | def all_dependent_issues(except=[]) |
|
598 | 598 | except << self |
|
599 | 599 | dependencies = [] |
|
600 | 600 | relations_from.each do |relation| |
|
601 | 601 | if relation.issue_to && !except.include?(relation.issue_to) |
|
602 | 602 | dependencies << relation.issue_to |
|
603 | 603 | dependencies += relation.issue_to.all_dependent_issues(except) |
|
604 | 604 | end |
|
605 | 605 | end |
|
606 | 606 | dependencies |
|
607 | 607 | end |
|
608 | 608 | |
|
609 | 609 | # Returns an array of issues that duplicate this one |
|
610 | 610 | def duplicates |
|
611 | 611 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} |
|
612 | 612 | end |
|
613 | 613 | |
|
614 | 614 | # Returns the due date or the target due date if any |
|
615 | 615 | # Used on gantt chart |
|
616 | 616 | def due_before |
|
617 | 617 | due_date || (fixed_version ? fixed_version.effective_date : nil) |
|
618 | 618 | end |
|
619 | 619 | |
|
620 | 620 | # Returns the time scheduled for this issue. |
|
621 | 621 | # |
|
622 | 622 | # Example: |
|
623 | 623 | # Start Date: 2/26/09, End Date: 3/04/09 |
|
624 | 624 | # duration => 6 |
|
625 | 625 | def duration |
|
626 | 626 | (start_date && due_date) ? due_date - start_date : 0 |
|
627 | 627 | end |
|
628 | 628 | |
|
629 | 629 | def soonest_start |
|
630 | 630 | @soonest_start ||= ( |
|
631 | 631 | relations_to.collect{|relation| relation.successor_soonest_start} + |
|
632 | 632 | ancestors.collect(&:soonest_start) |
|
633 | 633 | ).compact.max |
|
634 | 634 | end |
|
635 | 635 | |
|
636 | 636 | def reschedule_after(date) |
|
637 | 637 | return if date.nil? |
|
638 | 638 | if leaf? |
|
639 | 639 | if start_date.nil? || start_date < date |
|
640 | 640 | self.start_date, self.due_date = date, date + duration |
|
641 | 641 | save |
|
642 | 642 | end |
|
643 | 643 | else |
|
644 | 644 | leaves.each do |leaf| |
|
645 | 645 | leaf.reschedule_after(date) |
|
646 | 646 | end |
|
647 | 647 | end |
|
648 | 648 | end |
|
649 | 649 | |
|
650 | 650 | def <=>(issue) |
|
651 | 651 | if issue.nil? |
|
652 | 652 | -1 |
|
653 | 653 | elsif root_id != issue.root_id |
|
654 | 654 | (root_id || 0) <=> (issue.root_id || 0) |
|
655 | 655 | else |
|
656 | 656 | (lft || 0) <=> (issue.lft || 0) |
|
657 | 657 | end |
|
658 | 658 | end |
|
659 | 659 | |
|
660 | 660 | def to_s |
|
661 | 661 | "#{tracker} ##{id}: #{subject}" |
|
662 | 662 | end |
|
663 | 663 | |
|
664 | 664 | # Returns a string of css classes that apply to the issue |
|
665 | 665 | def css_classes |
|
666 | 666 | s = "issue status-#{status.position} priority-#{priority.position}" |
|
667 | 667 | s << ' closed' if closed? |
|
668 | 668 | s << ' overdue' if overdue? |
|
669 | 669 | s << ' child' if child? |
|
670 | 670 | s << ' parent' unless leaf? |
|
671 | 671 | s << ' private' if is_private? |
|
672 | 672 | s << ' created-by-me' if User.current.logged? && author_id == User.current.id |
|
673 | 673 | s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id |
|
674 | 674 | s |
|
675 | 675 | end |
|
676 | 676 | |
|
677 | 677 | # Saves an issue, time_entry, attachments, and a journal from the parameters |
|
678 | 678 | # Returns false if save fails |
|
679 | 679 | def save_issue_with_child_records(params, existing_time_entry=nil) |
|
680 | 680 | Issue.transaction do |
|
681 | 681 | if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project) |
|
682 | 682 | @time_entry = existing_time_entry || TimeEntry.new |
|
683 | 683 | @time_entry.project = project |
|
684 | 684 | @time_entry.issue = self |
|
685 | 685 | @time_entry.user = User.current |
|
686 | 686 | @time_entry.spent_on = User.current.today |
|
687 | 687 | @time_entry.attributes = params[:time_entry] |
|
688 | 688 | self.time_entries << @time_entry |
|
689 | 689 | end |
|
690 | 690 | |
|
691 | 691 | if valid? |
|
692 | 692 | attachments = Attachment.attach_files(self, params[:attachments]) |
|
693 | 693 | # TODO: Rename hook |
|
694 | 694 | Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal}) |
|
695 | 695 | begin |
|
696 | 696 | if save |
|
697 | 697 | # TODO: Rename hook |
|
698 | 698 | Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal}) |
|
699 | 699 | else |
|
700 | 700 | raise ActiveRecord::Rollback |
|
701 | 701 | end |
|
702 | 702 | rescue ActiveRecord::StaleObjectError |
|
703 | 703 | attachments[:files].each(&:destroy) |
|
704 | 704 | raise ActiveRecord::StaleObjectError |
|
705 | 705 | end |
|
706 | 706 | end |
|
707 | 707 | end |
|
708 | 708 | end |
|
709 | 709 | |
|
710 | 710 | # Unassigns issues from +version+ if it's no longer shared with issue's project |
|
711 | 711 | def self.update_versions_from_sharing_change(version) |
|
712 | 712 | # Update issues assigned to the version |
|
713 | 713 | update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id]) |
|
714 | 714 | end |
|
715 | 715 | |
|
716 | 716 | # Unassigns issues from versions that are no longer shared |
|
717 | 717 | # after +project+ was moved |
|
718 | 718 | def self.update_versions_from_hierarchy_change(project) |
|
719 | 719 | moved_project_ids = project.self_and_descendants.reload.collect(&:id) |
|
720 | 720 | # Update issues of the moved projects and issues assigned to a version of a moved project |
|
721 | 721 | Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids]) |
|
722 | 722 | end |
|
723 | 723 | |
|
724 | 724 | def parent_issue_id=(arg) |
|
725 | 725 | parent_issue_id = arg.blank? ? nil : arg.to_i |
|
726 | 726 | if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id) |
|
727 | 727 | @parent_issue.id |
|
728 | 728 | else |
|
729 | 729 | @parent_issue = nil |
|
730 | 730 | nil |
|
731 | 731 | end |
|
732 | 732 | end |
|
733 | 733 | |
|
734 | 734 | def parent_issue_id |
|
735 | 735 | if instance_variable_defined? :@parent_issue |
|
736 | 736 | @parent_issue.nil? ? nil : @parent_issue.id |
|
737 | 737 | else |
|
738 | 738 | parent_id |
|
739 | 739 | end |
|
740 | 740 | end |
|
741 | 741 | |
|
742 | 742 | # Extracted from the ReportsController. |
|
743 | 743 | def self.by_tracker(project) |
|
744 | 744 | count_and_group_by(:project => project, |
|
745 | 745 | :field => 'tracker_id', |
|
746 | 746 | :joins => Tracker.table_name) |
|
747 | 747 | end |
|
748 | 748 | |
|
749 | 749 | def self.by_version(project) |
|
750 | 750 | count_and_group_by(:project => project, |
|
751 | 751 | :field => 'fixed_version_id', |
|
752 | 752 | :joins => Version.table_name) |
|
753 | 753 | end |
|
754 | 754 | |
|
755 | 755 | def self.by_priority(project) |
|
756 | 756 | count_and_group_by(:project => project, |
|
757 | 757 | :field => 'priority_id', |
|
758 | 758 | :joins => IssuePriority.table_name) |
|
759 | 759 | end |
|
760 | 760 | |
|
761 | 761 | def self.by_category(project) |
|
762 | 762 | count_and_group_by(:project => project, |
|
763 | 763 | :field => 'category_id', |
|
764 | 764 | :joins => IssueCategory.table_name) |
|
765 | 765 | end |
|
766 | 766 | |
|
767 | 767 | def self.by_assigned_to(project) |
|
768 | 768 | count_and_group_by(:project => project, |
|
769 | 769 | :field => 'assigned_to_id', |
|
770 | 770 | :joins => User.table_name) |
|
771 | 771 | end |
|
772 | 772 | |
|
773 | 773 | def self.by_author(project) |
|
774 | 774 | count_and_group_by(:project => project, |
|
775 | 775 | :field => 'author_id', |
|
776 | 776 | :joins => User.table_name) |
|
777 | 777 | end |
|
778 | 778 | |
|
779 | 779 | def self.by_subproject(project) |
|
780 | 780 | ActiveRecord::Base.connection.select_all("select s.id as status_id, |
|
781 | 781 | s.is_closed as closed, |
|
782 | 782 | #{Issue.table_name}.project_id as project_id, |
|
783 | 783 | count(#{Issue.table_name}.id) as total |
|
784 | 784 | from |
|
785 | 785 | #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s |
|
786 | 786 | where |
|
787 | 787 | #{Issue.table_name}.status_id=s.id |
|
788 | 788 | and #{Issue.table_name}.project_id = #{Project.table_name}.id |
|
789 | 789 | and #{visible_condition(User.current, :project => project, :with_subprojects => true)} |
|
790 | 790 | and #{Issue.table_name}.project_id <> #{project.id} |
|
791 | 791 | group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any? |
|
792 | 792 | end |
|
793 | 793 | # End ReportsController extraction |
|
794 | 794 | |
|
795 | 795 | # Returns an array of projects that user can assign the issue to |
|
796 | 796 | def allowed_target_projects(user=User.current) |
|
797 | 797 | if new_record? |
|
798 | 798 | Project.all(:conditions => Project.allowed_to_condition(user, :add_issues)) |
|
799 | 799 | else |
|
800 | 800 | self.class.allowed_target_projects_on_move(user) |
|
801 | 801 | end |
|
802 | 802 | end |
|
803 | 803 | |
|
804 | 804 | # Returns an array of projects that user can move issues to |
|
805 | 805 | def self.allowed_target_projects_on_move(user=User.current) |
|
806 | 806 | projects = [] |
|
807 | 807 | if user.admin? |
|
808 | 808 | # admin is allowed to move issues to any active (visible) project |
|
809 | 809 | projects = Project.visible(user).all |
|
810 | 810 | elsif user.logged? |
|
811 | 811 | if Role.non_member.allowed_to?(:move_issues) |
|
812 | 812 | projects = Project.visible(user).all |
|
813 | 813 | else |
|
814 | 814 | user.memberships.each {|m| projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}} |
|
815 | 815 | end |
|
816 | 816 | end |
|
817 | 817 | projects |
|
818 | 818 | end |
|
819 | 819 | |
|
820 | 820 | private |
|
821 | 821 | |
|
822 | 822 | def after_project_change |
|
823 | 823 | # Update project_id on related time entries |
|
824 | 824 | TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id}) |
|
825 | 825 | |
|
826 | 826 | # Delete issue relations |
|
827 | 827 | unless Setting.cross_project_issue_relations? |
|
828 | 828 | relations_from.clear |
|
829 | 829 | relations_to.clear |
|
830 | 830 | end |
|
831 | 831 | |
|
832 | 832 | # Move subtasks |
|
833 | 833 | children.each do |child| |
|
834 | 834 | # Change project and keep project |
|
835 | 835 | child.send :project=, project, true |
|
836 | 836 | unless child.save |
|
837 | 837 | raise ActiveRecord::Rollback |
|
838 | 838 | end |
|
839 | 839 | end |
|
840 | 840 | end |
|
841 | 841 | |
|
842 | 842 | def update_nested_set_attributes |
|
843 | 843 | if root_id.nil? |
|
844 | 844 | # issue was just created |
|
845 | 845 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id) |
|
846 | 846 | set_default_left_and_right |
|
847 | 847 | Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id]) |
|
848 | 848 | if @parent_issue |
|
849 | 849 | move_to_child_of(@parent_issue) |
|
850 | 850 | end |
|
851 | 851 | reload |
|
852 | 852 | elsif parent_issue_id != parent_id |
|
853 | 853 | former_parent_id = parent_id |
|
854 | 854 | # moving an existing issue |
|
855 | 855 | if @parent_issue && @parent_issue.root_id == root_id |
|
856 | 856 | # inside the same tree |
|
857 | 857 | move_to_child_of(@parent_issue) |
|
858 | 858 | else |
|
859 | 859 | # to another tree |
|
860 | 860 | unless root? |
|
861 | 861 | move_to_right_of(root) |
|
862 | 862 | reload |
|
863 | 863 | end |
|
864 | 864 | old_root_id = root_id |
|
865 | 865 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id ) |
|
866 | 866 | target_maxright = nested_set_scope.maximum(right_column_name) || 0 |
|
867 | 867 | offset = target_maxright + 1 - lft |
|
868 | 868 | Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}", |
|
869 | 869 | ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt]) |
|
870 | 870 | self[left_column_name] = lft + offset |
|
871 | 871 | self[right_column_name] = rgt + offset |
|
872 | 872 | if @parent_issue |
|
873 | 873 | move_to_child_of(@parent_issue) |
|
874 | 874 | end |
|
875 | 875 | end |
|
876 | 876 | reload |
|
877 | 877 | # delete invalid relations of all descendants |
|
878 | 878 | self_and_descendants.each do |issue| |
|
879 | 879 | issue.relations.each do |relation| |
|
880 | 880 | relation.destroy unless relation.valid? |
|
881 | 881 | end |
|
882 | 882 | end |
|
883 | 883 | # update former parent |
|
884 | 884 | recalculate_attributes_for(former_parent_id) if former_parent_id |
|
885 | 885 | end |
|
886 | 886 | remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue) |
|
887 | 887 | end |
|
888 | 888 | |
|
889 | 889 | def update_parent_attributes |
|
890 | 890 | recalculate_attributes_for(parent_id) if parent_id |
|
891 | 891 | end |
|
892 | 892 | |
|
893 | 893 | def recalculate_attributes_for(issue_id) |
|
894 | 894 | if issue_id && p = Issue.find_by_id(issue_id) |
|
895 | 895 | # priority = highest priority of children |
|
896 | 896 | if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority) |
|
897 | 897 | p.priority = IssuePriority.find_by_position(priority_position) |
|
898 | 898 | end |
|
899 | 899 | |
|
900 | 900 | # start/due dates = lowest/highest dates of children |
|
901 | 901 | p.start_date = p.children.minimum(:start_date) |
|
902 | 902 | p.due_date = p.children.maximum(:due_date) |
|
903 | 903 | if p.start_date && p.due_date && p.due_date < p.start_date |
|
904 | 904 | p.start_date, p.due_date = p.due_date, p.start_date |
|
905 | 905 | end |
|
906 | 906 | |
|
907 | 907 | # done ratio = weighted average ratio of leaves |
|
908 | 908 | unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio |
|
909 | 909 | leaves_count = p.leaves.count |
|
910 | 910 | if leaves_count > 0 |
|
911 | 911 | average = p.leaves.average(:estimated_hours).to_f |
|
912 | 912 | if average == 0 |
|
913 | 913 | average = 1 |
|
914 | 914 | end |
|
915 | 915 | done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :joins => :status).to_f |
|
916 | 916 | progress = done / (average * leaves_count) |
|
917 | 917 | p.done_ratio = progress.round |
|
918 | 918 | end |
|
919 | 919 | end |
|
920 | 920 | |
|
921 | 921 | # estimate = sum of leaves estimates |
|
922 | 922 | p.estimated_hours = p.leaves.sum(:estimated_hours).to_f |
|
923 | 923 | p.estimated_hours = nil if p.estimated_hours == 0.0 |
|
924 | 924 | |
|
925 | 925 | # ancestors will be recursively updated |
|
926 | 926 | p.save(false) |
|
927 | 927 | end |
|
928 | 928 | end |
|
929 | 929 | |
|
930 | 930 | # Update issues so their versions are not pointing to a |
|
931 | 931 | # fixed_version that is not shared with the issue's project |
|
932 | 932 | def self.update_versions(conditions=nil) |
|
933 | 933 | # Only need to update issues with a fixed_version from |
|
934 | 934 | # a different project and that is not systemwide shared |
|
935 | 935 | Issue.scoped(:conditions => conditions).all( |
|
936 | 936 | :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" + |
|
937 | 937 | " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" + |
|
938 | 938 | " AND #{Version.table_name}.sharing <> 'system'", |
|
939 | 939 | :include => [:project, :fixed_version] |
|
940 | 940 | ).each do |issue| |
|
941 | 941 | next if issue.project.nil? || issue.fixed_version.nil? |
|
942 | 942 | unless issue.project.shared_versions.include?(issue.fixed_version) |
|
943 | 943 | issue.init_journal(User.current) |
|
944 | 944 | issue.fixed_version = nil |
|
945 | 945 | issue.save |
|
946 | 946 | end |
|
947 | 947 | end |
|
948 | 948 | end |
|
949 | 949 | |
|
950 | 950 | # Callback on attachment deletion |
|
951 | 951 | def attachment_added(obj) |
|
952 | 952 | if @current_journal && !obj.new_record? |
|
953 | 953 | @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename) |
|
954 | 954 | end |
|
955 | 955 | end |
|
956 | 956 | |
|
957 | 957 | # Callback on attachment deletion |
|
958 | 958 | def attachment_removed(obj) |
|
959 | 959 | journal = init_journal(User.current) |
|
960 | 960 | journal.details << JournalDetail.new(:property => 'attachment', |
|
961 | 961 | :prop_key => obj.id, |
|
962 | 962 | :old_value => obj.filename) |
|
963 | 963 | journal.save |
|
964 | 964 | end |
|
965 | 965 | |
|
966 | 966 | # Default assignment based on category |
|
967 | 967 | def default_assign |
|
968 | 968 | if assigned_to.nil? && category && category.assigned_to |
|
969 | 969 | self.assigned_to = category.assigned_to |
|
970 | 970 | end |
|
971 | 971 | end |
|
972 | 972 | |
|
973 | 973 | # Updates start/due dates of following issues |
|
974 | 974 | def reschedule_following_issues |
|
975 | 975 | if start_date_changed? || due_date_changed? |
|
976 | 976 | relations_from.each do |relation| |
|
977 | 977 | relation.set_issue_to_dates |
|
978 | 978 | end |
|
979 | 979 | end |
|
980 | 980 | end |
|
981 | 981 | |
|
982 | 982 | # Closes duplicates if the issue is being closed |
|
983 | 983 | def close_duplicates |
|
984 | 984 | if closing? |
|
985 | 985 | duplicates.each do |duplicate| |
|
986 | 986 | # Reload is need in case the duplicate was updated by a previous duplicate |
|
987 | 987 | duplicate.reload |
|
988 | 988 | # Don't re-close it if it's already closed |
|
989 | 989 | next if duplicate.closed? |
|
990 | 990 | # Same user and notes |
|
991 | 991 | if @current_journal |
|
992 | 992 | duplicate.init_journal(@current_journal.user, @current_journal.notes) |
|
993 | 993 | end |
|
994 | 994 | duplicate.update_attribute :status, self.status |
|
995 | 995 | end |
|
996 | 996 | end |
|
997 | 997 | end |
|
998 | 998 | |
|
999 | 999 | # Saves the changes in a Journal |
|
1000 | 1000 | # Called after_save |
|
1001 | 1001 | def create_journal |
|
1002 | 1002 | if @current_journal |
|
1003 | 1003 | # attributes changes |
|
1004 | 1004 | if @attributes_before_change |
|
1005 | 1005 | (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on)).each {|c| |
|
1006 | 1006 | before = @attributes_before_change[c] |
|
1007 | 1007 | after = send(c) |
|
1008 | 1008 | next if before == after || (before.blank? && after.blank?) |
|
1009 | 1009 | @current_journal.details << JournalDetail.new(:property => 'attr', |
|
1010 | 1010 | :prop_key => c, |
|
1011 | 1011 | :old_value => before, |
|
1012 | 1012 | :value => after) |
|
1013 | 1013 | } |
|
1014 | 1014 | end |
|
1015 | 1015 | if @custom_values_before_change |
|
1016 | 1016 | # custom fields changes |
|
1017 | 1017 | custom_field_values.each {|c| |
|
1018 | 1018 | before = @custom_values_before_change[c.custom_field_id] |
|
1019 | 1019 | after = c.value |
|
1020 | 1020 | next if before == after || (before.blank? && after.blank?) |
|
1021 | 1021 | |
|
1022 | 1022 | if before.is_a?(Array) || after.is_a?(Array) |
|
1023 | 1023 | before = [before] unless before.is_a?(Array) |
|
1024 | 1024 | after = [after] unless after.is_a?(Array) |
|
1025 | 1025 | |
|
1026 | 1026 | # values removed |
|
1027 | 1027 | (before - after).reject(&:blank?).each do |value| |
|
1028 | 1028 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1029 | 1029 | :prop_key => c.custom_field_id, |
|
1030 | 1030 | :old_value => value, |
|
1031 | 1031 | :value => nil) |
|
1032 | 1032 | end |
|
1033 | 1033 | # values added |
|
1034 | 1034 | (after - before).reject(&:blank?).each do |value| |
|
1035 | 1035 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1036 | 1036 | :prop_key => c.custom_field_id, |
|
1037 | 1037 | :old_value => nil, |
|
1038 | 1038 | :value => value) |
|
1039 | 1039 | end |
|
1040 | 1040 | else |
|
1041 | 1041 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1042 | 1042 | :prop_key => c.custom_field_id, |
|
1043 | 1043 | :old_value => before, |
|
1044 | 1044 | :value => after) |
|
1045 | 1045 | end |
|
1046 | 1046 | } |
|
1047 | 1047 | end |
|
1048 | 1048 | @current_journal.save |
|
1049 | 1049 | # reset current journal |
|
1050 | 1050 | init_journal @current_journal.user, @current_journal.notes |
|
1051 | 1051 | end |
|
1052 | 1052 | end |
|
1053 | 1053 | |
|
1054 | 1054 | # Query generator for selecting groups of issue counts for a project |
|
1055 | 1055 | # based on specific criteria |
|
1056 | 1056 | # |
|
1057 | 1057 | # Options |
|
1058 | 1058 | # * project - Project to search in. |
|
1059 | 1059 | # * field - String. Issue field to key off of in the grouping. |
|
1060 | 1060 | # * joins - String. The table name to join against. |
|
1061 | 1061 | def self.count_and_group_by(options) |
|
1062 | 1062 | project = options.delete(:project) |
|
1063 | 1063 | select_field = options.delete(:field) |
|
1064 | 1064 | joins = options.delete(:joins) |
|
1065 | 1065 | |
|
1066 | 1066 | where = "#{Issue.table_name}.#{select_field}=j.id" |
|
1067 | 1067 | |
|
1068 | 1068 | ActiveRecord::Base.connection.select_all("select s.id as status_id, |
|
1069 | 1069 | s.is_closed as closed, |
|
1070 | 1070 | j.id as #{select_field}, |
|
1071 | 1071 | count(#{Issue.table_name}.id) as total |
|
1072 | 1072 | from |
|
1073 | 1073 | #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j |
|
1074 | 1074 | where |
|
1075 | 1075 | #{Issue.table_name}.status_id=s.id |
|
1076 | 1076 | and #{where} |
|
1077 | 1077 | and #{Issue.table_name}.project_id=#{Project.table_name}.id |
|
1078 | 1078 | and #{visible_condition(User.current, :project => project)} |
|
1079 | 1079 | group by s.id, s.is_closed, j.id") |
|
1080 | 1080 | end |
|
1081 | 1081 | end |
General Comments 0
You need to be logged in to leave comments.
Login now