]> source.dussan.org Git - redmine.git/commitdiff
Code cleanup.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 25 Oct 2014 09:35:17 +0000 (09:35 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 25 Oct 2014 09:35:17 +0000 (09:35 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@13508 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/changeset.rb
app/models/issue.rb
test/unit/issue_test.rb
test/unit/repository_test.rb

index c54ad0bc3ae2f1be6808f531e1035ace6b8a493e..4eedae14523af5f8b1e698d9d92789e72c457f28 100644 (file)
@@ -227,7 +227,7 @@ class Changeset < ActiveRecord::Base
     # the issue may have been updated by the closure of another one (eg. duplicate)
     issue.reload
     # don't change the status is the issue is closed
-    return if issue.status && issue.status.is_closed?
+    return if issue.closed?
 
     journal = issue.init_journal(user || User.anonymous,
                                  ll(Setting.default_language,
index 910359bf5eb3d5345dadec45ad0741f32705ed57..78854568a4f3ca14e8c80d41f8a397d02094f691 100644 (file)
@@ -588,7 +588,7 @@ class Issue < ActiveRecord::Base
     if fixed_version
       if !assignable_versions.include?(fixed_version)
         errors.add :fixed_version_id, :inclusion
-      elsif reopened? && fixed_version.closed?
+      elsif reopening? && fixed_version.closed?
         errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version)
       end
     end
@@ -691,31 +691,33 @@ class Issue < ActiveRecord::Base
     status.present? && status.is_closed?
   end
 
+  # Returns true if the issue was closed when loaded
+  def was_closed?
+    status_was.present? && status_was.is_closed?
+  end
+
   # Return true if the issue is being reopened
-  def reopened?
-    if !new_record? && status_id_changed?
-      status_was = IssueStatus.find_by_id(status_id_was)
-      status_new = IssueStatus.find_by_id(status_id)
-      if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
-        return true
-      end
+  def reopening?
+    if new_record?
+      false
+    else
+      status_id_changed? && !closed? && was_closed?
     end
-    false
   end
+  alias :reopened? :reopening?
 
   # Return true if the issue is being closed
   def closing?
-    if !new_record? && status_id_changed?
-      if closed? && status_was && !status_was.is_closed?
-        return true
-      end
+    if new_record?
+      closed?
+    else
+      status_id_changed? && closed? && !was_closed?
     end
-    false
   end
 
   # Returns true if the issue is overdue
   def overdue?
-    !due_date.nil? && (due_date < Date.today) && !status.is_closed?
+    due_date.present? && (due_date < Date.today) && !closed?
   end
 
   # Is the amount of work done less than it should for the due date
@@ -1511,7 +1513,7 @@ class Issue < ActiveRecord::Base
   # The closed_on attribute stores the time of the last closing
   # and is preserved when the issue is reopened.
   def update_closed_on
-    if closing? || (new_record? && closed?)
+    if closing?
       self.closed_on = updated_on
     end
   end
index 2cf00f5ff9e6996e7ba944ecec82c8e5a5487f32..8347924580ae0a3802a49305aa8624eee6fb5b20 100644 (file)
@@ -2398,6 +2398,13 @@ class IssueTest < ActiveSupport::TestCase
     assert_equal IssueStatus.find(1), issue.status_was
   end
 
+  def test_status_was_should_return_status_before_change_with_status_id
+    issue = Issue.find(1)
+    assert_equal IssueStatus.find(1), issue.status
+    issue.status_id = 2
+    assert_equal IssueStatus.find(1), issue.status_was
+  end
+
   def test_status_was_should_be_reset_on_save
     issue = Issue.find(1)
     issue.status = IssueStatus.find(2)
@@ -2405,4 +2412,72 @@ class IssueTest < ActiveSupport::TestCase
     assert issue.save!
     assert_equal IssueStatus.find(2), issue.status_was
   end
+
+  def test_closing_should_return_true_when_closing_an_issue
+    issue = Issue.find(1)
+    issue.status = IssueStatus.find(2)
+    assert_equal false, issue.closing?
+    issue.status = IssueStatus.find(5)
+    assert_equal true, issue.closing?
+  end
+
+  def test_closing_should_return_true_when_closing_an_issue_with_status_id
+    issue = Issue.find(1)
+    issue.status_id = 2
+    assert_equal false, issue.closing?
+    issue.status_id = 5
+    assert_equal true, issue.closing?
+  end
+
+  def test_closing_should_return_true_for_new_closed_issue
+    issue = Issue.new
+    assert_equal false, issue.closing?
+    issue.status = IssueStatus.find(5)
+    assert_equal true, issue.closing?
+  end
+
+  def test_closing_should_return_true_for_new_closed_issue_with_status_id
+    issue = Issue.new
+    assert_equal false, issue.closing?
+    issue.status_id = 5
+    assert_equal true, issue.closing?
+  end
+
+  def test_closing_should_be_reset_after_save
+    issue = Issue.find(1)
+    issue.status_id = 5
+    assert_equal true, issue.closing?
+    issue.save!
+    assert_equal false, issue.closing?
+  end
+
+  def test_reopening_should_return_true_when_reopening_an_issue
+    issue = Issue.find(8)
+    issue.status = IssueStatus.find(6)
+    assert_equal false, issue.reopening?
+    issue.status = IssueStatus.find(2)
+    assert_equal true, issue.reopening?
+  end
+
+  def test_reopening_should_return_true_when_reopening_an_issue_with_status_id
+    issue = Issue.find(8)
+    issue.status_id = 6
+    assert_equal false, issue.reopening?
+    issue.status_id = 2
+    assert_equal true, issue.reopening?
+  end
+
+  def test_reopening_should_return_false_for_new_open_issue
+    issue = Issue.new
+    issue.status = IssueStatus.find(1)
+    assert_equal false, issue.reopening?
+  end
+
+  def test_reopening_should_be_reset_after_save
+    issue = Issue.find(8)
+    issue.status_id = 2
+    assert_equal true, issue.reopening?
+    issue.save!
+    assert_equal false, issue.reopening?
+  end
 end
index 9537e50a6573b093a8c6ccda1691b6910e329bb5..54f72bbe8ea86937339eb42f0d7ad1bdeec66301 100644 (file)
@@ -212,7 +212,7 @@ class RepositoryTest < ActiveSupport::TestCase
 
     # make sure issue 1 is not already closed
     fixed_issue = Issue.find(1)
-    assert !fixed_issue.status.is_closed?
+    assert !fixed_issue.closed?
     old_status = fixed_issue.status
 
     with_settings :notified_events => %w(issue_added issue_updated) do
@@ -222,7 +222,7 @@ class RepositoryTest < ActiveSupport::TestCase
 
     # fixed issues
     fixed_issue.reload
-    assert fixed_issue.status.is_closed?
+    assert fixed_issue.closed?
     assert_equal 90, fixed_issue.done_ratio
     assert_equal [101], fixed_issue.changeset_ids