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

Introducing product brand using taxon_brand_selector #5989

Merged

Conversation

shahmayur001
Copy link
Contributor

Summary

This implementation introduces a brand concept to Spree products by adding a brand method to the Spree::Product model. This method delegates brand selection to a configurable class, enabling modularity and customization.

The new Spree::TaxonBrandSelector class fetches brands based on the Brands taxonomy. Developers can customize this functionality through the brand_selector_class attribute in Spree::AppConfiguration.


Implementation

Model Method Implementation

A brand method has been added to the Spree::Product model to retrieve the associated brand.

Changes in Spree::Product

The new brand method is implemented as follows:

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

Class Implementation

The 'Spree::TaxonBrandSelector' class encapsulates the logic for identifying the brand:

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 class queries the database for taxons associated with the product and matches the 'Brands' taxonomy.

Configuration Implementation
A new configuration attribute was added to 'Spree::AppConfiguration' to specify the class responsible for selecting the brand for a product:

class_name_attribute :brand_selector_class, default: 'Spree::TaxonBrandSelector'

This configuration allows developers to override the default behavior by providing a custom class. By default, it uses 'Spree::TaxonBrandSelector'.

Testing
Unit tests were added to verify that the selector correctly identifies the first 'Brands' taxon.

RSpec Example

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
  end
end

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@github-actions github-actions bot added changelog:solidus_backend Changes to the solidus_backend gem changelog:solidus_core Changes to the solidus_core gem changelog:solidus_sample Changes to the solidus_sample gem and removed changelog:solidus_backend Changes to the solidus_backend gem labels Dec 3, 2024
@shahmayur001 shahmayur001 marked this pull request as ready for review December 3, 2024 12:46
@shahmayur001 shahmayur001 requested a review from a team as a code owner December 3, 2024 12:46
@fthobe
Copy link
Contributor

fthobe commented Dec 3, 2024

Documentation has also be added here: solidusio/edgeguides#138

@shahmayur001 shahmayur001 mentioned this pull request Dec 3, 2024
3 tasks
@kennyadsl
Copy link
Member

Thanks for the contribution, again.

The failure seems related. Also, there's still an extra merge commit. Let me know if I can help with doing a proper rebase.

@shahmayur001 shahmayur001 force-pushed the add_brand_to_product_from_config branch from 87fb968 to 9c23ff9 Compare December 3, 2024 14:42
@github-actions github-actions bot added changelog:solidus_backend Changes to the solidus_backend gem and removed changelog:solidus_backend Changes to the solidus_backend gem labels Dec 3, 2024
Copy link

codecov bot commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.46%. Comparing base (f7a1391) to head (603714a).
Report is 29 commits behind head on main.

Files with missing lines Patch % Lines
core/app/models/spree/product.rb 50.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5989   +/-   ##
=======================================
  Coverage   89.46%   89.46%           
=======================================
  Files         782      783    +1     
  Lines       17999    18011   +12     
=======================================
+ Hits        16102    16113   +11     
- Misses       1897     1898    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@shahmayur001 shahmayur001 force-pushed the add_brand_to_product_from_config branch from a7a5b0c to 224d570 Compare December 3, 2024 17:12
@github-actions github-actions bot added changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem and removed changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Dec 3, 2024
@shahmayur001 shahmayur001 force-pushed the add_brand_to_product_from_config branch from c20c992 to 2f49b21 Compare December 3, 2024 17:17
@github-actions github-actions bot added changelog:solidus_admin changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem and removed changelog:solidus_admin changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Dec 3, 2024
@shahmayur001 shahmayur001 force-pushed the add_brand_to_product_from_config branch from 4e2c5f9 to df9499e Compare December 3, 2024 17:50
@github-actions github-actions bot added changelog:solidus_admin changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Dec 3, 2024
@shahmayur001 shahmayur001 force-pushed the add_brand_to_product_from_config branch from df9499e to c156f37 Compare December 3, 2024 17:51
@github-actions github-actions bot removed changelog:solidus_admin changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Dec 3, 2024
@shahmayur001 shahmayur001 force-pushed the add_brand_to_product_from_config branch from a3d6922 to 54c70ab Compare December 3, 2024 19:28
@fthobe
Copy link
Contributor

fthobe commented Dec 3, 2024

@kennyadsl should be fine now, sorry for not cleaning up behind ourselves :)

Copy link
Member

@jarednorman jarednorman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we think this change requires a minor version bump? My main worry is that I know of existing stores that already have a brand column, and this is a breaking change for them, even if it's trivial to implement the selector in that case.

@fthobe
Copy link
Contributor

fthobe commented Dec 4, 2024

@jarednorman yes, I'd also polish the edge guide a little to contain backend instructions.

@fthobe
Copy link
Contributor

fthobe commented Dec 4, 2024

Do we think this change requires a minor version bump? My main worry is that I know of existing stores that already have a brand column, and this is a breaking change for them, even if it's trivial to implement the selector in that case.

It would be indicated. Let's see if we manage also to place meta data in that release. Deal?

@shahmayur001 shahmayur001 force-pushed the add_brand_to_product_from_config branch from 614fdf4 to 603714a Compare December 4, 2024 18:20
@kennyadsl
Copy link
Member

Do we think this change requires a minor version bump?

Do you mean major @jarednorman? BTW I dont' think we need to treat this change as a backward incompatibility, as it's not changing something existing. A special note in the next minor version changelog would be enough IMO.

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kennyadsl kennyadsl merged commit 2d5bda7 into solidusio:main Dec 7, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem changelog:solidus_sample Changes to the solidus_sample gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants