Skip to content

Commit aba1654

Browse files
authored
Merge pull request #6495 from ikraamg/perf/admin-products-stock-n-plus-one
Avoid an N+1 loading stock on the admin products index
2 parents d4463ad + 937943f commit aba1654

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

admin/app/controllers/solidus_admin/products_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def index
1616
Spree::Product.includes(
1717
:variant_images,
1818
master: :prices,
19-
variants: :prices
19+
variants: :prices,
20+
variants_including_master: {stock_items: :stock_location}
2021
),
2122
param: :q,
2223
distinct: false

admin/spec/requests/solidus_admin/products_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
1010
end
1111

12+
describe "GET #index" do
13+
before { create_list(:product, 3) }
14+
15+
it "renders successfully" do
16+
get solidus_admin.products_path
17+
expect(response).to have_http_status(:ok)
18+
end
19+
20+
it "loads stock for every product without an N+1" do
21+
expect { get solidus_admin.products_path }
22+
.to make_database_queries(matching: /from .spree_stock_items./i, count: 1)
23+
end
24+
end
25+
1226
describe "PATCH #update" do
1327
let(:product) { create(:product) }
1428
let(:params) do

0 commit comments

Comments
 (0)