Skip to content

Commit

Permalink
Add lookup of variants by semantic id
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Sep 12, 2024
1 parent 2e2c890 commit 63530b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ def self.active(currency = nil)
select("spree_variants.id") })
end

def self.linked_to(semantic_id)
includes(:semantic_links).references(:semantic_links)
.where(semantic_links: { semantic_id: }).first
end

def tax_category
super || TaxCategory.find_by(is_default: true)
end
Expand Down
13 changes: 13 additions & 0 deletions spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,19 @@
end
end

describe ".linked_to" do
let!(:variant_unlinked) { create(:variant) }
let!(:variant_linked) { create(:variant, semantic_links: [link]) }
let!(:variant_linked_unrelated) { create(:variant, semantic_links: [unrelated_link]) }
let(:link) { SemanticLink.new(semantic_id: "#my_precious") }
let(:unrelated_link) { SemanticLink.new(semantic_id: "#other") }

it "finds a variant by link" do
expect(Spree::Variant.linked_to("#my_precious"))
.to eq variant_linked
end
end

describe "generating the product and variant name" do
let(:product) { variant.product }

Expand Down

0 comments on commit 63530b9

Please sign in to comment.