From 1d94371e85e8c8dd595d960d8427a330f52f08fe Mon Sep 17 00:00:00 2001 From: Ko Nagase Date: Wed, 15 May 2024 23:28:37 +0900 Subject: [PATCH 1/5] Add test for supply item --- test/integration/issue_supply_items_test.rb | 48 ++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/test/integration/issue_supply_items_test.rb b/test/integration/issue_supply_items_test.rb index e07af1b..f7987e3 100644 --- a/test/integration/issue_supply_items_test.rb +++ b/test/integration/issue_supply_items_test.rb @@ -111,6 +111,52 @@ 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, :manage_supply_items + log_user 'jsmith', 'jsmith' + + sand = SupplyItem.generate! name: 'Sand', project: @project, unit: 'kg' + + get '/projects/ecookbook/issues/new' + assert_response :success + assert_select 'label', text: 'Supply items' + assert_select '.add_supply_items a', text: 'Add' + + 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 From 18311106cab4f392e64a167041c0ac7344b8fdbc Mon Sep 17 00:00:00 2001 From: Ko Nagase Date: Wed, 15 May 2024 23:29:56 +0900 Subject: [PATCH 2/5] Disallow used supply item deletion --- app/controllers/supply_items_controller.rb | 19 +++++++++++++++---- config/locales/en.yml | 1 + config/locales/ja.yml | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/controllers/supply_items_controller.rb b/app/controllers/supply_items_controller.rb index bfe2d1e..7a807dc 100644 --- a/app/controllers/supply_items_controller.rb +++ b/app/controllers/supply_items_controller.rb @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 9e9440b..d5073c6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -71,3 +71,4 @@ 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." diff --git a/config/locales/ja.yml b/config/locales/ja.yml index e2ca899..ba1144e 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -71,3 +71,4 @@ ja: permission_view_supply_items: 資材の項目を閲覧 permission_view_issue_supply_items: チケットの資材項目を閲覧 permission_manage_issue_supply_items: チケットの資材項目を管理 + error_can_not_delete_supply_item: 'この資材は使用中です。削除できません。' From 97249f7bcae99841d99991c0614a0b790ce57cea Mon Sep 17 00:00:00 2001 From: Ko Nagase Date: Thu, 16 May 2024 00:18:09 +0900 Subject: [PATCH 3/5] Adjust supply items controller and test --- app/controllers/supply_items_controller.rb | 2 +- test/integration/issue_supply_items_test.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/supply_items_controller.rb b/app/controllers/supply_items_controller.rb index 7a807dc..f46f908 100644 --- a/app/controllers/supply_items_controller.rb +++ b/app/controllers/supply_items_controller.rb @@ -117,7 +117,7 @@ def destroy flash[:error] = l(:error_can_not_delete_supply_item) redirect_to project_supply_items_path(@project) end - format.api {head :unprocessable_entity} + format.api { head :unprocessable_entity } end end end diff --git a/test/integration/issue_supply_items_test.rb b/test/integration/issue_supply_items_test.rb index f7987e3..603ff7f 100644 --- a/test/integration/issue_supply_items_test.rb +++ b/test/integration/issue_supply_items_test.rb @@ -113,15 +113,14 @@ def test_issue_supply_items_editing end def test_used_supply_item_cannot_be_deleted - Role.find(1).add_permission! :manage_issue_supply_items, :manage_supply_items + 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 - assert_select 'label', text: 'Supply items' - assert_select '.add_supply_items a', text: 'Add' post '/projects/ecookbook/issues', params: { issue: { From b3d16689893960f4ef58a68d403b3588701fc6c5 Mon Sep 17 00:00:00 2001 From: Ko Nagase Date: Thu, 16 May 2024 00:18:58 +0900 Subject: [PATCH 4/5] Add test for resource item --- test/integration/issue_resource_items_test.rb | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/integration/issue_resource_items_test.rb b/test/integration/issue_resource_items_test.rb index ba9b15a..f08871e 100644 --- a/test/integration/issue_resource_items_test.rb +++ b/test/integration/issue_resource_items_test.rb @@ -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 From dea00c8d42c7c20e3d10c787d3518fa7103375aa Mon Sep 17 00:00:00 2001 From: Ko Nagase Date: Thu, 16 May 2024 00:19:30 +0900 Subject: [PATCH 5/5] Disallow used resource item deletion --- .../asset_resource_items_controller.rb | 4 ++++ .../human_resource_items_controller.rb | 4 ++++ app/controllers/resource_items_controller.rb | 18 ++++++++++++++++-- app/models/resource_item.rb | 1 + config/locales/en.yml | 2 ++ config/locales/ja.yml | 2 ++ 6 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/controllers/asset_resource_items_controller.rb b/app/controllers/asset_resource_items_controller.rb index 1e98ee9..81290fd 100644 --- a/app/controllers/asset_resource_items_controller.rb +++ b/app/controllers/asset_resource_items_controller.rb @@ -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 diff --git a/app/controllers/human_resource_items_controller.rb b/app/controllers/human_resource_items_controller.rb index b5784bc..981d9f5 100644 --- a/app/controllers/human_resource_items_controller.rb +++ b/app/controllers/human_resource_items_controller.rb @@ -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 diff --git a/app/controllers/resource_items_controller.rb b/app/controllers/resource_items_controller.rb index 6f20ca1..075c729 100644 --- a/app/controllers/resource_items_controller.rb +++ b/app/controllers/resource_items_controller.rb @@ -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 diff --git a/app/models/resource_item.rb b/app/models/resource_item.rb index 2dee93b..c6e1cd3 100644 --- a/app/models/resource_item.rb +++ b/app/models/resource_item.rb @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index d5073c6..c644dcf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -72,3 +72,5 @@ en: 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." diff --git a/config/locales/ja.yml b/config/locales/ja.yml index ba1144e..44fccbc 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -72,3 +72,5 @@ ja: 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: "この人員は使用中です。削除できません。"