##// END OF EJS Templates
Adds missing scope for r2595....
Jean-Philippe Lang -
r2528:f0c676d3df2c
parent child
Show More
@@ -1,292 +1,294
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 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
58
57 59 # Returns true if usr or current user is allowed to view the issue
58 60 def visible?(usr=nil)
59 61 (usr || User.current).allowed_to?(:view_issues, self.project)
60 62 end
61 63
62 64 def after_initialize
63 65 if new_record?
64 66 # set default values for new records only
65 67 self.status ||= IssueStatus.default
66 68 self.priority ||= Enumeration.priorities.default
67 69 end
68 70 end
69 71
70 72 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
71 73 def available_custom_fields
72 74 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
73 75 end
74 76
75 77 def copy_from(arg)
76 78 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
77 79 self.attributes = issue.attributes.dup
78 80 self.custom_values = issue.custom_values.collect {|v| v.clone}
79 81 self
80 82 end
81 83
82 84 # Moves/copies an issue to a new project and tracker
83 85 # Returns the moved/copied issue on success, false on failure
84 86 def move_to(new_project, new_tracker = nil, options = {})
85 87 options ||= {}
86 88 issue = options[:copy] ? self.clone : self
87 89 transaction do
88 90 if new_project && issue.project_id != new_project.id
89 91 # delete issue relations
90 92 unless Setting.cross_project_issue_relations?
91 93 issue.relations_from.clear
92 94 issue.relations_to.clear
93 95 end
94 96 # issue is moved to another project
95 97 # reassign to the category with same name if any
96 98 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
97 99 issue.category = new_category
98 100 issue.fixed_version = nil
99 101 issue.project = new_project
100 102 end
101 103 if new_tracker
102 104 issue.tracker = new_tracker
103 105 end
104 106 if options[:copy]
105 107 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
106 108 issue.status = self.status
107 109 end
108 110 if issue.save
109 111 unless options[:copy]
110 112 # Manually update project_id on related time entries
111 113 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
112 114 end
113 115 else
114 116 Issue.connection.rollback_db_transaction
115 117 return false
116 118 end
117 119 end
118 120 return issue
119 121 end
120 122
121 123 def priority_id=(pid)
122 124 self.priority = nil
123 125 write_attribute(:priority_id, pid)
124 126 end
125 127
126 128 def estimated_hours=(h)
127 129 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
128 130 end
129 131
130 132 def validate
131 133 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
132 134 errors.add :due_date, :not_a_date
133 135 end
134 136
135 137 if self.due_date and self.start_date and self.due_date < self.start_date
136 138 errors.add :due_date, :greater_than_start_date
137 139 end
138 140
139 141 if start_date && soonest_start && start_date < soonest_start
140 142 errors.add :start_date, :invalid
141 143 end
142 144 end
143 145
144 146 def validate_on_create
145 147 errors.add :tracker_id, :invalid unless project.trackers.include?(tracker)
146 148 end
147 149
148 150 def before_create
149 151 # default assignment based on category
150 152 if assigned_to.nil? && category && category.assigned_to
151 153 self.assigned_to = category.assigned_to
152 154 end
153 155 end
154 156
155 157 def before_save
156 158 if @current_journal
157 159 # attributes changes
158 160 (Issue.column_names - %w(id description)).each {|c|
159 161 @current_journal.details << JournalDetail.new(:property => 'attr',
160 162 :prop_key => c,
161 163 :old_value => @issue_before_change.send(c),
162 164 :value => send(c)) unless send(c)==@issue_before_change.send(c)
163 165 }
164 166 # custom fields changes
165 167 custom_values.each {|c|
166 168 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
167 169 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
168 170 @current_journal.details << JournalDetail.new(:property => 'cf',
169 171 :prop_key => c.custom_field_id,
170 172 :old_value => @custom_values_before_change[c.custom_field_id],
171 173 :value => c.value)
172 174 }
173 175 @current_journal.save
174 176 end
175 177 # Save the issue even if the journal is not saved (because empty)
176 178 true
177 179 end
178 180
179 181 def after_save
180 182 # Reload is needed in order to get the right status
181 183 reload
182 184
183 185 # Update start/due dates of following issues
184 186 relations_from.each(&:set_issue_to_dates)
185 187
186 188 # Close duplicates if the issue was closed
187 189 if @issue_before_change && !@issue_before_change.closed? && self.closed?
188 190 duplicates.each do |duplicate|
189 191 # Reload is need in case the duplicate was updated by a previous duplicate
190 192 duplicate.reload
191 193 # Don't re-close it if it's already closed
192 194 next if duplicate.closed?
193 195 # Same user and notes
194 196 duplicate.init_journal(@current_journal.user, @current_journal.notes)
195 197 duplicate.update_attribute :status, self.status
196 198 end
197 199 end
198 200 end
199 201
200 202 def init_journal(user, notes = "")
201 203 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
202 204 @issue_before_change = self.clone
203 205 @issue_before_change.status = self.status
204 206 @custom_values_before_change = {}
205 207 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
206 208 # Make sure updated_on is updated when adding a note.
207 209 updated_on_will_change!
208 210 @current_journal
209 211 end
210 212
211 213 # Return true if the issue is closed, otherwise false
212 214 def closed?
213 215 self.status.is_closed?
214 216 end
215 217
216 218 # Returns true if the issue is overdue
217 219 def overdue?
218 220 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
219 221 end
220 222
221 223 # Users the issue can be assigned to
222 224 def assignable_users
223 225 project.assignable_users
224 226 end
225 227
226 228 # Returns an array of status that user is able to apply
227 229 def new_statuses_allowed_to(user)
228 230 statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
229 231 statuses << status unless statuses.empty?
230 232 statuses.uniq.sort
231 233 end
232 234
233 235 # Returns the mail adresses of users that should be notified for the issue
234 236 def recipients
235 237 recipients = project.recipients
236 238 # Author and assignee are always notified unless they have been locked
237 239 recipients << author.mail if author && author.active?
238 240 recipients << assigned_to.mail if assigned_to && assigned_to.active?
239 241 recipients.compact.uniq
240 242 end
241 243
242 244 def spent_hours
243 245 @spent_hours ||= time_entries.sum(:hours) || 0
244 246 end
245 247
246 248 def relations
247 249 (relations_from + relations_to).sort
248 250 end
249 251
250 252 def all_dependent_issues
251 253 dependencies = []
252 254 relations_from.each do |relation|
253 255 dependencies << relation.issue_to
254 256 dependencies += relation.issue_to.all_dependent_issues
255 257 end
256 258 dependencies
257 259 end
258 260
259 261 # Returns an array of issues that duplicate this one
260 262 def duplicates
261 263 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
262 264 end
263 265
264 266 # Returns the due date or the target due date if any
265 267 # Used on gantt chart
266 268 def due_before
267 269 due_date || (fixed_version ? fixed_version.effective_date : nil)
268 270 end
269 271
270 272 def duration
271 273 (start_date && due_date) ? due_date - start_date : 0
272 274 end
273 275
274 276 def soonest_start
275 277 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
276 278 end
277 279
278 280 def to_s
279 281 "#{tracker} ##{id}: #{subject}"
280 282 end
281 283
282 284 private
283 285
284 286 # Callback on attachment deletion
285 287 def attachment_removed(obj)
286 288 journal = init_journal(User.current)
287 289 journal.details << JournalDetail.new(:property => 'attachment',
288 290 :prop_key => obj.id,
289 291 :old_value => obj.filename)
290 292 journal.save
291 293 end
292 294 end
General Comments 0
You need to be logged in to leave comments. Login now