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 primary Taxon to products (#6047) #6109

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Introduce primary taxon for product
Created a new association between Product and Primary Taxon using the existing Taxon model.
  • Loading branch information
JustShah committed Feb 13, 2025
commit 1c98299d273696c15d8e0249bb10ae476eb5f2cc
1 change: 1 addition & 0 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Product < Spree::Base

belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products, optional: true
belongs_to :primary_taxon, class_name: 'Spree::Taxon', optional: true

has_one :master,
-> { where(is_master: true).with_discarded },
Expand Down
2 changes: 2 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ en:
name: Name
on_hand: On Hand
price: Master Price
primary_taxon: Primary Taxon
primary_taxon_id: Primary Taxon
promotionable: Promotable
shipping_category: Shipping Category
sku: Master SKU
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddPrimaryTaxonToProducts < ActiveRecord::Migration[7.0]
def change
change_table :spree_products do |t|
t.references :primary_taxon, { to_table: :spree_taxons }
end
end
end
2 changes: 1 addition & 1 deletion core/lib/spree/permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module PermittedAttributes
:meta_keywords, :price, :sku, :deleted_at,
:option_values_hash, :weight, :height, :width, :depth,
:shipping_category_id, :tax_category_id,
:taxon_ids, :option_type_ids, :cost_currency, :cost_price
:taxon_ids, :option_type_ids, :cost_currency, :cost_price, :primary_taxon_id
]

@@property_attributes = [:name, :presentation]
Expand Down
25 changes: 25 additions & 0 deletions core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,22 @@ class Extension < Spree::Base
end
end
end

describe "primary_taxon" do
it 'should belong to primary_taxon' do
expect(Spree::Product.reflect_on_association(:primary_taxon).macro).to eq(:belongs_to)
end

it 'should be optional' do
association = Spree::Product.reflect_on_association(:primary_taxon)
expect(association.options[:optional]).to be(true)
end

it 'should have a class_name of Spree::Taxon' do
association = Spree::Product.reflect_on_association(:primary_taxon)
expect(association.class_name).to eq('Spree::Taxon')
end
end
end
end

Expand Down Expand Up @@ -709,4 +725,13 @@ class Extension < Spree::Base
end
end
end

it 'is valid with or without a primary_taxon' do
product_with_taxon = create(:product, primary_taxon: create(:taxon))

product_without_taxon = create(:product, primary_taxon: nil)

expect(product_with_taxon).to be_valid
expect(product_without_taxon).to be_valid
end
end