From 7c25be4fbba408183fcfc072528e2a4674b82d65 Mon Sep 17 00:00:00 2001 From: aishmita-aggarwal Date: Mon, 29 Feb 2016 17:43:07 +0530 Subject: [PATCH] Update callback for taxonomy --- core/app/models/spree/taxonomy.rb | 2 +- core/spec/models/spree/taxonomy_spec.rb | 33 +++++++++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/core/app/models/spree/taxonomy.rb b/core/app/models/spree/taxonomy.rb index 43a14f25ffe..da488520f45 100644 --- a/core/app/models/spree/taxonomy.rb +++ b/core/app/models/spree/taxonomy.rb @@ -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") } diff --git a/core/spec/models/spree/taxonomy_spec.rb b/core/spec/models/spree/taxonomy_spec.rb index 1aae5a0d871..7f204473e09 100644 --- a/core/spec/models/spree/taxonomy_spec.rb +++ b/core/spec/models/spree/taxonomy_spec.rb @@ -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