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

Add template variants scope #5866

Merged
merged 2 commits into from
Oct 21, 2024
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: 5 additions & 0 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class Variant < Spree::Base

after_destroy :destroy_option_values_variants

scope :template_variants, -> do
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
left_joins(product: { option_types: :option_values }).where(is_master: true).where.not(spree_option_values: { id: nil }).reorder(nil).distinct
end
scope :non_template_variants, -> { where.not(id: template_variants) }

# Returns variants that are in stock. When stock locations are provided as
# a parameter, the scope is limited to variants that are in stock in the
# provided stock locations.
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/core/search/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Variant

def initialize(query_string, scope: Spree::Variant.all)
@query_string = query_string
@scope = scope
@scope = scope.non_template_variants
end

# Searches the variants table using the ransack 'search_terms' defined on the class.
Expand All @@ -39,7 +39,7 @@ def results
@scope.ransack(search_term_params(word)).result.pluck(:id)
end

Spree::Variant.where(id: matches.inject(:&))
@scope.where(id: matches.inject(:&))
end

private
Expand Down
10 changes: 10 additions & 0 deletions core/spec/lib/search/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def refute_found(query_string, variant)
it { refute_found("bca", variant) }
end

context "with a template variant" do
let!(:option_type) { create(:option_type, option_values: [option_value]) }
let(:option_value) { build(:option_value) }
let(:product) { create(:product, option_types: [option_type], sku: "TEMPLATE") }
let(:variant) { create(:variant, product: product, sku: "NOT_TEMPLATE") }

it { refute_found("TEMPLATE", product.master) }
it { assert_found("NOT_TEMPLATE", variant) }
end

context "by product" do
it { assert_found("My Special Product", variant) }
it { assert_found("My Spec", variant) }
Expand Down
22 changes: 22 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@

let!(:variant) { create(:variant) }

describe '.non_template_variants' do
let(:option_type) { create(:option_type, option_values: [option_value]) }
let(:option_value) { build(:option_value) }
let(:product) { create(:product, option_types: [option_type]) }
let!(:variant) { create(:variant, product: product) }

subject { described_class.non_template_variants }

it { is_expected.to contain_exactly(variant) }
end

describe '.template_variants' do
let(:option_type) { create(:option_type, option_values: [option_value]) }
let(:option_value) { build(:option_value) }
let(:product) { create(:product, option_types: [option_type]) }
let!(:variant) { create(:variant, product: product) }

subject { described_class.template_variants }

it { is_expected.to contain_exactly(product.master) }
end

describe 'delegates' do
let(:product) { build(:product) }
let(:variant) { build(:variant, product: product) }
Expand Down
Loading