Skip to content

Commit 1c98299

Browse files
committed
Introduce primary taxon for product
Created a new association between Product and Primary Taxon using the existing Taxon model.
1 parent 95a9ea8 commit 1c98299

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

core/app/models/spree/product.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Product < Spree::Base
3232

3333
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
3434
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products, optional: true
35+
belongs_to :primary_taxon, class_name: 'Spree::Taxon', optional: true
3536

3637
has_one :master,
3738
-> { where(is_master: true).with_discarded },

core/config/locales/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ en:
179179
name: Name
180180
on_hand: On Hand
181181
price: Master Price
182+
primary_taxon: Primary Taxon
183+
primary_taxon_id: Primary Taxon
182184
promotionable: Promotable
183185
shipping_category: Shipping Category
184186
sku: Master SKU
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddPrimaryTaxonToProducts < ActiveRecord::Migration[7.0]
2+
def change
3+
change_table :spree_products do |t|
4+
t.references :primary_taxon, { to_table: :spree_taxons }
5+
end
6+
end
7+
end

core/lib/spree/permitted_attributes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module PermittedAttributes
7878
:meta_keywords, :price, :sku, :deleted_at,
7979
:option_values_hash, :weight, :height, :width, :depth,
8080
:shipping_category_id, :tax_category_id,
81-
:taxon_ids, :option_type_ids, :cost_currency, :cost_price
81+
:taxon_ids, :option_type_ids, :cost_currency, :cost_price, :primary_taxon_id
8282
]
8383

8484
@@property_attributes = [:name, :presentation]

core/spec/models/spree/product_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,22 @@ class Extension < Spree::Base
452452
end
453453
end
454454
end
455+
456+
describe "primary_taxon" do
457+
it 'should belong to primary_taxon' do
458+
expect(Spree::Product.reflect_on_association(:primary_taxon).macro).to eq(:belongs_to)
459+
end
460+
461+
it 'should be optional' do
462+
association = Spree::Product.reflect_on_association(:primary_taxon)
463+
expect(association.options[:optional]).to be(true)
464+
end
465+
466+
it 'should have a class_name of Spree::Taxon' do
467+
association = Spree::Product.reflect_on_association(:primary_taxon)
468+
expect(association.class_name).to eq('Spree::Taxon')
469+
end
470+
end
455471
end
456472
end
457473

@@ -709,4 +725,13 @@ class Extension < Spree::Base
709725
end
710726
end
711727
end
728+
729+
it 'is valid with or without a primary_taxon' do
730+
product_with_taxon = create(:product, primary_taxon: create(:taxon))
731+
732+
product_without_taxon = create(:product, primary_taxon: nil)
733+
734+
expect(product_with_taxon).to be_valid
735+
expect(product_without_taxon).to be_valid
736+
end
712737
end

0 commit comments

Comments
 (0)