Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion core/app/models/spree/taxonomy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Taxonomy < Spree::Base
has_one :root, -> { where parent_id: nil }, class_name: "Spree::Taxon", dependent: :destroy

after_create :set_root
after_save :set_root_taxon_name
after_update :set_root_taxon_name, if: :name_changed?

default_scope { order("#{self.table_name}.position, #{self.table_name}.created_at") }

Expand Down
33 changes: 23 additions & 10 deletions core/spec/models/spree/taxonomy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
require 'spec_helper'

describe Spree::Taxonomy, :type => :model do
context "#destroy" do
before do
@taxonomy = create(:taxonomy)
@root_taxon = @taxonomy.root
@child_taxon = create(:taxon, :taxonomy_id => @taxonomy.id, :parent => @root_taxon)
describe Spree::Taxonomy, type: :model do
let(:taxonomy) { create(:taxonomy) }
let(:root_taxon) { taxonomy.root }
let(:child_taxon) { create(:taxon, taxonomy_id: taxonomy.id, parent: root_taxon) }

describe '#destroy' do
before { taxonomy.destroy }

context 'should destroy all associated taxons' do
it { expect{ Spree::Taxon.find(root_taxon.id) }.to raise_error(ActiveRecord::RecordNotFound) }
it { expect{ Spree::Taxon.find(child_taxon.id) }.to raise_error(ActiveRecord::RecordNotFound) }
end
end

it "should destroy all associated taxons" do
@taxonomy.destroy
expect{ Spree::Taxon.find(@root_taxon.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect{ Spree::Taxon.find(@child_taxon.id) }.to raise_error(ActiveRecord::RecordNotFound)
describe 'callbacks' do
it { is_expected.to callback(:set_root).after(:create) }
it { is_expected.to callback(:set_root_taxon_name).after(:update).if(:name_changed?) }
end

describe '#set_root_taxon_name' do
before do
taxonomy.update_column(:name, 'test')
taxonomy.send(:set_root_taxon_name)
end

it { expect(taxonomy.root.name).to eq(taxonomy.name) }
end
end