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 brand from config #5952

Closed

Conversation

shahmayur001
Copy link
Contributor

@shahmayur001 shahmayur001 commented Nov 27, 2024

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 the changelog:solidus_core Changes to the solidus_core gem label Nov 27, 2024
@shahmayur001 shahmayur001 marked this pull request as ready for review November 27, 2024 20:27
@shahmayur001 shahmayur001 requested a review from a team as a code owner November 27, 2024 20:27
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 for the contribution @shahmayur001!

Please, squash all commits into a single one with a meaningful title and description. Please, read all our contributing guidelines, in particular here.

It would also be great to:

  • update our seeds/samples to include a brand on the products, now that we have this, to make it more real.
  • document what a brand is on the guides, including how to customize the brand class with your own logic.

@fthobe
Copy link
Contributor

fthobe commented Dec 2, 2024

@kennyadsl There's no content about products or taxonomies what so every in the documentation. Where do you want the section for brands to be added?

@fthobe
Copy link
Contributor

fthobe commented Dec 2, 2024

@shahmayur001 Here you can find the sample data:
Taxon Sample File
Taxonomies Sample File
Please create a sample brand called solidus and nest it under the taxonomy Brands

@github-actions github-actions bot added the changelog:solidus_sample Changes to the solidus_sample gem label Dec 2, 2024
shahmayur001 and others added 15 commits December 2, 2024 23:26
We released two new gems and need the appropriate labels
for the pull request reviews.
This commit allows the menu of the new admin to accomodate routes from
other engines than solidus backend and solidus admin. This is needed for
`solidus_promotions`, which is built as a separate Rails Engine, but it
is also convenient for `solidus_paypal_commerce_platform` or even for
integrating gems like AlchemyCMS lateron.

Co-Authored-By: [email protected]
This makes sure that the promotion menus are always below the product
menu item, in both new admin and the backend.

It also changes the nomenclature to be "Promotions" for the legacy
promotion system and "Promotions (new)" for the new promotion system.
Prior to this commit, the new promotion system would exchange the
promotion and promotion category index page components if the gem was
loaded. This made it impossible to actually see the old promotion
configuration when using the new admin.

This commit now only changes the orders/index component if the new
promotion system is activated. In any case, it will display new
promotion and promotion category records under "Promotions (new)".
Not sure how we missed that when including `solidus_promotions`.
The  `authorization_subject` method from `solidus_admin` assumes that
all models are in the `Spree` namespace.
This controller still did not know how what to authorize against, and
wanted to visit a URL that doesn't exist when clicking on a promotion.

This also changes at least the name of each promotion to be a link
element that can easily be targeted with Capybara and works with all
major browsers.
Previously this would force the development server to run on 3000, but
it is useful to be able to override that.

Co-authored-by: Harmony Evangelina <[email protected]>
@fthobe
Copy link
Contributor

fthobe commented Dec 2, 2024

@jarednorman We are also integrating the taxons for brands inside the starter-frontend, at that point it makes sense to remove solidus from the product name in the sample data. Is that ok for you?

@fthobe fthobe deleted the add_brand_from_config branch December 3, 2024 11:26
@shahmayur001
Copy link
Contributor Author

Closing this PR as a clean version has been created: #5989.

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.

6 participants