Skip to content

Commit

Permalink
Check If Record Has DeletedAt Column
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
taufek committed Sep 21, 2023
1 parent 1ed0221 commit 0f58db2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/permanent_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 21 additions & 14 deletions spec/permanent_records_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/support/bed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Bed < ActiveRecord::Base
has_one :kitty
end
1 change: 1 addition & 0 deletions spec/support/kitty.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Kitty < ActiveRecord::Base
has_and_belongs_to_many :beds, dependent: :destroy
end
10 changes: 10 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 0f58db2

Please sign in to comment.