From 0f58db252268f9ca35ae128b488729ac323aaa2b Mon Sep 17 00:00:00 2001 From: Taufek Johar Date: Thu, 21 Sep 2023 14:42:07 +0800 Subject: [PATCH] Check If Record Has DeletedAt Column ## Context Previously, when model with has_many association without `deleted_at` column will raise "undefined method deleted_at=" error. With this change, it will revert to original destroy behavior when the record does not have `deleted_at` column. --- lib/permanent_records.rb | 2 +- spec/permanent_records_spec.rb | 35 ++++++++++++++++++++-------------- spec/support/bed.rb | 3 +++ spec/support/kitty.rb | 1 + spec/support/schema.rb | 10 ++++++++++ 5 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 spec/support/bed.rb diff --git a/lib/permanent_records.rb b/lib/permanent_records.rb index 820c5f8..1898fcc 100644 --- a/lib/permanent_records.rb +++ b/lib/permanent_records.rb @@ -215,7 +215,7 @@ def permanently_delete_records(dependent_records) record = klass.unscoped.where(klass.primary_key => id).first next unless record - record.deleted_at = nil + record.deleted_at = nil if record.respond_to?(:deleted_at) record.destroy(:force) end end diff --git a/spec/permanent_records_spec.rb b/spec/permanent_records_spec.rb index 19ab10b..331de34 100644 --- a/spec/permanent_records_spec.rb +++ b/spec/permanent_records_spec.rb @@ -10,7 +10,8 @@ let!(:location) { hole.create_location } let!(:difficulty) { hole.create_difficulty } let!(:comments) { 2.times.map { hole.comments.create! } } - let!(:kitty) { Kitty.create! } + let!(:bed) { Bed.create! } + let!(:kitty) { Kitty.create!(beds: [bed]) } let!(:meerkat) { Meerkat.create!(holes: [hole]) } describe '#destroy' do @@ -97,18 +98,6 @@ end end - context 'when model has no deleted_at column' do - let(:record) { kitty } - - it 'really removes the record' do - expect { subject }.to change { record.class.count }.by(-1) - end - - it 'makes deleted? return true' do - expect(subject).to be_deleted - end - end - context 'with dependent records' do context 'that are permanent' do it '' do @@ -202,6 +191,24 @@ it 'removes them' do expect { subject }.to change { Mole.count }.by(-1) end + + context 'with has many cardinality' do + context 'when model has no deleted_at column' do + let(:record) { kitty } + + it 'really removes the record' do + expect { subject }.to change { record.class.count }.by(-1) + end + + it 'really removes the associations' do + expect { subject }.to change { Bed.count }.by(-1) + end + + it 'makes deleted? return true' do + expect(subject).to be_deleted + end + end + end end context 'as default scope' do @@ -293,7 +300,7 @@ end context 'that were deleted previously' do - before { muskrat.update_attributes! deleted_at: 2.minutes.ago } + before { muskrat.update_attribute :deleted_at, 2.minutes.ago } it 'does not restore' do expect { subject }.to_not change(muskrat, :deleted?) end diff --git a/spec/support/bed.rb b/spec/support/bed.rb new file mode 100644 index 0000000..0a42450 --- /dev/null +++ b/spec/support/bed.rb @@ -0,0 +1,3 @@ +class Bed < ActiveRecord::Base + has_one :kitty +end diff --git a/spec/support/kitty.rb b/spec/support/kitty.rb index 483d0a6..c1c11af 100644 --- a/spec/support/kitty.rb +++ b/spec/support/kitty.rb @@ -1,2 +1,3 @@ class Kitty < ActiveRecord::Base + has_and_belongs_to_many :beds, dependent: :destroy end diff --git a/spec/support/schema.rb b/spec/support/schema.rb index bb2124c..0254311 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -13,8 +13,18 @@ t.references :hole end + create_table :beds, force: true do |t| + t.column :name, :string + end + create_table :kitties, force: true do |t| t.column :name, :string + t.references :bed + end + + create_table :beds_kitties, force: true do |t| + t.references :kitty + t.references :bed end create_table :holes, force: true do |t|