counter_culture 1.11.0 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8e94799083d30c0bd97dbcdd70290c18bcfcd68
4
- data.tar.gz: 49074978dffb068623ecf6aec34483d84d4f3704
3
+ metadata.gz: f9d98f26efe6fc7cba57f168e7da8b2d9714df7c
4
+ data.tar.gz: 6b0b08fef2199be674313c4bac316b16cc40ac8d
5
5
  SHA512:
6
- metadata.gz: 12d07d5c17e2f17eaadf865f180849b2a140f5d6a59cf074fdcbd98ac27dae74c0a340052a4bf1ea444029d592be2270d5b93985d599c7615287bafa31c8edd3
7
- data.tar.gz: e5c827862773bb11853e2ee918b3b6559af9fcc087db6ec5bfd50cf428efed0d4fdd7560f0e3099e4e2237af6c17c7e31cce7a951af26c879953d11912638924
6
+ metadata.gz: f5174d35328f38ace6423f37c183a9d9f377508b50ee31d6e8c13a9af0fe39e563711898176e8c2911ae7e7b8c669c5e32dc9901b1d55d346bb3a3ba0437706a
7
+ data.tar.gz: 053cb5c12961aaf86a6bd0dbb43e23ebd1d5a05b2d18c39fac6e741518286ca4509e209a171fa84d1ee411c860228f535e11a7eedf1ff97c5d54a3dc92178d59
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.12.0 (June 8, 2018)
2
+
3
+ Improvements:
4
+ - Adds support for the [Discard](https://github.com/jhawthorn/discard) soft-delete gem (#220)
5
+
1
6
  ## 1.11.0 (May 4, 2018)
2
7
 
3
8
  Bugfixes:
data/Gemfile CHANGED
@@ -26,8 +26,18 @@ group :development, :test do
26
26
  gem "awesome_print"
27
27
  gem "timecop"
28
28
 
29
- # to test the integration
30
- gem "paranoia"
29
+ # To test integrations
30
+
31
+ # Both the paranoia and discard integrations require Rails > 4.2
32
+ # Actually parsing the resolved rails version is complicated, so
33
+ # we're basing this on the incompatible Rails version strings from
34
+ # .travis.yml
35
+
36
+ unless ['~> 3.2.0', '~> 4.0.0', '~> 4.1.0'].include?(rails)
37
+ gem "discard"
38
+ gem "paranoia"
39
+ end
40
+
31
41
  if RUBY_VERSION < "2.3.0"
32
42
  gem "paper_trail", "< 9.0.0"
33
43
  else
data/README.md CHANGED
@@ -313,13 +313,16 @@ Product.counter_culture_fix_counts skip_unsupported: true
313
313
 
314
314
  Manually populating counter caches with dynamically over-written foreign keys (```:foreign_key_values``` option) is not supported. You will have to write code to handle this case yourself.
315
315
 
316
- ### Soft-deletes / `paranoia` gem
316
+ ### Soft-deletes with `paranoia` or `discard`
317
317
 
318
- This gem will keep counters correctly updated when using the
319
- [`paranoia` gem](https://github.com/rubysherpas/paranoia)
320
- for soft-delete support. However, to ensure that counts are incremented after a
321
- restore you have to make sure that the call to `acts_as_paranoid` comes before
322
- the call to `counter_culture` in your model:
318
+ This gem will keep counters correctly updated in Rails 4.2 or later when using
319
+ [paranoia](https://github.com/rubysherpas/paranoia) or
320
+ [discard](https://github.com/jhawthorn/discard) for soft-delete support.
321
+ However, to ensure that counts are incremented after a restore you have
322
+ to make sure to set up soft deletion (via `acts_as_paranoid` or
323
+ `include Discard::Model`) before the call to `counter_culture` in your model:
324
+
325
+ #### Paranoia
323
326
 
324
327
  ```ruby
325
328
  class SoftDelete < ActiveRecord::Base
@@ -330,6 +333,17 @@ class SoftDelete < ActiveRecord::Base
330
333
  end
331
334
  ```
332
335
 
336
+ #### Discard
337
+
338
+ ```ruby
339
+ class SoftDelete < ActiveRecord::Base
340
+ include Discard::Model
341
+
342
+ belongs_to :company
343
+ counter_culture :company
344
+ end
345
+ ```
346
+
333
347
  ### PaperTrail integration
334
348
 
335
349
  If you are using the [`paper_trail` gem](https://github.com/airblade/paper_trail)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.11.0
1
+ 1.12.0
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: counter_culture 1.11.0 ruby lib
5
+ # stub: counter_culture 1.12.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "counter_culture"
9
- s.version = "1.11.0"
9
+ s.version = "1.12.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Magnus von Koeller"]
14
- s.date = "2018-05-07"
14
+ s.date = "2018-06-08"
15
15
  s.description = "counter_culture provides turbo-charged counter caches that are kept up-to-date not just on create and destroy, that support multiple levels of indirection through relationships, allow dynamic column names and that avoid deadlocks by updating in the after_commit callback."
16
16
  s.email = "magnus@vonkoeller.de"
17
17
  s.extra_rdoc_files = [
@@ -60,7 +60,8 @@ Gem::Specification.new do |s|
60
60
  "spec/models/simple_dependent.rb",
61
61
  "spec/models/simple_main.rb",
62
62
  "spec/models/simple_review.rb",
63
- "spec/models/soft_delete.rb",
63
+ "spec/models/soft_delete_discard.rb",
64
+ "spec/models/soft_delete_paranoia.rb",
64
65
  "spec/models/subcateg.rb",
65
66
  "spec/models/transaction.rb",
66
67
  "spec/models/twitter_review.rb",
@@ -134,6 +135,7 @@ Gem::Specification.new do |s|
134
135
  s.add_development_dependency(%q<rspec>, ["~> 3.0"])
135
136
  s.add_development_dependency(%q<awesome_print>, [">= 0"])
136
137
  s.add_development_dependency(%q<timecop>, [">= 0"])
138
+ s.add_development_dependency(%q<discard>, [">= 0"])
137
139
  s.add_development_dependency(%q<paranoia>, [">= 0"])
138
140
  s.add_development_dependency(%q<paper_trail>, [">= 0"])
139
141
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
@@ -148,6 +150,7 @@ Gem::Specification.new do |s|
148
150
  s.add_dependency(%q<rspec>, ["~> 3.0"])
149
151
  s.add_dependency(%q<awesome_print>, [">= 0"])
150
152
  s.add_dependency(%q<timecop>, [">= 0"])
153
+ s.add_dependency(%q<discard>, [">= 0"])
151
154
  s.add_dependency(%q<paranoia>, [">= 0"])
152
155
  s.add_dependency(%q<paper_trail>, [">= 0"])
153
156
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
@@ -163,6 +166,7 @@ Gem::Specification.new do |s|
163
166
  s.add_dependency(%q<rspec>, ["~> 3.0"])
164
167
  s.add_dependency(%q<awesome_print>, [">= 0"])
165
168
  s.add_dependency(%q<timecop>, [">= 0"])
169
+ s.add_dependency(%q<discard>, [">= 0"])
166
170
  s.add_dependency(%q<paranoia>, [">= 0"])
167
171
  s.add_dependency(%q<paper_trail>, [">= 0"])
168
172
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
@@ -19,6 +19,7 @@ module CounterCulture
19
19
 
20
20
  # initialize callbacks only once
21
21
  after_create :_update_counts_after_create
22
+
22
23
  before_destroy :_update_counts_after_destroy, if: -> (model) do
23
24
  if model.respond_to?(:paranoia_destroyed?)
24
25
  !model.paranoia_destroyed?
@@ -26,12 +27,22 @@ module CounterCulture
26
27
  true
27
28
  end
28
29
  end
30
+
29
31
  after_update :_update_counts_after_update
32
+
30
33
  if respond_to?(:before_restore)
31
34
  before_restore :_update_counts_after_create,
32
35
  if: -> (model) { model.deleted? }
33
36
  end
34
37
 
38
+ if defined?(Discard::Model) && include?(Discard::Model)
39
+ before_discard :_update_counts_after_destroy,
40
+ if: ->(model) { !model.discarded? }
41
+
42
+ before_undiscard :_update_counts_after_create,
43
+ if: ->(model) { model.discarded? }
44
+ end
45
+
35
46
  # we keep a list of all counter caches we must maintain
36
47
  @after_commit_counter_cache = []
37
48
  end
@@ -226,6 +226,14 @@ module CounterCulture
226
226
  if model.column_names.include?('deleted_at')
227
227
  joins_sql += " AND #{target_table_alias}.deleted_at IS NULL"
228
228
  end
229
+
230
+ # respect the discard column if it exists
231
+ if defined?(Discard::Model) &&
232
+ model.include?(Discard::Model) &&
233
+ model.column_names.include?(model.discard_column.to_s)
234
+
235
+ joins_sql += " AND #{target_table_alias}.#{model.discard_column} IS NULL"
236
+ end
229
237
  end
230
238
  joins_sql
231
239
  end
@@ -21,7 +21,8 @@ require 'models/another_post'
21
21
  require 'models/another_post_comment'
22
22
  require 'models/person'
23
23
  require 'models/transaction'
24
- require 'models/soft_delete'
24
+ require 'models/soft_delete_discard'
25
+ require 'models/soft_delete_paranoia'
25
26
  require 'models/conversation'
26
27
  require 'models/candidate_profile'
27
28
  require 'models/candidate'
@@ -1699,59 +1700,115 @@ describe "CounterCulture" do
1699
1700
  end
1700
1701
  end
1701
1702
 
1702
- describe "when using acts_as_paranoia" do
1703
+ describe "when using discard for soft deletes" do
1703
1704
  it "works" do
1704
1705
  skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"
1705
1706
  company = Company.create!
1706
- expect(company.soft_deletes_count).to eq(0)
1707
- sd = SoftDelete.create!(company_id: company.id)
1708
- expect(company.reload.soft_deletes_count).to eq(1)
1707
+ expect(company.soft_delete_discards_count).to eq(0)
1708
+ sd = SoftDeleteDiscard.create!(company_id: company.id)
1709
+ expect(company.reload.soft_delete_discards_count).to eq(1)
1710
+
1711
+ sd.discard
1712
+ sd.reload
1713
+ expect(sd).to be_discarded
1714
+ expect(company.reload.soft_delete_discards_count).to eq(0)
1715
+
1716
+ company.update_attributes(soft_delete_discards_count: 100)
1717
+ expect(company.reload.soft_delete_discards_count).to eq(100)
1718
+ SoftDeleteDiscard.counter_culture_fix_counts
1719
+ expect(company.reload.soft_delete_discards_count).to eq(0)
1720
+
1721
+ sd.undiscard
1722
+ expect(company.reload.soft_delete_discards_count).to eq(1)
1723
+ end
1724
+
1725
+ it "runs destroy callback only once" do
1726
+ skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"
1727
+
1728
+ company = Company.create!
1729
+ sd = SoftDeleteDiscard.create!(company_id: company.id)
1730
+
1731
+ expect(company.reload.soft_delete_discards_count).to eq(1)
1732
+
1733
+ sd.discard
1734
+ expect(company.reload.soft_delete_discards_count).to eq(0)
1735
+
1736
+ sd.discard
1737
+ expect(company.reload.soft_delete_discards_count).to eq(0)
1738
+ end
1739
+
1740
+ it "runs restore callback only once" do
1741
+ skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"
1742
+
1743
+ company = Company.create!
1744
+ sd = SoftDeleteDiscard.create!(company_id: company.id)
1745
+
1746
+ expect(company.reload.soft_delete_discards_count).to eq(1)
1747
+
1748
+ sd.discard
1749
+ expect(company.reload.soft_delete_discards_count).to eq(0)
1750
+
1751
+ sd.undiscard
1752
+ expect(company.reload.soft_delete_discards_count).to eq(1)
1753
+
1754
+ sd.undiscard
1755
+ expect(company.reload.soft_delete_discards_count).to eq(1)
1756
+ end
1757
+ end
1758
+
1759
+ describe "when using paranoia for soft deletes" do
1760
+ it "works" do
1761
+ skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"
1762
+ company = Company.create!
1763
+ expect(company.soft_delete_paranoia_count).to eq(0)
1764
+ sd = SoftDeleteParanoia.create!(company_id: company.id)
1765
+ expect(company.reload.soft_delete_paranoia_count).to eq(1)
1709
1766
 
1710
1767
  sd.destroy
1711
1768
  sd.reload
1712
1769
  expect(sd.deleted_at).to be_truthy
1713
- expect(company.reload.soft_deletes_count).to eq(0)
1770
+ expect(company.reload.soft_delete_paranoia_count).to eq(0)
1714
1771
 
1715
- company.update_attributes(soft_deletes_count: 100)
1716
- expect(company.reload.soft_deletes_count).to eq(100)
1717
- SoftDelete.counter_culture_fix_counts
1718
- expect(company.reload.soft_deletes_count).to eq(0)
1772
+ company.update_attributes(soft_delete_paranoia_count: 100)
1773
+ expect(company.reload.soft_delete_paranoia_count).to eq(100)
1774
+ SoftDeleteParanoia.counter_culture_fix_counts
1775
+ expect(company.reload.soft_delete_paranoia_count).to eq(0)
1719
1776
 
1720
1777
  sd.restore
1721
- expect(company.reload.soft_deletes_count).to eq(1)
1778
+ expect(company.reload.soft_delete_paranoia_count).to eq(1)
1722
1779
  end
1723
1780
 
1724
1781
  it "runs destroy callback only once" do
1725
1782
  skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"
1726
1783
 
1727
1784
  company = Company.create!
1728
- sd = SoftDelete.create!(company_id: company.id)
1785
+ sd = SoftDeleteParanoia.create!(company_id: company.id)
1729
1786
 
1730
- expect(company.reload.soft_deletes_count).to eq(1)
1787
+ expect(company.reload.soft_delete_paranoia_count).to eq(1)
1731
1788
 
1732
1789
  sd.destroy
1733
- expect(company.reload.soft_deletes_count).to eq(0)
1790
+ expect(company.reload.soft_delete_paranoia_count).to eq(0)
1734
1791
 
1735
1792
  sd.destroy
1736
- expect(company.reload.soft_deletes_count).to eq(0)
1793
+ expect(company.reload.soft_delete_paranoia_count).to eq(0)
1737
1794
  end
1738
1795
 
1739
1796
  it "runs restore callback only once" do
1740
1797
  skip("Unsupported in this version of Rails") if Rails.version < "4.2.0"
1741
1798
 
1742
1799
  company = Company.create!
1743
- sd = SoftDelete.create!(company_id: company.id)
1800
+ sd = SoftDeleteParanoia.create!(company_id: company.id)
1744
1801
 
1745
- expect(company.reload.soft_deletes_count).to eq(1)
1802
+ expect(company.reload.soft_delete_paranoia_count).to eq(1)
1746
1803
 
1747
1804
  sd.destroy
1748
- expect(company.reload.soft_deletes_count).to eq(0)
1805
+ expect(company.reload.soft_delete_paranoia_count).to eq(0)
1749
1806
 
1750
1807
  sd.restore
1751
- expect(company.reload.soft_deletes_count).to eq(1)
1808
+ expect(company.reload.soft_delete_paranoia_count).to eq(1)
1752
1809
 
1753
1810
  sd.restore
1754
- expect(company.reload.soft_deletes_count).to eq(1)
1811
+ expect(company.reload.soft_delete_paranoia_count).to eq(1)
1755
1812
  end
1756
1813
  end
1757
1814
 
@@ -0,0 +1,6 @@
1
+ class SoftDeleteDiscard < ActiveRecord::Base
2
+ include Discard::Model if defined?(Discard::Model)
3
+
4
+ belongs_to :company
5
+ counter_culture :company
6
+ end
@@ -0,0 +1,6 @@
1
+ class SoftDeleteParanoia < ActiveRecord::Base
2
+ acts_as_paranoid if respond_to?(:acts_as_paranoid)
3
+
4
+ belongs_to :company
5
+ counter_culture :company
6
+ end
data/spec/schema.rb CHANGED
@@ -24,7 +24,8 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
24
24
  t.integer "review_approvals_count", :default => 0, :null => false
25
25
  t.integer "parent_id"
26
26
  t.integer "children_count", :default => 0, :null => false
27
- t.integer "soft_deletes_count", :default => 0, :null => false
27
+ t.integer "soft_delete_paranoia_count", :default => 0, :null => false
28
+ t.integer "soft_delete_discards_count", :default => 0, :null => false
28
29
  t.datetime "created_at"
29
30
  t.datetime "updated_at"
30
31
  end
@@ -176,11 +177,16 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
176
177
  t.integer "monetary_value", :null => false
177
178
  end
178
179
 
179
- create_table "soft_deletes", :force => true do |t|
180
+ create_table "soft_delete_paranoia", :force => true do |t|
180
181
  t.integer "company_id", :null => false
181
182
  t.timestamp "deleted_at"
182
183
  end
183
184
 
185
+ create_table "soft_delete_discards", :force => true do |t|
186
+ t.integer "company_id", :null => false
187
+ t.timestamp "discarded_at"
188
+ end
189
+
184
190
  #polymorphic
185
191
  create_table "poly_images", :force => true do |t|
186
192
  t.integer "imageable_id", :null => true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_culture
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus von Koeller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-07 00:00:00.000000000 Z
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: after_commit_action
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: discard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: paranoia
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -244,7 +258,8 @@ files:
244
258
  - spec/models/simple_dependent.rb
245
259
  - spec/models/simple_main.rb
246
260
  - spec/models/simple_review.rb
247
- - spec/models/soft_delete.rb
261
+ - spec/models/soft_delete_discard.rb
262
+ - spec/models/soft_delete_paranoia.rb
248
263
  - spec/models/subcateg.rb
249
264
  - spec/models/transaction.rb
250
265
  - spec/models/twitter_review.rb
@@ -1,6 +0,0 @@
1
- class SoftDelete < ActiveRecord::Base
2
- acts_as_paranoid
3
-
4
- belongs_to :company
5
- counter_culture :company
6
- end