Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
18 changes: 18 additions & 0 deletions spec/controllers/items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down