Skip to content

Commit

Permalink
Change param for is_in_kit, can_deactivate, can_delete to refer to item
Browse files Browse the repository at this point in the history
* Change param for item is_in_kit, can_deactivate_or_delete can_delete to refer to items housing kits
* Add rspec to is_in_kit for testing when given list of kits
* Add rspec for item index view to test when kit is created
  • Loading branch information
jimmyli97 committed Aug 29, 2024
1 parent 1b5defb commit 49ac15c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
14 changes: 7 additions & 7 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def has_inventory?(inventory = nil)
end
end

def is_in_kit?(kits = nil)
if kits
kits.any? { |k| k.item.line_items.map(&:item_id).include?(id) }
def is_in_kit?(items_housing_kits = nil)
if items_housing_kits
items_housing_kits.any? { |k| k.line_items.map(&:item_id).include?(id) }
else
organization.items
.active
Expand All @@ -153,12 +153,12 @@ def is_in_kit?(kits = nil)
end
end

def can_delete?(inventory = nil, kits = nil)
can_deactivate_or_delete?(inventory, kits) && contained_in_line_items.none? && !barcode_count&.positive?
def can_delete?(inventory = nil, items_housing_kits = nil)
can_deactivate_or_delete?(inventory, items_housing_kits) && contained_in_line_items.none? && !barcode_count&.positive?
end

# @return [Boolean]
def can_deactivate_or_delete?(inventory = nil, kits = nil)
def can_deactivate_or_delete?(inventory = nil, items_housing_kits = nil)
if inventory.nil? && Event.read_events?(organization)
inventory = View::Inventory.new(organization_id)
end
Expand All @@ -167,7 +167,7 @@ def can_deactivate_or_delete?(inventory = nil, kits = nil)
# If an active kit includes this item, then changing kit allocations would change inventory
# for an inactive item - which we said above we don't want to allow.

!has_inventory?(inventory) && !is_in_kit?(kits)
!has_inventory?(inventory) && !is_in_kit?(items_housing_kits)
end

def validate_destroy
Expand Down
25 changes: 18 additions & 7 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,26 @@
end

describe '#is_in_kit?' do
it "is true for items that are in a kit and false otherwise" do
item_not_in_kit = create(:item, organization: organization)
item_in_kit = create(:item, organization: organization)
before :each do
@item_not_in_kit = create(:item, organization: organization)
@item_in_kit = create(:item, organization: organization)

kit_params = attributes_for(:kit)
kit_params[:line_items_attributes] = [{item_id: item_in_kit.id, quantity: 1}]
KitCreateService.new(organization_id: organization.id, kit_params: kit_params).call
expect(item_in_kit.is_in_kit?).to be true
expect(item_not_in_kit.is_in_kit?).to be false
kit_params[:line_items_attributes] = [{item_id: @item_in_kit.id, quantity: 1}]
@kit = KitCreateService.new(organization_id: organization.id, kit_params: kit_params).call.kit
end
it "is true for items that are in a kit and false otherwise" do
expect(@item_in_kit.is_in_kit?).to be true
expect(@item_not_in_kit.is_in_kit?).to be false
end
it "checks if true if item is contained in given list of items housing kits" do
kit_params = attributes_for(:kit)
kit_params[:line_items_attributes] = [{item_id: create(:item).id, quantity: 1}]
kit_with_new_item = KitCreateService.new(organization_id: organization.id, kit_params: kit_params).call.kit

expect(@item_in_kit.is_in_kit?([@kit.item])).to be true
expect(@item_not_in_kit.is_in_kit?([@kit.item])).to be false
expect(@item_in_kit.is_in_kit?([kit_with_new_item.item])).to be false
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/requests/items_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
let(:response_format) { 'html' }

it { is_expected.to be_successful }
it "is successful with created kit" do
kit_params = attributes_for(:kit)
kit_params[:line_items_attributes] = [{ item_id: create(:item).id, quantity: 2}]
KitCreateService.new(organization_id: organization.id, kit_params: kit_params).call.kit

expect(subject).to be_successful
end
end

context "csv" do
Expand Down

0 comments on commit 49ac15c

Please sign in to comment.