Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added additional Taxon Parent must belong to same Taxonomy validation

Damian Legawiec68548be640ff
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 36a7feabf1..26a1fb9a66 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -28,6 +28,7 @@ module Spree
validates :hide_from_nav, inclusion: { in: [true, false] }
validates_associated :icon
validate :check_for_root, on: :create
+ validate :parent_belongs_to_same_taxonomy
with_options length: { maximum: 255 }, allow_blank: true do
validates :meta_keywords
validates :meta_description
@@ -112,5 +113,11 @@ module Spree
errors.add(:root_conflict, 'this taxonomy already has a root taxon')
end
end
+
+ def parent_belongs_to_same_taxonomy
+ if parent.present? && parent.taxonomy_id != taxonomy_id
+ errors.add(:parent, 'must belong to the same taxonomy')
+ end
+ end
end
end
diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rb
index 2ce6540729..f4aec4fcfa 100644
--- a/core/spec/models/spree/taxon_spec.rb
+++ b/core/spec/models/spree/taxon_spec.rb
@@ -3,7 +3,6 @@ require 'spec_helper'
describe Spree::Taxon, type: :model do
let(:taxonomy) { create(:taxonomy) }
let(:taxon) { build(:taxon, name: 'Ruby on Rails', parent_id: nil) }
- let(:valid_taxon) { build(:taxon, name: 'Vaild Rails', parent_id: 1, taxonomy_id: taxonomy.id) }
describe '#to_param' do
subject { super().to_param }
@@ -11,13 +10,30 @@ describe Spree::Taxon, type: :model do
it { is_expected.to eql taxon.permalink }
end
- context 'check_for_root' do
- it 'does not validate the taxon' do
- expect(taxon.valid?).to eq false
+ context 'validations' do
+ describe '#check_for_root' do
+ let(:valid_taxon) { build(:taxon, name: 'Vaild Rails', parent_id: 1, taxonomy: taxonomy) }
+
+ it 'does not validate the taxon' do
+ expect(taxon.valid?).to eq false
+ end
+
+ it 'validates the taxon' do
+ expect(valid_taxon.valid?).to eq true
+ end
end
- it 'validates the taxon' do
- expect(valid_taxon.valid?).to eq true
+ describe '#parent_belongs_to_same_taxonomy' do
+ let(:valid_parent) { create(:taxon, name: 'Valid Parent', taxonomy: taxonomy) }
+ let(:invalid_parent) { create(:taxon, name: 'Invalid Parent', taxonomy: create(:taxonomy)) }
+
+ it 'does not validate the taxon' do
+ expect(build(:taxon, taxonomy: taxonomy, parent: invalid_parent).valid?).to eq false
+ end
+
+ it 'validates the taxon' do
+ expect(build(:taxon, taxonomy: taxonomy, parent: valid_parent).valid?).to eq true
+ end
end
end