Skip to content

Commit

Permalink
Dont update running total on discarded records
Browse files Browse the repository at this point in the history
  • Loading branch information
tr4b4nt committed Jan 17, 2019
1 parent fa4b370 commit 7e86a4d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/counter_culture/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ def counter_culture(relation, options = {})
if: -> (model) { !model.paranoia_destroyed? }
end

after_update :_update_counts_after_update
after_update :_update_counts_after_update, if: -> (model) do
if defined?(Discard::Model) && model.class.include?(Discard::Model)
!model.discarded?
else
true
end
end

if respond_to?(:before_restore)
before_restore :_update_counts_after_create,
Expand Down
33 changes: 33 additions & 0 deletions spec/counter_culture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,39 @@
expect(company.reload.soft_delete_discards_count).to eq(0)
end
end

describe "dynamic column names with totaling instead of counting" do
describe 'when updating discarded records' do
it 'does not update sum' do
skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"

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

expect(company.reload.soft_delete_discard_values_sum).to eq(5)

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

sd.update value: 10
expect(company.reload.soft_delete_discard_values_sum).to eq(0)
end
end

describe 'when updating undiscarded records' do
it 'updates sum' do
skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"

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

expect(company.reload.soft_delete_discard_values_sum).to eq(5)

sd.update value: 10
expect(company.reload.soft_delete_discard_values_sum).to eq(10)
end
end
end
end

describe "when using paranoia for soft deletes" do
Expand Down
1 change: 1 addition & 0 deletions spec/models/soft_delete_discard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class SoftDeleteDiscard < ActiveRecord::Base

belongs_to :company
counter_culture :company
counter_culture :company, column_name: 'soft_delete_discard_values_sum', delta_column: 'value'
end
2 changes: 2 additions & 0 deletions spec/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
t.integer "children_count", :default => 0, :null => false
t.integer "soft_delete_paranoia_count", :default => 0, :null => false
t.integer "soft_delete_discards_count", :default => 0, :null => false
t.integer "soft_delete_discard_values_sum", :default => 0, :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
Expand Down Expand Up @@ -184,6 +185,7 @@

create_table "soft_delete_discards", :force => true do |t|
t.integer "company_id", :null => false
t.integer "value", :default => 0
t.timestamp "discarded_at"
end

Expand Down

0 comments on commit 7e86a4d

Please sign in to comment.