awesome_counter_cache 0.0.2 → 0.0.3
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
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53d101e35ce62ad8828e71e6312d4ccfbfe4b0c656138e9b62106691e293ea87
|
4
|
+
data.tar.gz: eb20dba29be9f77e111a818f6eb6b05d0aab5ece37ddd877b9f0f57543c37ecd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b225f04b9680ec55509a65a80cc1b9cd2449ad309ab78f67a4a80fe6b6cdd8d1da8620ceb3211bd3f0466f2e0d74af56b1c1f80d66085c626ba83e0f47bd9ed3
|
7
|
+
data.tar.gz: e476fef6661308e2d3fba792904fef6bc86dfbce9ee9366538e47a0db3cb219e1d60fe5ed966882e04caa1ec911702e2c623e430a2b814947b7f6f711d91111a
|
@@ -3,7 +3,7 @@ module AwesomeCounterCache::ClassMethods
|
|
3
3
|
id = awesome_counter_caches.length
|
4
4
|
args[:column_name] ||= "#{model_name.plural}_count"
|
5
5
|
args[:delta_magnitude] ||= proc { 1 }
|
6
|
-
args[:
|
6
|
+
args[:model_class] = self
|
7
7
|
args[:relation_name] = relation_name
|
8
8
|
args[:id] = id
|
9
9
|
|
@@ -1,11 +1,22 @@
|
|
1
1
|
class AwesomeCounterCache::CounterCache
|
2
|
-
|
2
|
+
attr_reader :column_name, :delta_magnitude, :id, :model_class, :relation_name
|
3
3
|
|
4
|
-
def initialize(column_name:, delta_magnitude:, id:,
|
4
|
+
def initialize(column_name:, delta_magnitude:, id:, model_class:, relation_name:)
|
5
5
|
@column_name = column_name
|
6
6
|
@delta_magnitude = delta_magnitude
|
7
7
|
@id = id
|
8
|
-
@
|
8
|
+
@model_class = model_class
|
9
9
|
@relation_name = relation_name
|
10
|
+
|
11
|
+
raise "Invalid model class: #{model_class&.name}" if model_class.blank?
|
12
|
+
raise "Invalid relation name: #{relation_name}" if relation_name.blank?
|
13
|
+
end
|
14
|
+
|
15
|
+
def reflection
|
16
|
+
model_class.reflections.fetch(relation_name.to_s)
|
17
|
+
end
|
18
|
+
|
19
|
+
def relation_foreign_key
|
20
|
+
reflection.foreign_key
|
10
21
|
end
|
11
22
|
end
|
@@ -3,7 +3,7 @@ module AwesomeCounterCache::InstanceMethods
|
|
3
3
|
id = counter_cache.id
|
4
4
|
state = @awesome_counter_cache_data.fetch(id)
|
5
5
|
state.delta_current = counter_cache.delta_magnitude.call(model)
|
6
|
-
model.create_awesome_counter_cache_for(
|
6
|
+
model.create_awesome_counter_cache_for(@awesome_counter_cache_data.fetch(id), counter_cache)
|
7
7
|
state.delta_original = state.delta_current
|
8
8
|
state.delta_current = nil
|
9
9
|
end
|
@@ -12,7 +12,7 @@ module AwesomeCounterCache::InstanceMethods
|
|
12
12
|
id = counter_cache.id
|
13
13
|
state = @awesome_counter_cache_data.fetch(id)
|
14
14
|
state.delta_current = counter_cache.delta_magnitude.call(model)
|
15
|
-
model.destroy_awesome_counter_cache_for(
|
15
|
+
model.destroy_awesome_counter_cache_for(@awesome_counter_cache_data.fetch(id), counter_cache)
|
16
16
|
state.delta_original = state.delta_current
|
17
17
|
state.delta_current = nil
|
18
18
|
end
|
@@ -21,7 +21,7 @@ module AwesomeCounterCache::InstanceMethods
|
|
21
21
|
id = counter_cache.id
|
22
22
|
state = @awesome_counter_cache_data.fetch(id)
|
23
23
|
state.delta_current = counter_cache.delta_magnitude.call(model)
|
24
|
-
model.update_awesome_counter_cache_for(
|
24
|
+
model.update_awesome_counter_cache_for(@awesome_counter_cache_data.fetch(id), counter_cache)
|
25
25
|
state.delta_original = state.delta_current
|
26
26
|
state.delta_current = nil
|
27
27
|
end
|
@@ -41,8 +41,8 @@ module AwesomeCounterCache::InstanceMethods
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
def create_awesome_counter_cache_for(
|
45
|
-
relation_model = __send__(relation_name)
|
44
|
+
def create_awesome_counter_cache_for(state, counter_cache)
|
45
|
+
relation_model = __send__(counter_cache.relation_name)
|
46
46
|
return if relation_model.blank?
|
47
47
|
|
48
48
|
addition = state.delta_current
|
@@ -66,8 +66,8 @@ module AwesomeCounterCache::InstanceMethods
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
def destroy_awesome_counter_cache_for(
|
70
|
-
relation_model = __send__(relation_name)
|
69
|
+
def destroy_awesome_counter_cache_for(state, counter_cache)
|
70
|
+
relation_model = __send__(counter_cache.relation_name)
|
71
71
|
return if relation_model.blank?
|
72
72
|
|
73
73
|
addition = -state.delta_original
|
@@ -78,10 +78,18 @@ module AwesomeCounterCache::InstanceMethods
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
-
def update_awesome_counter_cache_for(
|
81
|
+
def update_awesome_counter_cache_for(state, counter_cache) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
|
82
82
|
addition = state.delta_current - state.delta_original
|
83
|
-
relation_model_class =
|
84
|
-
|
83
|
+
relation_model_class = counter_cache.reflection.klass
|
84
|
+
|
85
|
+
if counter_cache.reflection.options[:through]
|
86
|
+
# Read ID through relationship because there isn't a column to read from
|
87
|
+
primary_key_value = __send__(counter_cache.relation_name)&.id
|
88
|
+
else
|
89
|
+
# Read ID directly from column because its faster (no db-lookup)
|
90
|
+
# Use __send__ because read_attribute fails silently if column doesn't exist
|
91
|
+
primary_key_value = __send__(counter_cache.relation_foreign_key)
|
92
|
+
end
|
85
93
|
|
86
94
|
if saved_changes.key?(counter_cache.relation_foreign_key)
|
87
95
|
# Record change from one to another - reduce and increase based on previously recorded values
|
@@ -96,7 +104,7 @@ module AwesomeCounterCache::InstanceMethods
|
|
96
104
|
# Increase the count of the new record
|
97
105
|
relation_model_class.update_counters(primary_key_value, counter_cache.column_name => state.delta_current) # rubocop:disable Rails/SkipsModelValidations
|
98
106
|
end
|
99
|
-
elsif !addition.zero?
|
107
|
+
elsif primary_key_value && !addition.zero?
|
100
108
|
# Add to the current record
|
101
109
|
relation_model_class.update_counters(primary_key_value, counter_cache.column_name => addition) # rubocop:disable Rails/SkipsModelValidations
|
102
110
|
end
|