@@ -20,6 +20,7 class IssueStatus < ActiveRecord::Base | |||||
20 | has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id" |
|
20 | has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id" | |
21 | acts_as_list |
|
21 | acts_as_list | |
22 |
|
22 | |||
|
23 | after_update :handle_is_closed_change | |||
23 | before_destroy :delete_workflow_rules |
|
24 | before_destroy :delete_workflow_rules | |
24 | after_save :update_default |
|
25 | after_save :update_default | |
25 |
|
26 | |||
@@ -94,6 +95,23 class IssueStatus < ActiveRecord::Base | |||||
94 |
|
95 | |||
95 | private |
|
96 | private | |
96 |
|
97 | |||
|
98 | # Updates issues closed_on attribute when an existing status is set as closed. | |||
|
99 | def handle_is_closed_change | |||
|
100 | if is_closed_changed? && is_closed == true | |||
|
101 | # First we update issues that have a journal for when the current status was set, | |||
|
102 | # a subselect is used to update all issues with a single query | |||
|
103 | subselect = "SELECT MAX(j.created_on) FROM #{Journal.table_name} j" + | |||
|
104 | " JOIN #{JournalDetail.table_name} d ON d.journal_id = j.id" + | |||
|
105 | " WHERE j.journalized_type = 'Issue' AND j.journalized_id = #{Issue.table_name}.id" + | |||
|
106 | " AND d.property = 'attr' AND d.prop_key = 'status_id' AND d.value = :status_id" | |||
|
107 | Issue.where(:status_id => id, :closed_on => nil).update_all(["closed_on = (#{subselect})", :status_id => id.to_s]) | |||
|
108 | ||||
|
109 | # Then we update issues that don't have a journal which means the | |||
|
110 | # current status was set on creation | |||
|
111 | Issue.where(:status_id => id, :closed_on => nil).update_all("closed_on = created_on") | |||
|
112 | end | |||
|
113 | end | |||
|
114 | ||||
97 | def check_integrity |
|
115 | def check_integrity | |
98 | raise "Can't delete status" if Issue.where(:status_id => id).any? |
|
116 | raise "Can't delete status" if Issue.where(:status_id => id).any? | |
99 | end |
|
117 | end |
@@ -18,7 +18,15 | |||||
18 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class IssueStatusTest < ActiveSupport::TestCase |
|
20 | class IssueStatusTest < ActiveSupport::TestCase | |
21 | fixtures :issue_statuses, :issues, :roles, :trackers |
|
21 | fixtures :projects, :users, :members, :member_roles, :roles, | |
|
22 | :groups_users, | |||
|
23 | :trackers, :projects_trackers, | |||
|
24 | :enabled_modules, | |||
|
25 | :versions, | |||
|
26 | :issue_statuses, :issue_categories, :issue_relations, :workflows, | |||
|
27 | :enumerations, | |||
|
28 | :issues, :journals, :journal_details, | |||
|
29 | :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values | |||
22 |
|
30 | |||
23 | def test_create |
|
31 | def test_create | |
24 | status = IssueStatus.new :name => "Assigned" |
|
32 | status = IssueStatus.new :name => "Assigned" | |
@@ -120,4 +128,38 class IssueStatusTest < ActiveSupport::TestCase | |||||
120 | assert_not_nil status |
|
128 | assert_not_nil status | |
121 | assert_equal "Resolved", status.name |
|
129 | assert_equal "Resolved", status.name | |
122 | end |
|
130 | end | |
|
131 | ||||
|
132 | def test_setting_status_as_closed_should_set_closed_on_for_issues_without_status_journal | |||
|
133 | issue = Issue.generate!(:status_id => 1, :created_on => 2.days.ago) | |||
|
134 | assert_nil issue.closed_on | |||
|
135 | ||||
|
136 | issue.status.update_attribute :is_closed, true | |||
|
137 | ||||
|
138 | issue.reload | |||
|
139 | assert issue.closed? | |||
|
140 | assert_equal issue.created_on, issue.closed_on | |||
|
141 | end | |||
|
142 | ||||
|
143 | def test_setting_status_as_closed_should_set_closed_on_for_issues_with_status_journal | |||
|
144 | issue = Issue.generate!(:status_id => 1, :created_on => 2.days.ago) | |||
|
145 | issue.init_journal(User.find(1)) | |||
|
146 | issue.status_id = 2 | |||
|
147 | issue.save! | |||
|
148 | ||||
|
149 | issue.status.update_attribute :is_closed, true | |||
|
150 | ||||
|
151 | issue.reload | |||
|
152 | assert issue.closed? | |||
|
153 | assert_equal issue.journals.first.created_on, issue.closed_on | |||
|
154 | end | |||
|
155 | ||||
|
156 | def test_setting_status_as_closed_should_not_set_closed_on_for_issues_with_other_status | |||
|
157 | issue = Issue.generate!(:status_id => 2) | |||
|
158 | ||||
|
159 | IssueStatus.find(1).update_attribute :is_closed, true | |||
|
160 | ||||
|
161 | issue.reload | |||
|
162 | assert !issue.closed? | |||
|
163 | assert_nil issue.closed_on | |||
|
164 | end | |||
123 | end |
|
165 | end |
General Comments 0
You need to be logged in to leave comments.
Login now