Skip to content

Commit

Permalink
Merge pull request #5189 from nebulab/ryanofwoods/fix-taxon-taxonomy_…
Browse files Browse the repository at this point in the history
…id-validation

Fix Taxon taxonomy id validation regression
  • Loading branch information
kennyadsl authored Jun 29, 2023
2 parents 9a273b8 + b574c21 commit 8206c05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Taxon < Spree::Base
validates :meta_keywords, length: { maximum: 255 }
validates :meta_description, length: { maximum: 255 }
validates :meta_title, length: { maximum: 255 }
validates :taxonomy_id, uniqueness: { message: :can_have_only_one_root }, if: -> { root? }
validates :taxonomy_id, uniqueness: { scope: :parent_id, message: :can_have_only_one_root }, if: -> { root? }

after_save :touch_ancestors_and_taxonomy
after_touch :touch_ancestors_and_taxonomy
Expand Down
17 changes: 14 additions & 3 deletions core/spec/models/spree/taxon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,27 @@
context "validations" do
context "taxonomy_id validations" do
let(:taxonomy) { create(:taxonomy) }
let(:taxon) { taxonomy.taxons.create(name: 'New node') }

it "ensures that only one root can be created" do
taxon = taxonomy.taxons.create(name: 'New node')
expect(taxon).to be_invalid
expect(taxon.errors.full_messages).to match_array(["Taxonomy can only have one root Taxon"])
end

it "allows for multiple taxons under taxonomy" do
expect(taxon.update(parent_id: taxonomy.root.id)).to eq(true)
it "allows for multiple taxons under a taxonomy" do
taxon = taxonomy.root.children.create!(name: 'First child', taxonomy: taxonomy)
expect(taxon).to be_valid
expect(taxonomy.taxons.many?).to eq(true)
second_taxon = taxonomy.root.children.create!(name: 'Second child', taxonomy: taxonomy)
expect(second_taxon).to be_valid
expect(taxonomy.root.children.many?).to eq(true)
end

# Regression test https://github.com/solidusio/solidus/issues/5187
it "does not invalidate the root taxon after having children taxons" do
taxonomy.root.children.create!(name: 'New node', taxonomy: taxonomy)
expect(taxonomy.taxons.many?).to eq(true)
expect(taxonomy.root).to be_valid
end
end

Expand Down

0 comments on commit 8206c05

Please sign in to comment.