awesome_counter_cache 0.0.2 → 0.0.3

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
  SHA256:
3
- metadata.gz: da9f3b10f0b3d3f91d23585c3cbb6234b585987d7d4ba4704026de6871830e66
4
- data.tar.gz: '09a7a1388a4d93709e10712d8dd3d8a9c7ad0af76a456d4b2b2b674a1f697d6b'
3
+ metadata.gz: 53d101e35ce62ad8828e71e6312d4ccfbfe4b0c656138e9b62106691e293ea87
4
+ data.tar.gz: eb20dba29be9f77e111a818f6eb6b05d0aab5ece37ddd877b9f0f57543c37ecd
5
5
  SHA512:
6
- metadata.gz: 635c8b6ba277428198ab978addf81aa9723b713d26f4dcf6a6a26bdc84ef84a444c7450be3cb4b1419afbb699be7f60fbaafcca92a7c8bb328f5d794cfcd274b
7
- data.tar.gz: ccd61848475acd36066de42204772e0ea8d0778e4a4ebda7b7dc26f3fe1de0f13cdc4f91d083e63a770a2df61328b2ddfe15cc15049a04e805f20fa1d703ba8e
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[:relation_foreign_key] = reflections.fetch(relation_name.to_s).foreign_key
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
- attr_accessor :column_name, :delta_magnitude, :id, :relation_foreign_key, :relation_name
2
+ attr_reader :column_name, :delta_magnitude, :id, :model_class, :relation_name
3
3
 
4
- def initialize(column_name:, delta_magnitude:, id:, relation_foreign_key:, relation_name:)
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
- @relation_foreign_key = relation_foreign_key
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(counter_cache.relation_name, @awesome_counter_cache_data.fetch(id), counter_cache)
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(counter_cache.relation_name, @awesome_counter_cache_data.fetch(id), counter_cache)
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(counter_cache.relation_name, @awesome_counter_cache_data.fetch(id), counter_cache)
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(relation_name, state, counter_cache)
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(relation_name, state, counter_cache)
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(relation_name, state, counter_cache) # rubocop:disable Metrics/AbcSize
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 = self.class.reflections.fetch(relation_name.to_s).klass
84
- primary_key_value = read_attribute(counter_cache.relation_foreign_key)
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
@@ -1,3 +1,3 @@
1
1
  module AwesomeCounterCache
2
- VERSION = "0.0.2".freeze
2
+ VERSION = "0.0.3".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awesome_counter_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj