@@ -58,7 +58,8 class Issue < ActiveRecord::Base | |||
|
58 | 58 | |
|
59 | 59 | named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status |
|
60 | 60 | |
|
61 | before_save :update_done_ratio_from_issue_status | |
|
61 | before_create :default_assign | |
|
62 | before_save :reschedule_following_issues, :close_duplicates, :update_done_ratio_from_issue_status | |
|
62 | 63 | after_save :create_journal |
|
63 | 64 | |
|
64 | 65 | # Returns true if usr or current user is allowed to view the issue |
@@ -242,13 +243,6 class Issue < ActiveRecord::Base | |||
|
242 | 243 | end |
|
243 | 244 | end |
|
244 | 245 | |
|
245 | def before_create | |
|
246 | # default assignment based on category | |
|
247 | if assigned_to.nil? && category && category.assigned_to | |
|
248 | self.assigned_to = category.assigned_to | |
|
249 | end | |
|
250 | end | |
|
251 | ||
|
252 | 246 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios |
|
253 | 247 | # even if the user turns off the setting later |
|
254 | 248 | def update_done_ratio_from_issue_status |
@@ -257,27 +251,6 class Issue < ActiveRecord::Base | |||
|
257 | 251 | end |
|
258 | 252 | end |
|
259 | 253 | |
|
260 | def after_save | |
|
261 | # Reload is needed in order to get the right status | |
|
262 | reload | |
|
263 | ||
|
264 | # Update start/due dates of following issues | |
|
265 | relations_from.each(&:set_issue_to_dates) | |
|
266 | ||
|
267 | # Close duplicates if the issue was closed | |
|
268 | if @issue_before_change && !@issue_before_change.closed? && self.closed? | |
|
269 | duplicates.each do |duplicate| | |
|
270 | # Reload is need in case the duplicate was updated by a previous duplicate | |
|
271 | duplicate.reload | |
|
272 | # Don't re-close it if it's already closed | |
|
273 | next if duplicate.closed? | |
|
274 | # Same user and notes | |
|
275 | duplicate.init_journal(@current_journal.user, @current_journal.notes) | |
|
276 | duplicate.update_attribute :status, self.status | |
|
277 | end | |
|
278 | end | |
|
279 | end | |
|
280 | ||
|
281 | 254 | def init_journal(user, notes = "") |
|
282 | 255 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
283 | 256 | @issue_before_change = self.clone |
@@ -305,6 +278,18 class Issue < ActiveRecord::Base | |||
|
305 | 278 | end |
|
306 | 279 | false |
|
307 | 280 | end |
|
281 | ||
|
282 | # Return true if the issue is being closed | |
|
283 | def closing? | |
|
284 | if !new_record? && status_id_changed? | |
|
285 | status_was = IssueStatus.find_by_id(status_id_was) | |
|
286 | status_new = IssueStatus.find_by_id(status_id) | |
|
287 | if status_was && status_new && !status_was.is_closed? && status_new.is_closed? | |
|
288 | return true | |
|
289 | end | |
|
290 | end | |
|
291 | false | |
|
292 | end | |
|
308 | 293 | |
|
309 | 294 | # Returns true if the issue is overdue |
|
310 | 295 | def overdue? |
@@ -502,6 +487,39 class Issue < ActiveRecord::Base | |||
|
502 | 487 | journal.save |
|
503 | 488 | end |
|
504 | 489 | |
|
490 | # Default assignment based on category | |
|
491 | def default_assign | |
|
492 | if assigned_to.nil? && category && category.assigned_to | |
|
493 | self.assigned_to = category.assigned_to | |
|
494 | end | |
|
495 | end | |
|
496 | ||
|
497 | # Updates start/due dates of following issues | |
|
498 | def reschedule_following_issues | |
|
499 | if start_date_changed? || due_date_changed? | |
|
500 | relations_from.each do |relation| | |
|
501 | relation.set_issue_to_dates | |
|
502 | end | |
|
503 | end | |
|
504 | end | |
|
505 | ||
|
506 | # Closes duplicates if the issue is being closed | |
|
507 | def close_duplicates | |
|
508 | if closing? | |
|
509 | duplicates.each do |duplicate| | |
|
510 | # Reload is need in case the duplicate was updated by a previous duplicate | |
|
511 | duplicate.reload | |
|
512 | # Don't re-close it if it's already closed | |
|
513 | next if duplicate.closed? | |
|
514 | # Same user and notes | |
|
515 | if @current_journal | |
|
516 | duplicate.init_journal(@current_journal.user, @current_journal.notes) | |
|
517 | end | |
|
518 | duplicate.update_attribute :status, self.status | |
|
519 | end | |
|
520 | end | |
|
521 | end | |
|
522 | ||
|
505 | 523 | # Saves the changes in a Journal |
|
506 | 524 | # Called after_save |
|
507 | 525 | def create_journal |
@@ -523,6 +541,8 class Issue < ActiveRecord::Base | |||
|
523 | 541 | :value => c.value) |
|
524 | 542 | } |
|
525 | 543 | @current_journal.save |
|
544 | # reset current journal | |
|
545 | init_journal @current_journal.user, @current_journal.notes | |
|
526 | 546 | end |
|
527 | 547 | end |
|
528 | 548 |
@@ -529,6 +529,32 class IssueTest < ActiveSupport::TestCase | |||
|
529 | 529 | end |
|
530 | 530 | assert ActionMailer::Base.deliveries.empty? |
|
531 | 531 | end |
|
532 | ||
|
533 | def test_saving_twice_should_not_duplicate_journal_details | |
|
534 | i = Issue.find(:first) | |
|
535 | i.init_journal(User.find(2), 'Some notes') | |
|
536 | # 2 changes | |
|
537 | i.subject = 'New subject' | |
|
538 | i.done_ratio = i.done_ratio + 10 | |
|
539 | assert_difference 'Journal.count' do | |
|
540 | assert_difference 'JournalDetail.count', 2 do | |
|
541 | assert i.save | |
|
542 | end | |
|
543 | end | |
|
544 | # 1 more change | |
|
545 | i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id]) | |
|
546 | assert_no_difference 'Journal.count' do | |
|
547 | assert_difference 'JournalDetail.count', 1 do | |
|
548 | i.save | |
|
549 | end | |
|
550 | end | |
|
551 | # no more change | |
|
552 | assert_no_difference 'Journal.count' do | |
|
553 | assert_no_difference 'JournalDetail.count' do | |
|
554 | i.save | |
|
555 | end | |
|
556 | end | |
|
557 | end | |
|
532 | 558 | |
|
533 | 559 | context "#done_ratio" do |
|
534 | 560 | setup do |
General Comments 0
You need to be logged in to leave comments.
Login now