counter_culture 1.5.1 → 1.6.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: ffba219862c747eb8b00e3b89d29f6306e32c2c2
4
- data.tar.gz: 80783eda6b30c56e2d5b203c9ffcd2e92a44056c
3
+ metadata.gz: 40d222184acc3b81f1abf7dfd2dbede253eacb7a
4
+ data.tar.gz: bdf081ac5294d1a5a8b693c3639ccf82b0642e0b
5
5
  SHA512:
6
- metadata.gz: 89b88c26c5646a04a828d59eefdd872c6da43e54242e6db119303809331f8a04095c99c699a3636056277e725b9f83c7f55a1d32c93358e5bc92bd8908e8e0cb
7
- data.tar.gz: 1bdba2475e7d09b878b89d5fae4de51f7116aa1326d3ea7eaa3fc3fc9a399683f606b5adf5998ac8f0da7fcc9ae2310299300482d9145c5d2b8cb2cb0349a0e0
6
+ metadata.gz: e81781257cff2487c108d441dfe92ea65bd5f8b42a90c61752d68fe7233d8aa30ebc90c8625c487626b7e461edfb36eaadde30aa539760dcb277a1a2fdd4f0bb
7
+ data.tar.gz: f0c9a1687eff220ccd4d703e901e49f7ad0193207f5fbdcb3a00b5dbb6aea8fd6e288c1f77ad037c9125bee0ce07873ca8f0fb2c5ecddfdc40fc2698532d9d04
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.6.0 (April 24, 2017)
2
+
3
+ Improvements:
4
+ - Keeps counts updated correctly when using the paranoia gem and restoring soft-deleted records
5
+
1
6
  ## 1.5.1 (April 17, 2017)
2
7
 
3
8
  Bugfixes:
data/Gemfile CHANGED
@@ -25,6 +25,9 @@ group :development, :test do
25
25
  gem "rspec", "~> 3.0"
26
26
  gem "awesome_print"
27
27
  gem "timecop"
28
+
29
+ # to test the integration
30
+ gem "paranoia"
28
31
  end
29
32
 
30
33
  group :development do
data/README.md CHANGED
@@ -293,6 +293,22 @@ end
293
293
 
294
294
  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.
295
295
 
296
+ ### Soft-deletes / `paranoia` gem
297
+
298
+ This gem will keep counters correctly updated when using the `paranoia` gem for
299
+ soft-delete support. However, to ensure that counts are incremented after a
300
+ restore you have to make sure that the call to `acts_as_paranoid` comes before
301
+ the call to `counter_culture` in your model:
302
+
303
+ ```ruby
304
+ class SoftDelete < ActiveRecord::Base
305
+ acts_as_paranoid
306
+
307
+ belongs_to :company
308
+ counter_culture :company
309
+ end
310
+ ```
311
+
296
312
  #### Polymorphic associations
297
313
 
298
314
  counter_culture now supports polymorphic associations of one level only.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.1
1
+ 1.6.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.5.1 ruby lib
5
+ # stub: counter_culture 1.6.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "counter_culture".freeze
9
- s.version = "1.5.1"
9
+ s.version = "1.6.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Magnus von Koeller".freeze]
14
- s.date = "2017-04-17"
14
+ s.date = "2017-04-24"
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.".freeze
16
16
  s.email = "magnus@vonkoeller.de".freeze
17
17
  s.extra_rdoc_files = [
@@ -58,6 +58,7 @@ Gem::Specification.new do |s|
58
58
  "spec/models/simple_dependent.rb",
59
59
  "spec/models/simple_main.rb",
60
60
  "spec/models/simple_review.rb",
61
+ "spec/models/soft_delete.rb",
61
62
  "spec/models/subcateg.rb",
62
63
  "spec/models/transaction.rb",
63
64
  "spec/models/twitter_review.rb",
@@ -131,6 +132,7 @@ Gem::Specification.new do |s|
131
132
  s.add_development_dependency(%q<rspec>.freeze, ["~> 3.0"])
132
133
  s.add_development_dependency(%q<awesome_print>.freeze, [">= 0"])
133
134
  s.add_development_dependency(%q<timecop>.freeze, [">= 0"])
135
+ s.add_development_dependency(%q<paranoia>.freeze, [">= 0"])
134
136
  s.add_development_dependency(%q<rdoc>.freeze, ["~> 3.12"])
135
137
  s.add_development_dependency(%q<bundler>.freeze, [">= 1.2.0"])
136
138
  s.add_development_dependency(%q<jeweler>.freeze, ["~> 2.1"])
@@ -143,6 +145,7 @@ Gem::Specification.new do |s|
143
145
  s.add_dependency(%q<rspec>.freeze, ["~> 3.0"])
144
146
  s.add_dependency(%q<awesome_print>.freeze, [">= 0"])
145
147
  s.add_dependency(%q<timecop>.freeze, [">= 0"])
148
+ s.add_dependency(%q<paranoia>.freeze, [">= 0"])
146
149
  s.add_dependency(%q<rdoc>.freeze, ["~> 3.12"])
147
150
  s.add_dependency(%q<bundler>.freeze, [">= 1.2.0"])
148
151
  s.add_dependency(%q<jeweler>.freeze, ["~> 2.1"])
@@ -156,6 +159,7 @@ Gem::Specification.new do |s|
156
159
  s.add_dependency(%q<rspec>.freeze, ["~> 3.0"])
157
160
  s.add_dependency(%q<awesome_print>.freeze, [">= 0"])
158
161
  s.add_dependency(%q<timecop>.freeze, [">= 0"])
162
+ s.add_dependency(%q<paranoia>.freeze, [">= 0"])
159
163
  s.add_dependency(%q<rdoc>.freeze, ["~> 3.12"])
160
164
  s.add_dependency(%q<bundler>.freeze, [">= 1.2.0"])
161
165
  s.add_dependency(%q<jeweler>.freeze, ["~> 2.1"])
@@ -21,6 +21,9 @@ module CounterCulture
21
21
  after_create :_update_counts_after_create
22
22
  after_destroy :_update_counts_after_destroy
23
23
  after_update :_update_counts_after_update
24
+ if respond_to?(:after_restore)
25
+ after_restore :_update_counts_after_create
26
+ end
24
27
 
25
28
  # we keep a list of all counter caches we must maintain
26
29
  @after_commit_counter_cache = []
@@ -52,7 +52,6 @@ module CounterCulture
52
52
  counter.relation_klass(counter.relation)
53
53
  end
54
54
 
55
-
56
55
  class Reconciliation
57
56
  attr_reader :counter, :options, :relation_class
58
57
 
@@ -72,9 +71,6 @@ module CounterCulture
72
71
 
73
72
  scope = relation_class
74
73
 
75
- # respect the deleted_at column if it exists
76
- scope = scope.where("#{model.table_name}.deleted_at IS NULL") if model.column_names.include?('deleted_at')
77
-
78
74
  counter_column_names = column_names || {nil => counter_cache_name}
79
75
 
80
76
  # iterate over all the possible counter cache column names
@@ -91,8 +87,14 @@ module CounterCulture
91
87
  # we need to join together tables until we get back to the table this class itself lives in
92
88
  # conditions must also be applied to the join on which we are counting
93
89
  join_clauses.each_with_index do |join, index|
94
- if index == join_clauses.size - 1 && where
95
- join += " AND (#{model.send(:sanitize_sql_for_conditions, where)})"
90
+ if index == join_clauses.size - 1
91
+ if where
92
+ join += " AND (#{model.send(:sanitize_sql_for_conditions, where)})"
93
+ end
94
+ # respect the deleted_at column if it exists
95
+ if model.column_names.include?('deleted_at')
96
+ join += " AND #{model.table_name}.deleted_at IS NULL"
97
+ end
96
98
  end
97
99
  counts_query = counts_query.joins(join)
98
100
  end
@@ -21,6 +21,7 @@ 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
25
 
25
26
  require 'database_cleaner'
26
27
  DatabaseCleaner.strategy = :deletion
@@ -1586,6 +1587,28 @@ describe "CounterCulture" do
1586
1587
  end
1587
1588
  end
1588
1589
 
1590
+ describe "when using acts_as_paranoia" do
1591
+ it "works" do
1592
+ company = Company.create!
1593
+ expect(company.soft_deletes_count).to eq(0)
1594
+ sd = SoftDelete.create!(company_id: company.id)
1595
+ expect(company.reload.soft_deletes_count).to eq(1)
1596
+
1597
+ sd.destroy
1598
+ sd.reload
1599
+ expect(sd.deleted_at).to be_truthy
1600
+ expect(company.reload.soft_deletes_count).to eq(0)
1601
+
1602
+ company.update_attributes(soft_deletes_count: 100)
1603
+ expect(company.reload.soft_deletes_count).to eq(100)
1604
+ SoftDelete.counter_culture_fix_counts
1605
+ expect(company.reload.soft_deletes_count).to eq(0)
1606
+
1607
+ sd.restore
1608
+ expect(company.reload.soft_deletes_count).to eq(1)
1609
+ end
1610
+ end
1611
+
1589
1612
  describe "with polymorphic_associations" do
1590
1613
  before(:all) do
1591
1614
  require 'models/poly_image'
@@ -0,0 +1,6 @@
1
+ class SoftDelete < ActiveRecord::Base
2
+ acts_as_paranoid
3
+
4
+ belongs_to :company
5
+ counter_culture :company
6
+ end
data/spec/schema.rb CHANGED
@@ -24,6 +24,7 @@ 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
28
  t.datetime "created_at"
28
29
  t.datetime "updated_at"
29
30
  end
@@ -175,18 +176,25 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
175
176
  t.integer "monetary_value", :null => false
176
177
  end
177
178
 
179
+ create_table "soft_deletes", :force => true do |t|
180
+ t.integer "company_id", :null => false
181
+ t.timestamp "deleted_at"
182
+ end
183
+
178
184
  #polymorphic
179
185
  create_table "poly_images", :force => true do |t|
180
186
  t.integer "imageable_id", :null => true
181
187
  t.string "imageable_type", :null => true
182
188
  t.string "url"
183
189
  end
190
+
184
191
  create_table "poly_employees", :force => true do |t|
185
192
  t.string "name"
186
193
  t.integer "poly_images_count", :default => 0, :null => false
187
194
  t.integer "poly_images_count_dup", :default => 0, :null => false
188
195
  t.integer "special_poly_images_count", :default => 0, :null => false
189
196
  end
197
+
190
198
  create_table "poly_products", :primary_key => 'pp_pk_id', :force => true do |t|
191
199
  t.string "brand_name"
192
200
  t.integer "poly_images_count", :default => 0, :null => false
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.5.1
4
+ version: 1.6.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: 2017-04-17 00:00:00.000000000 Z
11
+ date: 2017-04-24 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: paranoia
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: rdoc
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -214,6 +228,7 @@ files:
214
228
  - spec/models/simple_dependent.rb
215
229
  - spec/models/simple_main.rb
216
230
  - spec/models/simple_review.rb
231
+ - spec/models/soft_delete.rb
217
232
  - spec/models/subcateg.rb
218
233
  - spec/models/transaction.rb
219
234
  - spec/models/twitter_review.rb