counter-cache 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65f431cac9a84b8abe458919608c07c66501c68e
4
- data.tar.gz: c0582fc1f4d829fc50162ce50746b33fdf06f2c8
3
+ metadata.gz: 5cac236244f5fa2812dc3ba95405240ff016fce4
4
+ data.tar.gz: e2281ca37f31cc531cb2bba1aa1f9ec3438ca5d1
5
5
  SHA512:
6
- metadata.gz: db21a9548ac98191ddf559ff962c78f221656998efbe5f1a0c2af14e153e7256647fa3f54bc4f00faa8b3d34b9feabd9f94d6f82410805b241eb3384593ba1a9
7
- data.tar.gz: 8b4abafb442cbdedef2ddffae83ebc7d71a418dedbef791ad3f963abc7c039b45e44fa8d4d2d992514da60091e8d555932439555c319f33923ac0fc12271f5a4
6
+ metadata.gz: 38f9a282a897f6790147ea80dd9198db5bf27fb39aeec1339916480297a02182d1e11066304ef674339103070f0d6bc874f0d3ef0f3f1d19bcff59389e339941
7
+ data.tar.gz: afc080f77508a700108aa7ea37687a5c509633efd2048c693b60a3fa2894f52e5b6b2ebd358e0ebd7578d6006b5ac7ec81474ad13d45c724824600ce3290c422
data/README.md CHANGED
@@ -98,6 +98,19 @@ Counter caches are configured on the models from the perspective of the child mo
98
98
 
99
99
  #### Basic Counter with recalculation:
100
100
 
101
+ ```ruby
102
+ class Post
103
+ include Counter::Cache
104
+
105
+ counter_cache_on column: :posts_count, # users.posts_count
106
+ relation: :user,
107
+ relation_class_name: "User",
108
+ method: :calculate_posts_count # This is a method on the user.
109
+ end
110
+ ```
111
+
112
+ #### To allow a timestamp to be updated:
113
+
101
114
  ```ruby
102
115
  class Post
103
116
  include Counter::Cache
@@ -106,6 +119,7 @@ class Post
106
119
  relation: :user,
107
120
  relation_class_name: "User",
108
121
  method: :calculate_posts_count, # This is a method on the user.
122
+ touch_column: :posts_updated_at
109
123
  end
110
124
  ```
111
125
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- counter-cache (0.0.2)
4
+ counter-cache (0.1.0)
5
5
  activerecord (>= 3.0)
6
6
 
7
7
  GEM
@@ -90,3 +90,6 @@ DEPENDENCIES
90
90
  rake
91
91
  rspec (>= 3.0)
92
92
  sqlite3
93
+
94
+ BUNDLED WITH
95
+ 1.10.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- counter-cache (0.0.2)
4
+ counter-cache (0.1.0)
5
5
  activerecord (>= 3.0)
6
6
 
7
7
  GEM
@@ -96,3 +96,6 @@ DEPENDENCIES
96
96
  rake
97
97
  rspec (>= 3.0)
98
98
  sqlite3
99
+
100
+ BUNDLED WITH
101
+ 1.10.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- counter-cache (0.0.2)
4
+ counter-cache (0.1.0)
5
5
  activerecord (>= 3.0)
6
6
 
7
7
  GEM
@@ -95,3 +95,6 @@ DEPENDENCIES
95
95
  rake
96
96
  rspec (>= 3.0)
97
97
  sqlite3
98
+
99
+ BUNDLED WITH
100
+ 1.10.6
@@ -11,14 +11,16 @@ module Counter
11
11
  private
12
12
 
13
13
  def create_and_enqueue(delay, cached)
14
+ parameters = { relation_class_name: relation_class,
15
+ relation_id: relation_id,
16
+ column: options.column,
17
+ method: options.method,
18
+ cache: cached,
19
+ counter: counter_class_name }
20
+ parameters[:touch_column] = options.touch_column unless options.touch_column.nil?
14
21
  options.worker_adapter.enqueue(delay,
15
22
  source_object_class_name,
16
- { relation_class_name: relation_class,
17
- relation_id: relation_id,
18
- column: options.column,
19
- method: options.method,
20
- cache: cached,
21
- counter: counter_class_name })
23
+ parameters)
22
24
  end
23
25
  end
24
26
  end
@@ -18,6 +18,7 @@ module Counter
18
18
 
19
19
  def save_new_value!(value)
20
20
  relation_object.send("#{options.column}=", value)
21
+ relation_object.send("#{options.touch_column}=", DateTime.now) if options.touch_column
21
22
  relation_object.save!(validate: false)
22
23
  end
23
24
 
@@ -13,6 +13,10 @@ module Counter
13
13
  options[:column]
14
14
  end
15
15
 
16
+ def touch_column
17
+ options[:touch_column]
18
+ end
19
+
16
20
  def relation
17
21
  options[:relation]
18
22
  end
@@ -1,5 +1,5 @@
1
1
  module Counter
2
2
  module Cache
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -2,52 +2,106 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Counter::Cache::Counters::BufferCounter::Enqueuer do
4
4
  let(:worker_adapter) { double }
5
- let(:options) { double(worker_adapter: worker_adapter,
6
- wait: 10,
7
- column: "boo",
8
- method: "calculate_boo",
9
- cached?: true,
10
- recalculation?: false,
11
- recalculation_delay: 20) }
12
5
 
13
6
  let(:source_object_class_name) { "BooUser" }
14
7
  let(:relation_id) { 1 }
15
8
  let(:relation_type) { "Boo" }
16
9
 
17
- let(:enqueuer) { Counter::Cache::Counters::BufferCounter::Enqueuer.new(options, source_object_class_name, relation_id, relation_type, "SuperCounter") }
18
-
19
10
  describe '#enqueue' do
20
- before do
21
- expect(worker_adapter).to receive(:enqueue).with(10,
22
- "BooUser",
23
- { relation_class_name: "Boo",
24
- relation_id: 1,
25
- column: "boo",
26
- method: "calculate_boo",
27
- cache: true,
28
- counter: "SuperCounter" })
29
- end
30
-
31
- describe 'when recalculation is true' do
11
+ context "without touch column" do
32
12
  before do
33
- expect(options).to receive(:recalculation?).and_return(true)
34
- end
13
+ @options = double(worker_adapter: worker_adapter,
14
+ wait: 10,
15
+ column: "boo",
16
+ method: "calculate_boo",
17
+ cached?: true,
18
+ recalculation?: false,
19
+ recalculation_delay: 20)
20
+
21
+ @enqueuer = Counter::Cache::Counters::BufferCounter::Enqueuer.new(@options, source_object_class_name, relation_id, relation_type, "SuperCounter")
35
22
 
36
- it "enqueues two jobs" do
37
- expect(worker_adapter).to receive(:enqueue).with(20,
23
+ expect(worker_adapter).to receive(:enqueue).with(10,
38
24
  "BooUser",
39
25
  { relation_class_name: "Boo",
40
26
  relation_id: 1,
41
27
  column: "boo",
42
28
  method: "calculate_boo",
43
- cache: false,
29
+ cache: true,
44
30
  counter: "SuperCounter" })
45
- enqueuer.enqueue!(double)
31
+ end
32
+
33
+ describe 'when recalculation is true' do
34
+ before do
35
+ expect(@options).to receive(:recalculation?).and_return(true)
36
+ end
37
+
38
+ it "enqueues two jobs" do
39
+ expect(@options).to receive(:touch_column).twice
40
+ expect(worker_adapter).to receive(:enqueue).with(20,
41
+ "BooUser",
42
+ { relation_class_name: "Boo",
43
+ relation_id: 1,
44
+ column: "boo",
45
+ method: "calculate_boo",
46
+ cache: false,
47
+ counter: "SuperCounter" })
48
+ @enqueuer.enqueue!(double)
49
+ end
50
+ end
51
+
52
+ it 'enqueues one job' do
53
+ expect(@options).to receive(:touch_column)
54
+ @enqueuer.enqueue!(double)
46
55
  end
47
56
  end
48
57
 
49
- it 'enqueues one job' do
50
- enqueuer.enqueue!(double)
58
+ context "with touch column" do
59
+ before(:each) do
60
+ @options = double(worker_adapter: worker_adapter,
61
+ wait: 10,
62
+ column: "boo",
63
+ touch_column: "boo_updated_at",
64
+ method: "calculate_boo",
65
+ cached?: true,
66
+ recalculation?: false,
67
+ recalculation_delay: 20)
68
+ @enqueuer = Counter::Cache::Counters::BufferCounter::Enqueuer.new(@options, source_object_class_name, relation_id, relation_type, "SuperCounter")
69
+
70
+ expect(worker_adapter).to receive(:enqueue).with(10,
71
+ "BooUser",
72
+ { relation_class_name: "Boo",
73
+ relation_id: 1,
74
+ column: "boo",
75
+ touch_column: "boo_updated_at",
76
+ method: "calculate_boo",
77
+ cache: true,
78
+ counter: "SuperCounter" })
79
+ end
80
+
81
+ describe 'when recalculation is true' do
82
+ before do
83
+ expect(@options).to receive(:recalculation?).and_return(true)
84
+ end
85
+
86
+ it "enqueues two jobs" do
87
+ expect(@options).to receive(:touch_column).exactly(4).times
88
+ expect(worker_adapter).to receive(:enqueue).with(20,
89
+ "BooUser",
90
+ { relation_class_name: "Boo",
91
+ relation_id: 1,
92
+ column: "boo",
93
+ touch_column: "boo_updated_at",
94
+ method: "calculate_boo",
95
+ cache: false,
96
+ counter: "SuperCounter" })
97
+ @enqueuer.enqueue!(double)
98
+ end
99
+ end
100
+
101
+ it 'enqueues one job' do
102
+ expect(@options).to receive(:touch_column)
103
+ @enqueuer.enqueue!(double)
104
+ end
51
105
  end
52
106
  end
53
107
  end
@@ -4,7 +4,7 @@ RSpec.describe Counter::Cache::Counters::BufferCounter::Saver do
4
4
  class Boo
5
5
  end
6
6
 
7
- let(:relation_object) { double(boo_count: 2) }
7
+ let(:relation_object) { double(boo_count: 2, boo_updated_at: DateTime.now) }
8
8
  let(:options) { double(relation_class_name: "Boo", relation_id: 1, column: "boo_count", method: nil, source_object_class_name: Boo) }
9
9
  let(:saver) { Counter::Cache::Counters::BufferCounter::Saver.new(options) }
10
10
 
@@ -21,6 +21,7 @@ RSpec.describe Counter::Cache::Counters::BufferCounter::Saver do
21
21
 
22
22
  before do
23
23
  allow(options).to receive(:cached?).and_return(true)
24
+ allow(options).to receive(:touch_column).and_return(nil)
24
25
  expect(relation_object).to receive(:boo_count=).with(4)
25
26
  expect(relation_object).to receive(:save!)
26
27
  expect(counting_store).to receive(:del)
@@ -41,15 +42,29 @@ RSpec.describe Counter::Cache::Counters::BufferCounter::Saver do
41
42
  expect(counting_store).to receive(:del)
42
43
  end
43
44
 
44
- it 'saves the value' do
45
- expect(relation_object).to receive(:boo_count=).with(4)
46
- expect(relation_object).to receive(:save!)
47
- saver.save!
45
+ context "with touch column" do
46
+ it 'saves the value' do
47
+ allow(options).to receive(:touch_column).and_return(:boo_updated_at)
48
+ expect(relation_object).to receive(:boo_count=).with(4)
49
+ expect(relation_object).to receive(:boo_updated_at=).with(DateTime.now)
50
+ expect(relation_object).to receive(:save!)
51
+ saver.save!
52
+ end
53
+ end
54
+
55
+ context "without touch column" do
56
+ it 'saves the value' do
57
+ allow(options).to receive(:touch_column).and_return(nil)
58
+ expect(relation_object).to receive(:boo_count=).with(4)
59
+ expect(relation_object).to receive(:save!)
60
+ saver.save!
61
+ end
48
62
  end
49
63
  end
50
64
 
51
65
  describe 'when cached? is false' do
52
66
  before do
67
+ allow(options).to receive(:touch_column).and_return(nil)
53
68
  allow(options).to receive(:cached?).and_return(false)
54
69
  end
55
70
 
@@ -34,6 +34,13 @@ RSpec.describe Counter::Cache::OptionsParser do
34
34
  end
35
35
  end
36
36
 
37
+ describe "#touch_column" do
38
+ let(:options) { { touch_column: "touch_column_name" } }
39
+ it "returns option if set" do
40
+ expect(parser.touch_column).to eq("touch_column_name")
41
+ end
42
+ end
43
+
37
44
  describe "#method" do
38
45
  let(:options) { { method: "method" } }
39
46
  it "returns option if set" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Henry & Matt Camuto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-16 00:00:00.000000000 Z
11
+ date: 2015-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -207,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
207
  version: '0'
208
208
  requirements: []
209
209
  rubyforge_project:
210
- rubygems_version: 2.2.2
210
+ rubygems_version: 2.2.3
211
211
  signing_key:
212
212
  specification_version: 4
213
213
  summary: Counting is hard.