Skip to content

Commit

Permalink
Introducing product brand using taxon_brand_selector
Browse files Browse the repository at this point in the history
  • Loading branch information
shahmayur001 committed Dec 3, 2024
1 parent 9013f71 commit 9c23ff9
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def user

it "cannot be destroyed" do
is_expected.to be_forbidden
expect(subject.body).to eq I18n.t("spree.error_user_destroy_with_orders")
expect(subject.body).to eq("Cannot delete a user with orders")
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ def gallery
@gallery ||= Spree::Config.product_gallery_class.new(self)
end

def brand
Spree::Config.brand_selector_class.new(self).call
end

private

def any_variants_not_track_inventory?
Expand Down
22 changes: 22 additions & 0 deletions core/app/models/spree/taxon_brand_selector.rb
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
3 changes: 3 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,8 @@ en:
billing: Billing
billing_address: Billing Address
both: Both
brand: Brand
brands: Brands
calculated_reimbursements: Calculated Reimbursements
calculator: Calculator
calculator_settings_warning: If you are changing the calculator type or preference source, you must save first before you can edit the calculator settings
Expand Down Expand Up @@ -1516,6 +1518,7 @@ en:
end: End
ending_in: Ending in
error: error
error_user_destroy_with_orders: Cannot delete a user with orders
errors:
messages:
cannot_delete_finalized_stock_location: Stock Location cannot be destroyed if you have open stock transfers.
Expand Down
7 changes: 7 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,13 @@ def payment_canceller
# Spree::StoreCreditPrioritizer.
class_name_attribute :store_credit_prioritizer_class, default: 'Spree::StoreCreditPrioritizer'

# Allows finding brand for product.
#
# @!attribute [rw] brand_selector_class
# @return [Class] a class with the same public interfaces as
# Spree::TaxonBrandSelector.
class_name_attribute :brand_selector_class, default: 'Spree::TaxonBrandSelector'

# @!attribute [rw] taxon_image_style_default
#
# Defines which style to default to when style is not provided
Expand Down
46 changes: 46 additions & 0 deletions core/spec/models/spree/taxon_brand_selector_spec.rb
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
6 changes: 4 additions & 2 deletions sample/db/samples/taxonomies.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

store = Spree::Store.where(code: 'sample-store').first

taxonomies = [
{ name: "Categories" },
{ name: "Brand" }
{ name: "Categories", store: },
{ name: "Brands", store: }
]

taxonomies.each do |taxonomy_attrs|
Expand Down
24 changes: 24 additions & 0 deletions sample/db/samples/taxons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Spree::Sample.load_sample("products")

categories = Spree::Taxonomy.find_by!(name: "Categories")
brands = Spree::Taxonomy.find_by!(name: "Brands")

products = {
solidus_bottles: "Solidus Water Bottle",
Expand All @@ -29,6 +30,29 @@
name: "Categories",
taxonomy: categories,
},
{
name: "Brands",
taxonomy: brands
},
{
name: "Solidus",
taxonomy: brands,
parent: "Brands",
products: [
products[:solidus_tote],
products[:solidus_hoodie],
products[:solidus_mug_set],
products[:solidus_hat],
products[:solidus_sticker],
products[:solidus_notebook],
products[:solidus_tshirt],
products[:solidus_long_sleeve_tee],
products[:solidus_dark_tee],
products[:solidus_bottles],
products[:solidus_canvas_tote],
products[:solidus_cap]
]
},
{
name: "Clothing",
taxonomy: categories,
Expand Down

0 comments on commit 9c23ff9

Please sign in to comment.