Merge pull request #6679 from Matt-Yorkley/soft-delete-enterprise-fees

Soft-delete Enterprise Fees
This commit is contained in:
Matt-Yorkley
2021-01-21 19:57:29 +01:00
committed by GitHub
5 changed files with 36 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
class EnterpriseFee < ActiveRecord::Base
include Spree::Core::CalculatedAdjustments
acts_as_paranoid
belongs_to :enterprise
belongs_to :tax_category, class_name: 'Spree::TaxCategory', foreign_key: 'tax_category_id'

View File

@@ -162,6 +162,13 @@ module Spree
result
end
# Allow accessing soft-deleted originator objects
def originator
return if originator_type.blank?
originator_type.constantize.unscoped { super }
end
private
def update_adjustable

View File

@@ -0,0 +1,5 @@
class AddDeletedAtToEnterpriseFee < ActiveRecord::Migration
def change
add_column :enterprise_fees, :deleted_at, :datetime
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20201219120055) do
ActiveRecord::Schema.define(version: 20210115143738) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -108,6 +108,7 @@ ActiveRecord::Schema.define(version: 20201219120055) do
t.datetime "updated_at", null: false
t.integer "tax_category_id"
t.boolean "inherits_tax_category", default: false, null: false
t.datetime "deleted_at"
end
add_index "enterprise_fees", ["enterprise_id"], name: "index_enterprise_fees_on_enterprise_id", using: :btree

View File

@@ -139,4 +139,24 @@ describe EnterpriseFee do
end.to change(order.adjustments, :count).by(0)
end
end
describe "soft-deletion" do
let(:tax_category) { create(:tax_category) }
let(:enterprise_fee) { create(:enterprise_fee, tax_category: tax_category ) }
let!(:adjustment) { create(:adjustment, originator: enterprise_fee) }
before do
enterprise_fee.destroy
enterprise_fee.reload
end
it "soft-deletes the enterprise fee" do
expect(enterprise_fee.deleted_at).to_not be_nil
end
it "can be accessed by old adjustments" do
expect(adjustment.reload.originator).to eq enterprise_fee
expect(adjustment.originator.tax_category).to eq enterprise_fee.tax_category
end
end
end