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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def index
Spree::Product.includes(
:variant_images,
master: :prices,
variants: :prices
variants: :prices,
variants_including_master: {stock_items: :stock_location}
),
param: :q,
distinct: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def update

def resource_class = Spree::StockItem

def resources_collection = Spree::StockItem.reorder(nil)
def resources_collection
Spree::StockItem.reorder(nil).includes(
:stock_location,
variant: [:product, :images, {option_values: :option_type}]
)
end

def resources_sorting_options
{
Expand Down
9 changes: 9 additions & 0 deletions admin/spec/requests/solidus_admin/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
end

describe "GET #index" do
it "loads variant stock in a single query" do
create(:stock_location)
create_list(:product, 3)

expect { get solidus_admin.products_path }.to make_database_queries(matching: /from .spree_stock_items./i, count: 1)
end
end

describe "PATCH #update" do
let(:product) { create(:product) }
let(:params) do
Expand Down
22 changes: 22 additions & 0 deletions admin/spec/requests/solidus_admin/stock_items_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "SolidusAdmin::StockItemsController", type: :request do
let(:admin_user) { create(:admin_user) }

before do
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
end

describe "GET #index" do
it "loads stock item stock locations in a single query" do
3.times do
location = create(:stock_location, propagate_all_variants: false)
create(:stock_item, stock_location: location, variant: create(:variant))
end

expect { get solidus_admin.stock_items_path }.to make_database_queries(matching: /from .spree_stock_locations./i, count: 2)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def stock_location

def scope
@stock_location.stock_movements.accessible_by(current_ability)
.includes(stock_item: {variant: [:product, :prices, :stock_items, :images, {option_values: :option_type}]})
end

def stock_movement_params
Expand Down
1 change: 1 addition & 0 deletions api/app/controllers/spree/api/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Spree::Api::UsersController < Spree::Api::BaseController

def index
user_scope = user_class.accessible_by(current_ability, :show)
.includes(bill_address: [:state, :country], ship_address: [:state, :country])
if params[:ids]
ids = params[:ids].split(",").flatten
@users = user_scope.where(id: ids)
Expand Down
12 changes: 12 additions & 0 deletions api/spec/requests/spree/api/stock_movements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ module Spree::Api
expect(response.status).to eq(201)
expect(json_response).to have_attributes(attributes)
end

it "loads stock movement variant prices in a single query" do
3.times do
variant = create(:variant)
item = Spree::StockItem.where(stock_location:, variant:).first_or_create!
create(:stock_movement, stock_item: item)
end

expect {
get spree.api_stock_location_stock_movements_path(stock_location)
}.to make_database_queries(matching: /from .spree_prices./i, count: 1)
end
end
end
end
8 changes: 8 additions & 0 deletions api/spec/requests/spree/api/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ module Spree::Api
expect(json_response["users"].size).to eq 2
end

it "loads user addresses without an N+1" do
allow(Spree::LegacyUser).to receive(:find_by).with(hash_including(:spree_api_key)) { current_api_user }

3.times { create(:user_with_addresses) }

expect { get spree.api_users_path }.to make_database_queries(matching: /from .spree_addresses./i, count: 1)
end

it "can control the page size through a parameter" do
2.times { create(:user) }
get spree.api_users_path, params: {per_page: 1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PaymentsController < Spree::Admin::BaseController
respond_to :html

def index
@payments = @order.payments.includes(refunds: :reason)
@payments = @order.payments.includes(:payment_method, refunds: :reason)
@refunds = @payments.flat_map(&:refunds)
redirect_to new_admin_order_payment_url(@order) if @payments.empty?
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ module Admin
let(:user) { create(:admin_user) }
let(:order) { create(:order) }

describe "#index" do
let(:order) { create(:order, bill_address: create(:address)) }

it "loads the listed payments' methods without an N+1" do
3.times { create(:payment, order:, payment_method: create(:check_payment_method)) }

expect {
get :index, params: {order_id: order.number}
}.to make_database_queries(matching: /from .spree_payment_methods./i, count: 2)
end
end

describe "#create" do
context "with a valid credit card" do
let(:order) { create(:order_with_line_items, state: "payment") }
Expand Down
Loading