Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixes images preloading on product lists (#13476)

Damian Legawiec1b9cda213fa6
diff --git a/admin/app/controllers/spree/admin/classifications_controller.rb b/admin/app/controllers/spree/admin/classifications_controller.rb
index 472e1fa9e5..ae8c59e74e 100644
--- a/admin/app/controllers/spree/admin/classifications_controller.rb
+++ b/admin/app/controllers/spree/admin/classifications_controller.rb
@@ -38,7 +38,6 @@ module Spree
includes(
:taxon,
product: {
- variant_images: [],
master: [:images, :stock_items, :stock_locations],
variants: [:images, :stock_items, :stock_locations]
}
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rb
index b4bb3dace3..156bc567ee 100644
--- a/admin/app/controllers/spree/admin/products_controller.rb
+++ b/admin/app/controllers/spree/admin/products_controller.rb
@@ -37,7 +37,7 @@ module Spree
scope = current_store.products.not_archived.accessible_by(current_ability, :index)
scope = scope.where.not(id: params[:omit_ids].split(',')) if params[:omit_ids].present?
- @products = scope.includes(master: :primary_image, variants: :primary_image).multi_search(query).limit(params[:limit] || 10)
+ @products = scope.includes(master: :images, variants: :images).multi_search(query).limit(params[:limit] || 10)
respond_to do |format|
format.turbo_stream do
@@ -222,8 +222,8 @@ module Spree
{
stock_items: [],
variants_including_master: [],
- master: [:prices, :primary_image, :stock_items],
- variants: [:prices, :primary_image, :stock_items],
+ master: [:prices, :images, :stock_items],
+ variants: [:prices, :images, :stock_items],
}
end
@@ -301,7 +301,6 @@ module Spree
shipping_category: [],
master: [:prices, :images, :stock_items, :stock_locations],
variants: [:prices, :images, :stock_items, :stock_locations],
- variant_images: [],
}
end
diff --git a/admin/app/controllers/spree/admin/stock_items_controller.rb b/admin/app/controllers/spree/admin/stock_items_controller.rb
index c04bc85a58..151f424114 100644
--- a/admin/app/controllers/spree/admin/stock_items_controller.rb
+++ b/admin/app/controllers/spree/admin/stock_items_controller.rb
@@ -19,7 +19,7 @@ module Spree
def collection_includes
{
stock_location: [],
- variant: [option_values: :option_type, product: [variant_images: [], variants: [:images], master: [:images]], images: []]
+ variant: [option_values: :option_type, product: [variants: [:images], master: [:images]], images: []]
}
end
diff --git a/admin/app/controllers/spree/admin/users_controller.rb b/admin/app/controllers/spree/admin/users_controller.rb
index b8c2c21c8a..ab6f8d493a 100644
--- a/admin/app/controllers/spree/admin/users_controller.rb
+++ b/admin/app/controllers/spree/admin/users_controller.rb
@@ -131,7 +131,7 @@ module Spree
@last_order = @user.completed_orders.last
return unless @last_order.present?
- @last_order_line_items = @last_order.line_items.includes(variant: [:images, { option_values: :option_type }, { product: [:variant_images, { master: :images }, { variants: :images }] }])
+ @last_order_line_items = @last_order.line_items.includes(variant: [:images, { option_values: :option_type }, { product: [{ master: :images }, { variants: :images }] }])
end
end
end
diff --git a/admin/spec/controllers/spree/admin/assets_controller_spec.rb b/admin/spec/controllers/spree/admin/assets_controller_spec.rb
index 123628f50b..70cfb1a951 100644
--- a/admin/spec/controllers/spree/admin/assets_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/assets_controller_spec.rb
@@ -45,6 +45,29 @@ describe Spree::Admin::AssetsController, type: :controller do
expect(asset.session_id).to be_nil
end
end
+
+ context 'with viewable and type Spree::Image' do
+ subject { post :create, params: params, format: :turbo_stream }
+
+ let(:params) do
+ { asset: { type: 'Spree::Image', alt: "some text", attachment: attachment, viewable_id: product.master.id, viewable_type: "Spree::Variant" } }
+ end
+
+ it 'creates a new Spree::Image' do
+ expect { subject }.to change(Spree::Image, :count).by(1)
+
+ expect(response).to have_http_status(:ok)
+ expect(Spree::Image.last.viewable).to eq(product.master)
+ end
+
+ it 'increments variant image_count counter cache' do
+ expect { subject }.to change { product.master.reload.image_count }.by(1)
+ end
+
+ it 'increments product total_image_count counter cache' do
+ expect { subject }.to change { product.reload.total_image_count }.by(1)
+ end
+ end
end
describe '#update' do
diff --git a/api/app/controllers/spree/api/v2/platform/classifications_controller.rb b/api/app/controllers/spree/api/v2/platform/classifications_controller.rb
index b42280eb5a..d2cf87db8a 100644
--- a/api/app/controllers/spree/api/v2/platform/classifications_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/classifications_controller.rb
@@ -12,7 +12,7 @@ module Spree
def scope_includes
[
taxon: [],
- product: [:variants_including_master, :variant_images, :master, { variants: [:prices] }]
+ product: [:variants_including_master, :master, { variants: [:prices] }]
]
end
diff --git a/core/app/models/spree/image.rb b/core/app/models/spree/image.rb
index b4ff738d98..c0a2633ddd 100644
--- a/core/app/models/spree/image.rb
+++ b/core/app/models/spree/image.rb
@@ -65,12 +65,10 @@ module Spree
end
def should_touch_product_variants?
- return false unless viewable.is_a?(Spree::Variant)
- return false unless viewable.is_master?
- return false unless viewable.product.has_variants?
- return false unless saved_change_to_position?
-
- true
+ viewable.is_a?(Spree::Variant) &&
+ viewable.is_master? &&
+ viewable.product.has_variants? &&
+ saved_change_to_position?
end
def increment_viewable_image_count
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index f6e1ee8f65..5cdea7425a 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -39,8 +39,8 @@ module Spree
publishes_lifecycle_events
MEMOIZED_METHODS = %w[total_on_hand taxonomy_ids taxon_and_ancestors category
- default_variant_id tax_category default_variant
- default_image secondary_image category_taxon brand_taxon main_taxon
+ default_variant_id tax_category default_variant variant_for_images
+ category_taxon brand_taxon main_taxon
purchasable? in_stock? backorderable? digital?]
STATUSES = %w[draft active archived].freeze
@@ -250,7 +250,7 @@ module Spree
end
delegate :display_amount, :display_price, :has_default_price?, :track_inventory?,
- :display_compare_at_price, :images, :primary_image, :secondary_image, :image_count, to: :default_variant
+ :display_compare_at_price, :images, to: :default_variant
alias master_images images
@@ -343,7 +343,7 @@ module Spree
end
# Returns true if any variant (including master) has images.
- # Uses total_image_count counter cache for performance.
+ # Uses loaded association when available, otherwise falls back to counter cache.
# @return [Boolean]
def has_variant_images?
return variant_images.any? if association(:variant_images).loaded?
@@ -351,34 +351,43 @@ module Spree
total_image_count.positive?
end
- # Returns true if the product has any images across all variants.
- # This is an alias for has_variant_images? for consistency with Variant#has_images?
+ # Alias for has_variant_images? for consistency with Variant#has_images?
alias has_images? has_variant_images?
- # Returns default Image for Product
- # First it tries to get an image from the master variant
- # Fallbacks to the default variant image
- # Fallbacks to the first image attached to other variants
- # @return [Spree::Image]
+ # Returns the variant that should be used for displaying images.
+ # Priority: master > default_variant > first variant with images
+ # @return [Spree::Variant, nil]
+ def variant_for_images
+ @variant_for_images ||= find_variant_for_images
+ end
+
+ # Returns default Image for Product.
+ # @return [Spree::Image, nil]
def default_image
- @default_image ||= if master.has_images?
- master.primary_image
- elsif has_variants? && default_variant.has_images?
- default_variant.primary_image
- elsif has_variant_images?
- find_first_variant_image
- end
+ variant_for_images&.primary_image
end
- # Finds first image from variants with images using preloaded data when available
+ # Returns secondary Image for Product (for hover effects).
# @return [Spree::Image, nil]
- def find_first_variant_image
- if variants.loaded?
- variant_with_image = variants.find(&:has_images?)
- variant_with_image&.primary_image
- else
- variant_images.first
- end
+ def secondary_image
+ variant_for_images&.secondary_image
+ end
+
+ # Alias for default_image for consistency.
+ alias primary_image default_image
+
+ # Returns the image count from the variant used for displaying images.
+ # @return [Integer]
+ def image_count
+ variant_for_images&.image_count || 0
+ end
+
+ # Finds first variant with images using preloaded data when available.
+ # @return [Spree::Variant, nil]
+ def find_variant_with_images
+ return variants.find(&:has_images?) if variants.loaded?
+
+ variants.joins(:images).first
end
# Returns the short description for the product
@@ -705,6 +714,16 @@ module Spree
private
+ # Determines which variant should be used for displaying images.
+ # Priority: master > default_variant > first variant with images
+ def find_variant_for_images
+ return master if master.has_images?
+ return default_variant if has_variants? && default_variant.has_images?
+ return find_variant_with_images if has_variant_images?
+
+ nil
+ end
+
def add_associations_from_prototype
if prototype_id && prototype = Spree::Prototype.find_by(id: prototype_id)
prototype.properties.each do |property|
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 0402f27453..134b82e586 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -47,8 +47,6 @@ module Spree
has_many :option_values, through: :option_value_variants, dependent: :destroy, class_name: 'Spree::OptionValue'
has_many :images, -> { order(:position) }, as: :viewable, dependent: :destroy, class_name: 'Spree::Image'
- has_one :primary_image, -> { order(:position).limit(1) }, as: :viewable, class_name: 'Spree::Image'
- has_one :secondary_image, -> { order(:position).limit(1).offset(1) }, as: :viewable, class_name: 'Spree::Image'
has_many :prices,
class_name: 'Spree::Price',
@@ -287,27 +285,36 @@ module Spree
end
# Returns true if the variant has images.
- # @return [Boolean] true if the variant has images.
+ # Uses loaded association when available, otherwise falls back to counter cache.
+ # @return [Boolean]
def has_images?
return images.any? if images.loaded?
image_count.positive?
end
- # Returns default Image for Variant
- # @return [Spree::Image]
+ # Returns default Image for Variant, falling back to product's default image.
+ # @return [Spree::Image, nil]
def default_image
- @default_image ||= if has_images?
- images.loaded? ? images.first : primary_image
- else
- product.default_image
- end
+ @default_image ||= has_images? ? images.first : product.default_image
+ end
+
+ # Returns first Image for Variant.
+ # @return [Spree::Image, nil]
+ def primary_image
+ images.first
+ end
+
+ # Returns second Image for Variant (for hover effects).
+ # @return [Spree::Image, nil]
+ def secondary_image
+ images.second
end
- # Returns additional Images for Variant
+ # Returns all images except the default image, combining variant and product images.
# @return [Array<Spree::Image>]
def additional_images
- @additional_images ||= (images + product.images).uniq.find_all { |image| image.id != default_image&.id }
+ @additional_images ||= (images + product.images).uniq.reject { |image| image.id == default_image&.id }
end
# Returns an array of hashes with the option type name, value and presentation
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index a32befe8c9..c254e89614 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -885,29 +885,36 @@ describe Spree::Product, type: :model do
describe '#default_image' do
let(:product) { create(:product, stores: [store]) }
- let!(:image) { create(:image, viewable: product.master) }
- it 'returns the first image for the product' do
- product.reload
- expect(product.default_image).to eq(image)
+ context 'when master has images' do
+ let!(:image) { create(:image, viewable: product.master) }
+
+ it 'returns the master image' do
+ expect(product.reload.default_image).to eq(image)
+ end
+
+ context 'with variants that also have images' do
+ let!(:variant) { create(:variant, product: product) }
+ let!(:variant_image) { create(:image, viewable: variant) }
+
+ it 'returns the master image (master takes priority)' do
+ expect(product.reload.default_image).to eq(image)
+ end
+ end
end
- context 'with variants' do
+ context 'when master has no images but variant does' do
let!(:variant) { create(:variant, product: product) }
- let!(:image2) { create(:image, viewable: variant) }
+ let!(:variant_image) { create(:image, viewable: variant) }
- it 'returns the first image for the product' do
- product.reload
- expect(product.default_image).to eq(image)
+ it 'returns the variant image' do
+ expect(product.reload.default_image).to eq(variant_image)
end
+ end
- context 'when master has no images' do
- let!(:image) { nil }
-
- it 'returns the first image for the variant' do
- product.reload
- expect(product.default_image).to eq(image2)
- end
+ context 'when no variants have images' do
+ it 'returns nil' do
+ expect(product.default_image).to be_nil
end
end
end
@@ -925,8 +932,7 @@ describe Spree::Product, type: :model do
let!(:image) { create(:image, viewable: product.master) }
it 'returns true' do
- product.reload
- expect(product.has_variant_images?).to be true
+ expect(product.reload.has_variant_images?).to be true
end
end
@@ -935,26 +941,27 @@ describe Spree::Product, type: :model do
let!(:image) { create(:image, viewable: variant) }
it 'returns true' do
- product.reload
- expect(product.has_variant_images?).to be true
+ expect(product.reload.has_variant_images?).to be true
end
end
context 'when variant_images are preloaded' do
let!(:image) { create(:image, viewable: product.master) }
- it 'uses loaded association instead of counter cache' do
- product_with_images = Spree::Product.includes(:variant_images).find(product.id)
- expect(product_with_images.association(:variant_images)).to be_loaded
- expect(product_with_images.has_variant_images?).to be true
+ it 'uses loaded association' do
+ loaded_product = Spree::Product.includes(:variant_images).find(product.id)
+
+ expect(loaded_product.association(:variant_images)).to be_loaded
+ expect(loaded_product.has_variant_images?).to be true
end
end
context 'when variant_images are preloaded but empty' do
- it 'returns false using loaded association' do
- product_with_images = Spree::Product.includes(:variant_images).find(product.id)
- expect(product_with_images.association(:variant_images)).to be_loaded
- expect(product_with_images.has_variant_images?).to be false
+ it 'returns false' do
+ loaded_product = Spree::Product.includes(:variant_images).find(product.id)
+
+ expect(loaded_product.association(:variant_images)).to be_loaded
+ expect(loaded_product.has_variant_images?).to be false
end
end
end
@@ -968,14 +975,13 @@ describe Spree::Product, type: :model do
end
end
- context 'when master has images but default_variant (a non-master variant) does not' do
+ context 'when master has images but default_variant does not' do
let!(:variant) { create(:variant, product: product) }
let!(:image) { create(:image, viewable: product.master) }
- it 'returns true because it checks all variants, not just default_variant' do
+ it 'returns true (checks all variants)' do
product.reload
expect(product.default_variant).to eq(variant)
- expect(product.default_variant.has_images?).to be false
expect(product.has_images?).to be true
end
end
@@ -985,8 +991,7 @@ describe Spree::Product, type: :model do
let!(:image) { create(:image, viewable: variant) }
it 'returns true' do
- product.reload
- expect(product.has_images?).to be true
+ expect(product.reload.has_images?).to be true
end
end
end
@@ -994,76 +999,155 @@ describe Spree::Product, type: :model do
describe '#image_count' do
let(:product) { create(:product, stores: [store]) }
- context 'when default_variant is master (no variants)' do
- it 'returns master image count' do
+ context 'when no variants have images' do
+ it 'returns 0' do
expect(product.image_count).to eq(0)
end
+ end
- context 'with images on master' do
- let!(:images) { create_list(:image, 2, viewable: product.master) }
+ context 'when master has images' do
+ let!(:images) { create_list(:image, 2, viewable: product.master) }
+
+ it 'returns the master image count' do
+ expect(product.reload.image_count).to eq(2)
+ end
- it 'returns the image count' do
+ context 'when variant also has images' do
+ let!(:variant) { create(:variant, product: product) }
+ let!(:variant_images) { create_list(:image, 3, viewable: variant) }
+
+ it 'returns master image count (master takes priority)' do
product.reload
+ expect(product.variant_for_images).to eq(product.master)
expect(product.image_count).to eq(2)
end
end
end
- context 'when default_variant is a non-master variant' do
+ context 'when only variant has images' do
let!(:variant) { create(:variant, product: product) }
- let!(:master_images) { create_list(:image, 2, viewable: product.master) }
let!(:variant_images) { create_list(:image, 3, viewable: variant) }
- it 'returns the default_variant image count, not master' do
+ it 'returns the variant image count' do
product.reload
- expect(product.default_variant).to eq(variant)
+ expect(product.variant_for_images).to eq(variant)
expect(product.image_count).to eq(3)
end
end
end
- describe '#find_first_variant_image' do
+ describe '#variant_for_images' do
let(:product) { create(:product, stores: [store]) }
context 'when no variants have images' do
it 'returns nil' do
- expect(product.send(:find_first_variant_image)).to be_nil
+ expect(product.variant_for_images).to be_nil
end
end
- context 'when variants are not preloaded' do
+ context 'when master has images' do
+ let!(:image) { create(:image, viewable: product.master) }
+
+ it 'returns master' do
+ expect(product.reload.variant_for_images).to eq(product.master)
+ end
+ end
+
+ context 'when only default_variant has images' do
let!(:variant) { create(:variant, product: product) }
let!(:image) { create(:image, viewable: variant) }
- it 'returns first variant image via variant_images association' do
- product.reload
- expect(product.send(:find_first_variant_image)).to eq(image)
+ it 'returns default_variant' do
+ expect(product.reload.variant_for_images).to eq(variant)
end
end
- context 'when variants are preloaded with primary_image' do
+ context 'when only a non-default variant has images' do
let!(:variant1) { create(:variant, product: product) }
let!(:variant2) { create(:variant, product: product) }
let!(:image) { create(:image, viewable: variant2) }
- it 'finds the variant with images and returns its primary_image' do
- product_with_variants = Spree::Product.includes(variants: :primary_image).find(product.id)
- product_with_variants.variants.reload # ensure loaded
+ it 'returns the variant with images' do
+ product.reload
+ expect(product.default_variant).to eq(variant1)
+ expect(product.variant_for_images).to eq(variant2)
+ end
+ end
+ end
+
+ describe '#secondary_image' do
+ let(:product) { create(:product, stores: [store]) }
- result = product_with_variants.send(:find_first_variant_image)
- expect(result).to eq(image)
+ context 'when no variants have images' do
+ it 'returns nil' do
+ expect(product.secondary_image).to be_nil
end
end
- context 'when multiple variants have images' do
- let!(:variant1) { create(:variant, product: product) }
- let!(:variant2) { create(:variant, product: product) }
- let!(:image1) { create(:image, viewable: variant1) }
- let!(:image2) { create(:image, viewable: variant2) }
+ context 'when variant has only one image' do
+ let!(:image) { create(:image, viewable: product.master) }
+
+ it 'returns nil' do
+ expect(product.reload.secondary_image).to be_nil
+ end
+ end
+
+ context 'when variant has multiple images' do
+ let!(:image1) { create(:image, viewable: product.master, position: 1) }
+ let!(:image2) { create(:image, viewable: product.master, position: 2) }
+
+ it 'returns the second image' do
+ expect(product.reload.secondary_image).to eq(image2)
+ end
+ end
- it 'returns the first variant image' do
+ context 'when images are on a non-master variant' do
+ let!(:variant) { create(:variant, product: product) }
+ let!(:image1) { create(:image, viewable: variant, position: 1) }
+ let!(:image2) { create(:image, viewable: variant, position: 2) }
+
+ it 'returns the second image from that variant' do
product.reload
- expect(product.send(:find_first_variant_image)).to eq(image1)
+ expect(product.secondary_image).to eq(image2)
+ end
+ end
+ end
+
+ describe 'image methods with eager loading' do
+ let(:product) { create(:product, stores: [store]) }
+ let!(:variant) { create(:variant, product: product) }
+ let!(:image) { create(:image, viewable: variant) }
+
+ let(:storefront_includes) do
+ [
+ :prices_including_master,
+ { master: [:images, :prices],
+ variants: [:images, :prices, :option_values] }
+ ]
+ end
+
+ it 'returns correct images with storefront includes' do
+ loaded_product = Spree::Product.includes(*storefront_includes).find(product.id)
+
+ expect(loaded_product.default_image).to eq(image)
+ expect(loaded_product.variant_for_images).to eq(variant)
+ expect(loaded_product.has_images?).to be true
+ end
+
+ context 'when image is on non-default variant' do
+ let!(:variant2) { create(:variant, product: product) }
+
+ before do
+ variant.stock_items.update_all(count_on_hand: 0, backorderable: false)
+ variant2.stock_items.update_all(count_on_hand: 10, backorderable: true)
+ end
+
+ it 'returns image from non-default variant' do
+ loaded_product = Spree::Product.includes(*storefront_includes).find(product.id)
+
+ expect(loaded_product.default_variant).to eq(variant2)
+ expect(loaded_product.variant_for_images).to eq(variant)
+ expect(loaded_product.default_image).to eq(image)
end
end
end
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index 2796e9933a..31feb47c8a 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -1395,31 +1395,52 @@ describe Spree::Variant, type: :model do
describe '#default_image' do
let(:variant) { create(:variant) }
+
+ context 'when variant has images' do
+ let!(:image) { create(:image, position: 1, viewable: variant) }
+
+ it 'returns the first image' do
+ expect(variant.reload.default_image).to eq(image)
+ end
+ end
+
+ context 'when variant has no images' do
+ let!(:product_image) { create(:image, viewable: variant.product.master) }
+
+ it 'falls back to product default image' do
+ # Reload to get updated counter cache
+ variant.product.reload
+ expect(variant.default_image).to eq(product_image)
+ end
+ end
+ end
+
+ describe '#primary_image' do
+ let(:variant) { create(:variant) }
let!(:image) { create(:image, position: 1, viewable: variant) }
- it 'returns the first image for the variant' do
- variant.reload
- expect(variant.default_image).to eq(image)
+ it 'returns the first image' do
+ expect(variant.primary_image).to eq(image)
end
end
describe '#secondary_image' do
let(:variant) { create(:variant) }
- let!(:image) { create(:image, position: 1, viewable: variant) }
+ let!(:image1) { create(:image, position: 1, viewable: variant) }
let!(:image2) { create(:image, position: 2, viewable: variant) }
- it 'returns the second image for the variant' do
+ it 'returns the second image' do
expect(variant.secondary_image).to eq(image2)
end
end
describe '#additional_images' do
let(:variant) { create(:variant) }
- let!(:image) { create(:image, position: 1, viewable: variant) }
+ let!(:image1) { create(:image, position: 1, viewable: variant) }
let!(:image2) { create(:image, position: 2, viewable: variant) }
let!(:image3) { create(:image, position: 3, viewable: variant) }
- it 'returns the additional images for the variant' do
+ it 'returns all images except the default' do
expect(variant.additional_images).to eq([image2, image3])
end
end
@@ -1436,27 +1457,28 @@ describe Spree::Variant, type: :model do
context 'when variant has images' do
let!(:image) { create(:image, viewable: variant) }
- it 'returns true using counter cache' do
- variant.reload
- expect(variant.has_images?).to be true
+ it 'returns true' do
+ expect(variant.reload.has_images?).to be true
end
end
context 'when images are preloaded' do
let!(:image) { create(:image, viewable: variant) }
- it 'uses loaded images instead of counter cache' do
- variant_with_images = Spree::Variant.includes(:images).find(variant.id)
- expect(variant_with_images.images).to be_loaded
- expect(variant_with_images.has_images?).to be true
+ it 'uses loaded association' do
+ loaded_variant = Spree::Variant.includes(:images).find(variant.id)
+
+ expect(loaded_variant.images).to be_loaded
+ expect(loaded_variant.has_images?).to be true
end
end
context 'when images are preloaded but empty' do
- it 'returns false using loaded images' do
- variant_with_images = Spree::Variant.includes(:images).find(variant.id)
- expect(variant_with_images.images).to be_loaded
- expect(variant_with_images.has_images?).to be false
+ it 'returns false' do
+ loaded_variant = Spree::Variant.includes(:images).find(variant.id)
+
+ expect(loaded_variant.images).to be_loaded
+ expect(loaded_variant.has_images?).to be false
end
end
end
diff --git a/storefront/app/controllers/concerns/spree/cart_methods.rb b/storefront/app/controllers/concerns/spree/cart_methods.rb
index c026cb3140..615b7917f7 100644
--- a/storefront/app/controllers/concerns/spree/cart_methods.rb
+++ b/storefront/app/controllers/concerns/spree/cart_methods.rb
@@ -44,7 +44,6 @@ module Spree
{ option_values: [:option_type] },
{
product: [
- :variant_images,
{ master: :images },
{ variants: :images }
]
diff --git a/storefront/app/controllers/spree/store_controller.rb b/storefront/app/controllers/spree/store_controller.rb
index c7c48b27b7..306e015f73 100644
--- a/storefront/app/controllers/spree/store_controller.rb
+++ b/storefront/app/controllers/spree/store_controller.rb
@@ -185,9 +185,9 @@ module Spree
:prices_including_master,
:option_types,
:option_values,
- { master: [:primary_image, :secondary_image, :prices, :stock_items, :stock_locations, { stock_items: :stock_location }],
+ { master: [:images, :prices, :stock_items, :stock_locations, { stock_items: :stock_location }],
variants: [
- :primary_image, :secondary_image, :prices, :option_values, :stock_items, :stock_locations,
+ :images, :prices, :option_values, :stock_items, :stock_locations,
{ option_values: :option_type, stock_items: :stock_location }
],
taxons: [:taxonomy],