Skip to content

Commit

Permalink
Merge pull request #21 from gtt-project/fix/disallow-used-item-deletion
Browse files Browse the repository at this point in the history
Disallow used supply/resource item deletion
  • Loading branch information
sanak authored May 16, 2024
2 parents afe1104 + dea00c8 commit 2417eee
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/controllers/asset_resource_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ def resource_class
def redirect_to_index
redirect_to project_asset_resource_items_path @project
end

def error_can_not_delete_resource_item
l(:error_can_not_delete_asset_resource_item)
end
end

4 changes: 4 additions & 0 deletions app/controllers/human_resource_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ def resource_class
def redirect_to_index
redirect_to project_human_resource_items_path @project
end

def error_can_not_delete_resource_item
l(:error_can_not_delete_human_resource_item)
end
end
18 changes: 16 additions & 2 deletions app/controllers/resource_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,22 @@ def update
end

def destroy
find_resource_item.destroy
redirect_to_index
@resource_item = find_resource_item
if @resource_item.issues.empty?
@resource_item.destroy
respond_to do |format|
format.html { redirect_to_index }
format.api { render_api_ok }
end
else
respond_to do |format|
format.html do
flash[:error] = error_can_not_delete_resource_item
redirect_to_index
end
format.api { head :unprocessable_entity }
end
end
end

def autocomplete
Expand Down
19 changes: 15 additions & 4 deletions app/controllers/supply_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,21 @@ def update
end

def destroy
find_supply_item.destroy
respond_to do |format|
format.html { redirect_to project_supply_items_path(@project) }
format.api { render_api_ok }
@supply_item = find_supply_item
if @supply_item.issues.empty?
@supply_item.destroy
respond_to do |format|
format.html { redirect_to project_supply_items_path(@project) }
format.api { render_api_ok }
end
else
respond_to do |format|
format.html do
flash[:error] = l(:error_can_not_delete_supply_item)
redirect_to project_supply_items_path(@project)
end
format.api { head :unprocessable_entity }
end
end
end

Expand Down
1 change: 1 addition & 0 deletions app/models/resource_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ResourceItem < (defined?(ApplicationRecord) == 'constant' ? ApplicationRec
belongs_to :category, class_name: 'ResourceCategory'

has_many :issue_resource_items
has_many :issues, through: :issue_resource_items

validates :project_id, presence: true
#validates :category_id, presence: true
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ en:
permission_view_supply_items: View supply items
permission_view_issue_supply_items: View issue supply items
permission_manage_issue_supply_items: Manage issue supply items
error_can_not_delete_supply_item: "This supply item contains issues and cannot be deleted."
error_can_not_delete_asset_resource_item: "This asset resource item contains issues and cannot be deleted."
error_can_not_delete_human_resource_item: "This human resource item contains issues and cannot be deleted."
3 changes: 3 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ ja:
permission_view_supply_items: 資材の項目を閲覧
permission_view_issue_supply_items: チケットの資材項目を閲覧
permission_manage_issue_supply_items: チケットの資材項目を管理
error_can_not_delete_supply_item: 'この資材は使用中です。削除できません。'
error_can_not_delete_asset_resource_item: "この資産は使用中です。削除できません。"
error_can_not_delete_human_resource_item: "この人員は使用中です。削除できません。"
45 changes: 45 additions & 0 deletions test/integration/issue_resource_items_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,51 @@ def test_issue_resource_items_editing
assert_select 'div.issue_resource_item_wrap', text: /ASL-JJ 262/, count: 0

end

def test_used_resource_items_cannot_be_deleted
Role.find(1).add_permission! :manage_issue_resources
Role.find(1).add_permission! :view_issue_resources
Role.find(1).add_permission! :manage_resource_items
log_user 'jsmith', 'jsmith'

get '/projects/ecookbook/issues/new'
assert_response :success

post '/projects/ecookbook/issues', params: {
issue: {
subject: 'test',
issue_resource_items_attributes: [
{"resource_item_id" => @item.id.to_s}
]
}
}

@item.reload
assert_equal 1, @item.issue_resource_items.count
assert issue_resource_item = @item.issue_resource_items.first
assert issue = issue_resource_item.issue
assert_equal 'test', issue.subject

follow_redirect!
assert_response :success
assert_select 'div.label', text: 'Assets:'
assert_select 'div.value', text: /RCM 429/

assert_no_difference 'ResourceItem.assets.count' do
delete "/projects/ecookbook/asset_resource_items/#{@item.id}"
end
assert_redirected_to '/projects/ecookbook/asset_resource_items'
assert_not_nil flash[:error]

assert_difference 'Issue.count', -1 do
delete "/issues/#{issue.id}"
end
assert_difference 'ResourceItem.assets.count', -1 do
delete "/projects/ecookbook/asset_resource_items/#{@item.id}"
end
assert_redirected_to '/projects/ecookbook/asset_resource_items'
assert_nil flash[:error]
end
end


47 changes: 46 additions & 1 deletion test/integration/issue_supply_items_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,51 @@ def test_issue_supply_items_editing
assert_select 'div.value', text: /Sand/, count: 0

end
end

def test_used_supply_item_cannot_be_deleted
Role.find(1).add_permission! :manage_issue_supply_items
Role.find(1).add_permission! :manage_supply_items
log_user 'jsmith', 'jsmith'

sand = SupplyItem.generate! name: 'Sand', project: @project, unit: 'kg'

get '/projects/ecookbook/issues/new'
assert_response :success

post '/projects/ecookbook/issues', params: {
issue: {
subject: 'test',
issue_supply_items_attributes: [
{"quantity"=>"10.5", "supply_item_id"=>sand.id.to_s}
]
}
}

sand.reload
assert_equal 1, sand.issue_supply_items.count
assert issue_supply_item = sand.issue_supply_items.first
assert_equal 10.5, issue_supply_item.quantity
assert issue = issue_supply_item.issue
assert_equal 'test', issue.subject

follow_redirect!
assert_response :success
assert_select 'label', text: 'Supply items'
assert_select 'div.value', text: 'Sand (10.5 kg)'

assert_no_difference 'SupplyItem.count' do
delete "/projects/ecookbook/supply_items/#{sand.id}"
end
assert_redirected_to '/projects/ecookbook/supply_items'
assert_not_nil flash[:error]

assert_difference 'Issue.count', -1 do
delete "/issues/#{issue.id}"
end
assert_difference 'SupplyItem.count', -1 do
delete "/projects/ecookbook/supply_items/#{sand.id}"
end
assert_redirected_to '/projects/ecookbook/supply_items'
assert_nil flash[:error]
end
end

0 comments on commit 2417eee

Please sign in to comment.