From 7042dd2fcb521c57d62b5305a8cddeec03e5e860 Mon Sep 17 00:00:00 2001 From: jeet12323 <21it043@charusat.edu.in> Date: Sun, 31 Aug 2025 23:04:08 +0530 Subject: [PATCH 1/4] In inventory when an item is deactivated it displays list of partners which contains items in child requests --- app/controllers/items_controller.rb | 13 +++++++++++++ app/models/item.rb | 1 + 2 files changed, 14 insertions(+) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index c87e608e92..0b8a72f579 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -91,6 +91,14 @@ def update def deactivate item = current_organization.items.find(params[:id]) + + partners = Partner + .joins(families: { children: :requested_items }) + .where(organization_id: current_organization.id) + .where(children: { active: true }) + .where(items: { id: item.id }) + .distinct + begin item.deactivate! rescue => e @@ -99,6 +107,11 @@ def deactivate return end + if partners.any? + flash[:alert] = "The following partners have active children with this item: " + + partners.map(&:name).map(&:downcase).sort.join(", ") + end + flash[:notice] = "#{item.name} has been deactivated." redirect_to items_path end diff --git a/app/models/item.rb b/app/models/item.rb index 15e7b1382c..c74307b2b2 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -52,6 +52,7 @@ class Item < ApplicationRecord has_many :donations, through: :line_items, source: :itemizable, source_type: "::Donation" has_many :distributions, through: :line_items, source: :itemizable, source_type: "::Distribution" has_many :request_units, class_name: "ItemUnit", dependent: :destroy + has_and_belongs_to_many :children, class_name: 'Partners::Child' scope :active, -> { where(active: true) } From b432c6677efd97ec8ffc0d72eaaf36cd52027616 Mon Sep 17 00:00:00 2001 From: jeet12323 <21it043@charusat.edu.in> Date: Sun, 31 Aug 2025 23:35:49 +0530 Subject: [PATCH 2/4] maintaining standards changed single quotes to double --- app/models/item.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/item.rb b/app/models/item.rb index c74307b2b2..405ee4d7b6 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -52,7 +52,7 @@ class Item < ApplicationRecord has_many :donations, through: :line_items, source: :itemizable, source_type: "::Donation" has_many :distributions, through: :line_items, source: :itemizable, source_type: "::Distribution" has_many :request_units, class_name: "ItemUnit", dependent: :destroy - has_and_belongs_to_many :children, class_name: 'Partners::Child' + has_and_belongs_to_many :children, class_name: "Partners::Child" scope :active, -> { where(active: true) } From 77404983386b6531cf65f7da8f608797b57f25ab Mon Sep 17 00:00:00 2001 From: Jeet Date: Sun, 31 Aug 2025 18:11:17 +0000 Subject: [PATCH 3/4] solved rubocop errors --- app/controllers/items_controller.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 0b8a72f579..c63efbf76e 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -93,12 +93,12 @@ def deactivate item = current_organization.items.find(params[:id]) partners = Partner - .joins(families: { children: :requested_items }) - .where(organization_id: current_organization.id) - .where(children: { active: true }) - .where(items: { id: item.id }) - .distinct - + .joins(families: {children: :requested_items}) + .where(organization_id: current_organization.id) + .where(children: {active: true}) + .where(items: {id: item.id}) + .distinct + begin item.deactivate! rescue => e @@ -109,8 +109,8 @@ def deactivate if partners.any? flash[:alert] = "The following partners have active children with this item: " + - partners.map(&:name).map(&:downcase).sort.join(", ") - end + partners.map(&:name).map(&:downcase).sort.join(", ") + end flash[:notice] = "#{item.name} has been deactivated." redirect_to items_path From 63c4570f3956978368752ba9067868068c77794e Mon Sep 17 00:00:00 2001 From: jeet12323 <21it043@charusat.edu.in> Date: Tue, 30 Sep 2025 00:13:03 +0530 Subject: [PATCH 4/4] Rspec written for this --- spec/controllers/items_controller_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/controllers/items_controller_spec.rb b/spec/controllers/items_controller_spec.rb index 96fad6b4bc..a445af9900 100644 --- a/spec/controllers/items_controller_spec.rb +++ b/spec/controllers/items_controller_spec.rb @@ -219,6 +219,24 @@ expect(response).to have_notice end end + + describe "PATCH #deactivate" do + let(:item) { create(:item, organization: organization) } + let!(:partner) { create(:partner, organization: organization, name: "Helping Hands") } + let!(:family) { create(:partners_family, partner: partner) } + let!(:child) { create(:partners_child, family: family, active: true) } + + before do + child.requested_items << item + end + + it "sets a flash alert listing partners with active children for this item" do + patch :deactivate, params: { id: item.id } + + expect(response).to redirect_to(items_path) + expect(flash[:alert]).to include("helping hands") # downcased in controller + end + end end context "While not signed in" do