Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing image in autocomplete variant #3032

Merged
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
5 changes: 4 additions & 1 deletion core/app/models/spree/gallery/variant_gallery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def initialize(variant)
#
# @return [Enumerable<Spree::Image>] all images in the gallery
def images
@images ||= @variant.images
@images ||=
@variant.images.presence ||
(!@variant.is_master? && @variant.product.master.images).presence ||
Spree::Image.none
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/gallery/variant_gallery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

RSpec.describe Spree::Gallery::VariantGallery do
let(:gallery) { described_class.new(variant) }
let(:variant) { Spree::Variant.new }
let(:variant) { build_stubbed(:variant) }

shared_context 'has multiple images' do
let(:first_image) { build(:image) }
Expand Down
41 changes: 37 additions & 4 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -811,12 +811,45 @@
end
end

describe '#gallery' do
let(:product) { Spree::Variant.new }
subject { product.gallery }
describe "#gallery" do
let(:variant) { build_stubbed(:variant) }
subject { variant.gallery }

it 'responds to #images' do
it "responds to #images" do
expect(subject).to respond_to(:images)
end

context "when variant.images is empty" do
let(:product) { create(:product) }
let(:variant) { create(:variant, product: product) }

it "fallbacks to variant.product.master.images" do
product.master.images = [create(:image)]

expect(product.master).not_to eq variant

expect(variant.gallery.images).to eq product.master.images
end

context "and variant.product.master.images is also empty" do
it "returns Spree::Image.none" do
expect(product.master).not_to eq variant
expect(product.master.images.presence).to be nil

expect(variant.gallery.images).to eq Spree::Image.none
end
end

context "and is master" do
it "returns Spree::Image.none" do
variant = product.master

expect(variant.is_master?).to be true
expect(variant.images.presence).to be nil

expect(variant.gallery.images).to eq Spree::Image.none
end
end
end
end
end