@@ -58,7 +58,8 class Issue < ActiveRecord::Base | |||||
58 |
|
58 | |||
59 | named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status |
|
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 | after_save :create_journal |
|
63 | after_save :create_journal | |
63 |
|
64 | |||
64 | # Returns true if usr or current user is allowed to view the issue |
|
65 | # Returns true if usr or current user is allowed to view the issue | |
@@ -242,13 +243,6 class Issue < ActiveRecord::Base | |||||
242 | end |
|
243 | end | |
243 | end |
|
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 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios |
|
246 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios | |
253 | # even if the user turns off the setting later |
|
247 | # even if the user turns off the setting later | |
254 | def update_done_ratio_from_issue_status |
|
248 | def update_done_ratio_from_issue_status | |
@@ -257,27 +251,6 class Issue < ActiveRecord::Base | |||||
257 | end |
|
251 | end | |
258 | end |
|
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 | def init_journal(user, notes = "") |
|
254 | def init_journal(user, notes = "") | |
282 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
255 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) | |
283 | @issue_before_change = self.clone |
|
256 | @issue_before_change = self.clone | |
@@ -306,6 +279,18 class Issue < ActiveRecord::Base | |||||
306 | false |
|
279 | false | |
307 | end |
|
280 | end | |
308 |
|
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 | |||
|
293 | ||||
309 | # Returns true if the issue is overdue |
|
294 | # Returns true if the issue is overdue | |
310 | def overdue? |
|
295 | def overdue? | |
311 | !due_date.nil? && (due_date < Date.today) && !status.is_closed? |
|
296 | !due_date.nil? && (due_date < Date.today) && !status.is_closed? | |
@@ -502,6 +487,39 class Issue < ActiveRecord::Base | |||||
502 | journal.save |
|
487 | journal.save | |
503 | end |
|
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 | # Saves the changes in a Journal |
|
523 | # Saves the changes in a Journal | |
506 | # Called after_save |
|
524 | # Called after_save | |
507 | def create_journal |
|
525 | def create_journal | |
@@ -523,6 +541,8 class Issue < ActiveRecord::Base | |||||
523 | :value => c.value) |
|
541 | :value => c.value) | |
524 | } |
|
542 | } | |
525 | @current_journal.save |
|
543 | @current_journal.save | |
|
544 | # reset current journal | |||
|
545 | init_journal @current_journal.user, @current_journal.notes | |||
526 | end |
|
546 | end | |
527 | end |
|
547 | end | |
528 |
|
548 |
@@ -530,6 +530,32 class IssueTest < ActiveSupport::TestCase | |||||
530 | assert ActionMailer::Base.deliveries.empty? |
|
530 | assert ActionMailer::Base.deliveries.empty? | |
531 | end |
|
531 | end | |
532 |
|
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 | |||
|
558 | ||||
533 | context "#done_ratio" do |
|
559 | context "#done_ratio" do | |
534 | setup do |
|
560 | setup do | |
535 | @issue = Issue.find(1) |
|
561 | @issue = Issue.find(1) |
General Comments 0
You need to be logged in to leave comments.
Login now