-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing product brand using taxon_brand_selector
- Loading branch information
1 parent
f7a1391
commit 54c70ab
Showing
6 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
class TaxonBrandSelector | ||
BRANDS_TAXONOMY_NAME = "Brands" | ||
|
||
def initialize(product) | ||
@product = product | ||
end | ||
|
||
def call | ||
product.taxons | ||
.joins(:taxonomy) | ||
.where(spree_taxonomies: { name: BRANDS_TAXONOMY_NAME }) | ||
.first | ||
end | ||
|
||
private | ||
|
||
attr_reader :product | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Spree::TaxonBrandSelector, type: :model do | ||
let(:taxonomy) { create(:taxonomy, name: "Brands") } | ||
let(:taxon) { create(:taxon, taxonomy: taxonomy, name: "Brand A") } | ||
let(:product) { create(:product, taxons: [taxon]) } | ||
|
||
subject { described_class.new(product) } | ||
|
||
describe "#call" do | ||
context "when the product has a taxon under the 'Brands' taxonomy" do | ||
it "returns the first taxon under 'Brands'" do | ||
expect(subject.call).to eq(taxon) | ||
end | ||
end | ||
|
||
context "when the product has multiple taxons under the 'Brands' taxonomy" do | ||
let(:taxon_b) { create(:taxon, taxonomy: taxonomy, name: "Brand B") } | ||
before { product.taxons << taxon_b } | ||
|
||
it "returns the first taxon under 'Brands'" do | ||
expect(subject.call).to eq(taxon) | ||
end | ||
end | ||
|
||
context "when the product does not have a taxon under the 'Brands' taxonomy" do | ||
let(:other_taxonomy) { create(:taxonomy, name: "Categories") } | ||
let(:other_taxon) { create(:taxon, taxonomy: other_taxonomy, name: "Category A") } | ||
let(:product) { create(:product, taxons: [other_taxon]) } | ||
|
||
it "returns nil" do | ||
expect(subject.call).to be_nil | ||
end | ||
end | ||
|
||
context "when the product has no taxons" do | ||
let(:product) { create(:product) } | ||
|
||
it "returns nil" do | ||
expect(subject.call).to be_nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters