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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added `Product#thumbnail` and `Variant#thumbnail`
Damian Legawiec08d59435d874
diff --git a/admin/app/controllers/spree/admin/classifications_controller.rb b/admin/app/controllers/spree/admin/classifications_controller.rbindex 3840d93837..c88546a997 100644--- a/admin/app/controllers/spree/admin/classifications_controller.rb+++ b/admin/app/controllers/spree/admin/classifications_controller.rb@@ -38,8 +38,9 @@ module Spreeincludes(:taxon,product: {- master: [:images, :stock_items, :stock_locations],- variants: [:images, :stock_items, :stock_locations]+ thumbnail: [attachment_attachment: :blob],+ master: [:stock_items, :stock_locations],+ variants: [:stock_items, :stock_locations]}).accessible_by(current_ability)diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rbindex f954c430dd..c262ed4614 100644--- a/admin/app/controllers/spree/admin/products_controller.rb+++ b/admin/app/controllers/spree/admin/products_controller.rb@@ -37,7 +37,7 @@ module Spreescope = 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: :images, variants: :images).multi_search(query).limit(params[:limit] || 10)+ @products = scope.includes(:thumbnail).multi_search(query).limit(params[:limit] || 10)respond_to do |format|format.turbo_stream do@@ -211,9 +211,10 @@ module Spree# These includes are not picked automatically by ar_lazy_preload gem so we need to specify them manually.def collection_includes{+ thumbnail: [attachment_attachment: :blob],stock_items: [],- master: [:images, :prices, :stock_items],- variants: [:images, :prices, :stock_items]+ master: [:prices, :stock_items],+ variants: [:prices, :stock_items]}end@@ -256,7 +257,9 @@ module Spreereturn if @product.has_variants?available_stock_locations_list(master_stock_items_locations_opts).each do |_name, id|- @product.master.stock_items.build(stock_location_id: id, count_on_hand: 0) unless @product.master.stock_items.find { |stock_item| stock_item.stock_location_id == id }+ @product.master.stock_items.build(stock_location_id: id, count_on_hand: 0) unless @product.master.stock_items.find do |stock_item|+ stock_item.stock_location_id == id+ endendend@@ -268,7 +271,9 @@ module Spreereturn unless Spree::Config[:product_properties_enabled]Spree::Property.all.each do |property|- @product.product_properties.build(property: property) unless @product.product_properties.find { |product_property| product_property.property_id == property.id }+ @product.product_properties.build(property: property) unless @product.product_properties.find do |product_property|+ product_property.property_id == property.id+ endendend@@ -289,13 +294,11 @@ module Spreeenddef permitted_resource_params- @permitted_resource_params ||= begin- if cannot?(:activate, @product) && @new_status&.to_sym == :active- params.require(:product).permit(permitted_product_attributes).except(:status, :make_active_at)- else- params.require(:product).permit(permitted_product_attributes)- end- end+ @permitted_resource_params ||= if cannot?(:activate, @product) && @new_status&.to_sym == :active+ params.require(:product).permit(permitted_product_attributes).except(:status, :make_active_at)+ else+ params.require(:product).permit(permitted_product_attributes)+ endendendenddiff --git a/core/app/models/spree/image.rb b/core/app/models/spree/image.rbindex c0a2633ddd..7becca3dca 100644--- a/core/app/models/spree/image.rb+++ b/core/app/models/spree/image.rb@@ -5,6 +5,9 @@ module Spreeinclude Spree::ImageMethodsafter_commit :touch_product_variants, if: :should_touch_product_variants?, on: :update+ after_commit :update_variant_thumbnail, on: [:create, :destroy]+ after_commit :update_variant_thumbnail_on_reorder, on: :update, if: :saved_change_to_position?+ after_commit :update_variant_thumbnail_on_viewable_change, on: :update, if: :saved_change_to_viewable_id?after_create :increment_viewable_image_countafter_destroy :decrement_viewable_image_count@@ -84,5 +87,20 @@ module SpreeSpree::Variant.decrement_counter(:image_count, viewable_id)Spree::Product.decrement_counter(:total_image_count, viewable.product_id)end++ def update_variant_thumbnail+ return unless viewable.is_a?(Spree::Variant)++ viewable.update_thumbnail!+ viewable.product.update_thumbnail!+ end++ def update_variant_thumbnail_on_reorder+ update_variant_thumbnail+ end++ def update_variant_thumbnail_on_viewable_change+ update_variant_thumbnail+ endendenddiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 34becdeb08..84a2369fb0 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -119,6 +119,8 @@ module Spreehas_many :variant_images, -> { order(:position) }, source: :images, through: :variants_including_masterhas_many :variant_images_without_master, -> { order(:position) }, source: :images, through: :variants+ belongs_to :thumbnail, class_name: 'Spree::Image', optional: true+has_many :option_value_variants, class_name: 'Spree::OptionValueVariant', through: :variantshas_many :option_values, class_name: 'Spree::OptionValue', through: :variants@@ -367,9 +369,10 @@ module Spreeend# Returns default Image for Product.+ # Uses cached thumbnail_id which is updated when images are added/removed/reordered.# @return [Spree::Image, nil]def default_image- variant_for_images&.primary_image+ thumbnailend# Backward compatibility for Spree 5.2 and earlier.@@ -395,6 +398,13 @@ module Spreevariant_for_images&.image_count || 0end+ # Updates the thumbnail_id to the first image from variant_images.+ # Called when images are added, removed, or reordered on any variant.+ def update_thumbnail!+ first_image = variant_images.order(:position).first+ update_column(:thumbnail_id, first_image&.id)+ end+# Finds first variant with images using preloaded data when available.# @return [Spree::Variant, nil]def find_variant_with_imagesdiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 0d9a3d0438..b8a7d64619 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -49,6 +49,7 @@ module Spreehas_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'+ belongs_to :thumbnail, class_name: 'Spree::Image', optional: truehas_many :prices,class_name: 'Spree::Price',@@ -295,10 +296,17 @@ module Spreeimage_count.positive?end- # Returns default Image for Variant, falling back to product's default image.+ # Returns default Image for Variant.# @return [Spree::Image, nil]def default_image- @default_image ||= has_images? ? images.first : product.default_image+ thumbnail+ end++ # Updates the thumbnail_id to the first image by position.+ # Called when images are added, removed, or reordered.+ def update_thumbnail!+ first_image = images.order(:position).first+ update_column(:thumbnail_id, first_image&.id)end# Returns first Image for Variant.diff --git a/core/db/migrate/20260131000000_add_thumbnail_id_to_spree_variants_and_products.rb b/core/db/migrate/20260131000000_add_thumbnail_id_to_spree_variants_and_products.rbnew file mode 100644index 0000000000..44fed1f80b--- /dev/null+++ b/core/db/migrate/20260131000000_add_thumbnail_id_to_spree_variants_and_products.rb@@ -0,0 +1,9 @@+class AddThumbnailIdToSpreeVariantsAndProducts < ActiveRecord::Migration[7.2]+ def change+ add_column :spree_variants, :thumbnail_id, :bigint, if_not_exists: true+ add_column :spree_products, :thumbnail_id, :bigint, if_not_exists: true++ add_index :spree_variants, :thumbnail_id, if_not_exists: true+ add_index :spree_products, :thumbnail_id, if_not_exists: true+ end+enddiff --git a/core/lib/tasks/images.rake b/core/lib/tasks/images.rakenew file mode 100644index 0000000000..db37112d44--- /dev/null+++ b/core/lib/tasks/images.rake@@ -0,0 +1,20 @@+namespace :spree do+ namespace :images do+ desc 'Backfill thumbnail_id for all variants and products'+ task backfill_thumbnails: :environment do+ puts 'Backfilling variant thumbnails...'+ Spree::Variant.where(thumbnail_id: nil).where.not(image_count: 0).find_each do |variant|+ first_image = variant.images.order(:position).first+ variant.update_column(:thumbnail_id, first_image.id) if first_image+ end++ puts 'Backfilling product thumbnails...'+ Spree::Product.where(thumbnail_id: nil).where.not(total_image_count: 0).find_each do |product|+ first_image = product.variant_images.order(:position).first+ product.update_column(:thumbnail_id, first_image.id) if first_image+ end++ puts 'Done!'+ end+ end+enddiff --git a/core/spec/models/spree/image_spec.rb b/core/spec/models/spree/image_spec.rbindex f2f5c24ac6..ba71c48227 100644--- a/core/spec/models/spree/image_spec.rb+++ b/core/spec/models/spree/image_spec.rb@@ -123,4 +123,47 @@ describe Spree::Image, type: :model doexpect(product.reload.total_image_count).to eq(3)endend++ describe 'thumbnail_id updates' do+ let(:product) { create(:product) }+ let(:variant) { product.master }++ it 'sets variant thumbnail_id when first image is created' do+ expect(variant.thumbnail_id).to be_nil+ image = create(:image, viewable: variant)+ expect(variant.reload.thumbnail_id).to eq(image.id)+ end++ it 'sets product thumbnail_id when first image is created' do+ expect(product.thumbnail_id).to be_nil+ image = create(:image, viewable: variant)+ expect(product.reload.thumbnail_id).to eq(image.id)+ end++ it 'updates thumbnail_id when first image is destroyed' do+ image1 = create(:image, viewable: variant, position: 1)+ image2 = create(:image, viewable: variant, position: 2)+ expect(variant.reload.thumbnail_id).to eq(image1.id)++ image1.destroy+ expect(variant.reload.thumbnail_id).to eq(image2.id)+ end++ it 'sets thumbnail_id to nil when last image is destroyed' do+ image = create(:image, viewable: variant)+ expect(variant.reload.thumbnail_id).to eq(image.id)++ image.destroy+ expect(variant.reload.thumbnail_id).to be_nil+ end++ it 'updates thumbnail_id when image position changes' do+ image1 = create(:image, viewable: variant, position: 1)+ image2 = create(:image, viewable: variant, position: 2)+ expect(variant.reload.thumbnail_id).to eq(image1.id)++ image2.update!(position: 0)+ expect(variant.reload.thumbnail_id).to eq(image2.id)+ end+ endenddiff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex 31feb47c8a..3dd81c3377 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -1405,12 +1405,8 @@ describe Spree::Variant, type: :model doendcontext '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)+ it 'returns nil' do+ expect(variant.default_image).to be_nilendendenddiff --git a/storefront/app/controllers/spree/store_controller.rb b/storefront/app/controllers/spree/store_controller.rbindex 4b3a79abe2..02a61c1750 100644--- a/storefront/app/controllers/spree/store_controller.rb+++ b/storefront/app/controllers/spree/store_controller.rb@@ -184,9 +184,10 @@ module Spree# These includes are not picked automatically by ar_lazy_preload gem so we need to specify them manually.def storefront_products_includes{+ thumbnail: [attachment_attachment: :blob],taxons: [],- master: [:images, :prices, { stock_items: :stock_location }],- variants: [:images, :prices, { stock_items: :stock_location }]+ master: [:prices, { stock_items: :stock_location }],+ variants: [:prices, { stock_items: :stock_location }]}end