Skip to content

Commit

Permalink
Dont update counts when hard-destroying discarded records.
Browse files Browse the repository at this point in the history
  • Loading branch information
tr4b4nt committed Nov 6, 2018
1 parent 6c62075 commit 55717cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/counter_culture/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def counter_culture(relation, options = {})
before_destroy :_update_counts_after_destroy, if: -> (model) do
if model.respond_to?(:paranoia_destroyed?)
!model.paranoia_destroyed?
elsif defined?(Discard::Model) && model.class.include?(Discard::Model)
!model.discarded?
else
true
end
Expand Down
29 changes: 29 additions & 0 deletions spec/counter_culture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,35 @@
sd.undiscard
expect(company.reload.soft_delete_discards_count).to eq(1)
end

describe "when calling hard-destroy" do
it "does not run destroy callback for discarded records" do
skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"

company = Company.create!
sd = SoftDeleteDiscard.create!(company_id: company.id)

expect(company.reload.soft_delete_discards_count).to eq(1)

sd.discard
expect(company.reload.soft_delete_discards_count).to eq(0)

sd.destroy
expect(company.reload.soft_delete_discards_count).to eq(0)
end

it "runs destroy callback for undiscarded records" do
skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"

company = Company.create!
sd = SoftDeleteDiscard.create!(company_id: company.id)

expect(company.reload.soft_delete_discards_count).to eq(1)

sd.destroy
expect(company.reload.soft_delete_discards_count).to eq(0)
end
end
end

describe "when using paranoia for soft deletes" do
Expand Down

0 comments on commit 55717cc

Please sign in to comment.