Skip to content

Commit 4fe4b66

Browse files
committed
Merge branch 'master' of github.com:rubyforgood/diaper
2 parents 97ea180 + c7c0654 commit 4fe4b66

File tree

4 files changed

+152
-3
lines changed

4 files changed

+152
-3
lines changed

Gemfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ gem "webpacker", "~> 3.5"
4949

5050
group :development, :test do
5151
gem "awesome_print"
52+
gem "fakeredis", require: "fakeredis/rspec"
5253
gem "guard-rspec"
5354
gem "pry-rails"
5455
gem "pry-nav"
56+
gem 'rb-readline', '~> 0.5.3'
5557
gem "rspec-rails", "~> 3.8"
58+
gem "rubocop"
5659
gem "terminal-notifier-guard"
5760
gem "terminal-notifier"
5861
gem "timecop"
59-
gem "rubocop"
60-
gem "fakeredis", require: "fakeredis/rspec"
6162
end
6263

6364
group :development do

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ GEM
326326
rb-fsevent (0.10.3)
327327
rb-inotify (0.10.0)
328328
ffi (~> 1.0)
329+
rb-readline (0.5.5)
329330
redis (4.1.0)
330331
ref (2.0.0)
331332
regexp_parser (1.3.0)
@@ -516,6 +517,7 @@ DEPENDENCIES
516517
rails (~> 5.2.2)
517518
rails-controller-testing
518519
rails-erd
520+
rb-readline (~> 0.5.3)
519521
rspec-rails (~> 3.8)
520522
rubocop
521523
sass-rails

app/controllers/items_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ def update
4747
end
4848

4949
def destroy
50-
current_organization.items.find(params[:id]).destroy
50+
item = current_organization.items.find(params[:id])
51+
ActiveRecord::Base.transaction do
52+
item.destroy
53+
end
54+
55+
flash[:notice] = "#{item.name} has been removed."
5156
redirect_to items_path
5257
end
5358

spec/system/item_system_spec.rb

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
RSpec.describe "Item management", type: :system do
2+
before do
3+
sign_in(@user)
4+
end
5+
let!(:url_prefix) { "/#{@organization.to_param}" }
6+
it "can create a new item as a user" do
7+
visit url_prefix + "/items/new"
8+
item_traits = attributes_for(:item)
9+
fill_in "Name", with: item_traits[:name]
10+
select BaseItem.last.name, from: "Base Item"
11+
click_button "Save"
12+
13+
expect(page.find(".alert")).to have_content "added"
14+
end
15+
16+
it "can create a new item with empty attributes as a user" do
17+
visit url_prefix + "/items/new"
18+
click_button "Save"
19+
20+
expect(page.find(".alert")).to have_content "didn't work"
21+
end
22+
23+
it "can update an existing item as a user" do
24+
item = create(:item)
25+
visit url_prefix + "/items/#{item.id}/edit"
26+
click_button "Save"
27+
28+
expect(page.find(".alert")).to have_content "updated"
29+
end
30+
31+
it "can update an existing item with empty attributes as a user" do
32+
item = create(:item)
33+
visit url_prefix + "/items/#{item.id}/edit"
34+
fill_in "Name", with: ""
35+
click_button "Save"
36+
37+
expect(page.find(".alert")).to have_content "didn't work"
38+
end
39+
40+
it "can filter the #index by base item as a user" do
41+
Item.delete_all
42+
create(:item, base_item: BaseItem.first)
43+
create(:item, base_item: BaseItem.last)
44+
visit url_prefix + "/items"
45+
select BaseItem.first.name, from: "filters_by_base_item"
46+
click_button "Filter"
47+
within "#tbl_items" do
48+
expect(page).to have_css("tbody tr", count: 1)
49+
end
50+
end
51+
52+
describe "destroying items" do
53+
subject { create(:item, name: "DELETEME", organization: @user.organization) }
54+
context "when an item has history" do
55+
before do
56+
create(:donation, :with_items, item: subject)
57+
end
58+
it "can be soft-deleted (deactivated) by the user" do
59+
expect do
60+
visit url_prefix + "/items"
61+
expect(page).to have_content(subject.name)
62+
within "tr[data-item-id='#{subject.id}']" do
63+
accept_confirm do
64+
click_on "Delete", match: :first
65+
end
66+
end
67+
page.find(".alert-info")
68+
end.to change { Item.unscoped.count }.by(0).and change { Item.count }.by(-1)
69+
subject.reload
70+
expect(subject).not_to be_active
71+
end
72+
end
73+
74+
context "when an item does not have history" do
75+
it "can be fully deleted by the user" do
76+
subject
77+
expect do
78+
visit url_prefix + "/items"
79+
expect(page).to have_content(subject.name)
80+
within "tr[data-item-id='#{subject.id}']" do
81+
accept_confirm do
82+
click_on "Delete", match: :first
83+
end
84+
end
85+
page.find(".alert-info")
86+
end.to change { Item.unscoped.count }.by(-1).and change { Item.count }.by(-1)
87+
expect { subject.reload }.to raise_error(ActiveRecord::RecordNotFound)
88+
end
89+
end
90+
end
91+
92+
describe "Item Table Tabs >" do
93+
let(:item_pullups) { create(:item, name: "the most wonderful magical pullups that truly potty train", category: "Magic Toddlers") }
94+
let(:item_tampons) { create(:item, name: "blackbeard's rugged tampons", category: "Menstrual Products") }
95+
let(:storage_name) { "the poop catcher warehouse" }
96+
let(:storage) { create(:storage_location, :with_items, item: item_pullups, item_quantity: num_pullups_in_donation, name: storage_name) }
97+
let!(:aux_storage) { create(:storage_location, :with_items, item: item_pullups, item_quantity: num_pullups_second_donation, name: "a secret secondary location") }
98+
let(:num_pullups_in_donation) { 666 }
99+
let(:num_pullups_second_donation) { 1 }
100+
let(:num_tampons_in_donation) { 42 }
101+
let(:num_tampons_second_donation) { 17 }
102+
let(:donation_tampons) { create(:donation, :with_items, storage_location: storage, item_quantity: num_tampons_in_donation, item: item_tampons) }
103+
let(:donation_aux_tampons) { create(:donation, :with_items, storage_location: aux_storage, item_quantity: num_tampons_second_donation, item: item_tampons) }
104+
before do
105+
storage.increase_inventory(donation_tampons)
106+
aux_storage.increase_inventory(donation_aux_tampons)
107+
visit url_prefix + "/items"
108+
end
109+
# Consolidated these into one to reduce the setup/teardown
110+
it "should display items in separate tabs", js: true do
111+
tab_items_only_text = page.find("table#tbl_items", visible: true).text
112+
expect(tab_items_only_text).not_to have_content "Quantity"
113+
expect(tab_items_only_text).to have_content item_pullups.name
114+
expect(tab_items_only_text).to have_content item_tampons.name
115+
116+
click_link "Items and Quantity" # href="#sectionB"
117+
tab_items_and_quantity_text = page.find("table#tbl_items_quantity", visible: true).text
118+
expect(tab_items_and_quantity_text).to have_content "Quantity"
119+
expect(tab_items_and_quantity_text).not_to have_content storage_name
120+
expect(tab_items_and_quantity_text).to have_content num_pullups_in_donation
121+
expect(tab_items_and_quantity_text).to have_content num_pullups_second_donation
122+
expect(tab_items_and_quantity_text).to have_content num_tampons_in_donation
123+
expect(tab_items_and_quantity_text).to have_content num_tampons_second_donation
124+
expect(tab_items_and_quantity_text).to have_content item_pullups.name
125+
expect(tab_items_and_quantity_text).to have_content item_tampons.name
126+
127+
click_link "Items, Quantity, and Location" # href="#sectionC"
128+
tab_items_quantity_location_text = page.find("table#tbl_items_location", visible: true).text
129+
expect(tab_items_quantity_location_text).to have_content "Quantity"
130+
expect(tab_items_quantity_location_text).to have_content storage_name
131+
expect(tab_items_quantity_location_text).to have_content num_pullups_in_donation
132+
expect(tab_items_quantity_location_text).to have_content num_pullups_second_donation
133+
expect(tab_items_quantity_location_text).to have_content num_pullups_in_donation + num_pullups_second_donation
134+
expect(tab_items_quantity_location_text).to have_content num_tampons_in_donation
135+
expect(tab_items_quantity_location_text).to have_content num_tampons_second_donation
136+
expect(tab_items_quantity_location_text).to have_content num_tampons_in_donation + num_tampons_second_donation
137+
expect(tab_items_quantity_location_text).to have_content item_pullups.name
138+
expect(tab_items_quantity_location_text).to have_content item_tampons.name
139+
end
140+
end
141+
end

0 commit comments

Comments
 (0)