counter_one 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e507eac4fc8bc71388c8ee667c96a0d0595ef15778702af8290e889f507abeb0
4
+ data.tar.gz: 3e72f346a0b0fa66ff5c9e18b2717275be8435b80775a8c033c1544af085b56c
5
+ SHA512:
6
+ metadata.gz: e0b73fea1fadcbc87f516f16046b8f01e6739182850c2c3fb4efad0edddd5d94125d4722d6ded491cdeeb0d9780c740fb7cf1f92eef4f2c28137613043e80e82
7
+ data.tar.gz: 05e948b8b604653c5bd2949913163fce58e2e5d69f65539b3d02342ef3844123edd66b947e9bc4f90b847bb10e69e7208eab236277091f2ff0741d150fb8f796
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-19
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in counter_one.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ counter_one (0.1.0)
5
+ activerecord (>= 4.2)
6
+ activesupport (>= 4.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (7.0.3.1)
12
+ activesupport (= 7.0.3.1)
13
+ activerecord (7.0.3.1)
14
+ activemodel (= 7.0.3.1)
15
+ activesupport (= 7.0.3.1)
16
+ activesupport (7.0.3.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ concurrent-ruby (1.1.10)
22
+ diff-lcs (1.5.0)
23
+ i18n (1.12.0)
24
+ concurrent-ruby (~> 1.0)
25
+ minitest (5.16.2)
26
+ rake (13.0.6)
27
+ rspec (3.11.0)
28
+ rspec-core (~> 3.11.0)
29
+ rspec-expectations (~> 3.11.0)
30
+ rspec-mocks (~> 3.11.0)
31
+ rspec-core (3.11.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-expectations (3.11.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-mocks (3.11.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.11.0)
39
+ rspec-support (3.11.0)
40
+ sqlite3 (1.4.4)
41
+ tzinfo (2.0.5)
42
+ concurrent-ruby (~> 1.0)
43
+
44
+ PLATFORMS
45
+ x86_64-darwin-20
46
+ x86_64-linux
47
+
48
+ DEPENDENCIES
49
+ counter_one!
50
+ rake (~> 13.0)
51
+ rspec (~> 3.0)
52
+ sqlite3
53
+
54
+ BUNDLED WITH
55
+ 2.2.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Nikolay Seskin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # CounterOne
2
+
3
+ Improved counter cache for Rails app with support various relationships and conditions.
4
+
5
+ ## Features
6
+ - Updates the counter cache for create, destroy, and update actions, as well as any single action
7
+ - Counter caches for multi levels and has_one/has_many through relations
8
+ - Conditions for counter caches
9
+ - Recalculating counter caches with conditions
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'counter_one'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install counter_one
26
+
27
+ ## Usage
28
+
29
+ ### Counter cache for simple relation
30
+
31
+ ```ruby
32
+ class User < ActiveRecord::Base
33
+ has_many :products
34
+ end
35
+
36
+ class Product < ActiveRecord::Base
37
+ belongs_to :user
38
+ counter_one :user
39
+ end
40
+ ```
41
+
42
+ It will be keep up to date products_count for users when product is created or destroyed.
43
+
44
+ ### Counter cache for multi levels relation
45
+
46
+ ```ruby
47
+ class User < ActiveRecord::Base
48
+ has_many :products
49
+ end
50
+
51
+ class Product < ActiveRecord::Base
52
+ belongs_to :user
53
+ end
54
+
55
+ class Comment < ActiveRecord::Base
56
+ belongs_to :product
57
+ counter_one [:product, :user]
58
+ end
59
+ ```
60
+
61
+ It will be keep up to date comments_count for users when comment is created or destroyed.
62
+
63
+ ### Counter cache for has_one through relation
64
+
65
+ ```ruby
66
+ class User < ActiveRecord::Base
67
+ has_many :products
68
+ end
69
+
70
+ class Product < ActiveRecord::Base
71
+ belongs_to :user
72
+ end
73
+
74
+ class Comment < ActiveRecord::Base
75
+ belongs_to :product
76
+ has_one :user, through: :product
77
+
78
+ counter_one :user
79
+ end
80
+ ```
81
+
82
+ ### Counter cache with custom counter field
83
+
84
+ ```ruby
85
+ class User < ActiveRecord::Base
86
+ has_many :products
87
+ end
88
+
89
+ class Product < ActiveRecord::Base
90
+ belongs_to :user
91
+ counter_one :user, column: :custom_counter
92
+ end
93
+ ```
94
+
95
+ ### Counter cache with conditions
96
+
97
+ ```ruby
98
+ class User < ActiveRecord::Base
99
+ has_many :products
100
+ end
101
+
102
+ class Product < ActiveRecord::Base
103
+ belongs_to :user
104
+ counter_one :user, column: :active_products, only: ->(product) { product.active? }
105
+ end
106
+ ```
107
+
108
+ ### Counter cache only for deleted records
109
+
110
+ ```ruby
111
+ class User < ActiveRecord::Base
112
+ has_many :products
113
+ end
114
+
115
+ class Product < ActiveRecord::Base
116
+ belongs_to :user
117
+ counter_one :user, column: :deleted_products, on: [:destroy]
118
+ end
119
+ ```
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,74 @@
1
+ module CounterOne
2
+ class Counter
3
+
4
+ attr_reader :model, :relation, :options
5
+
6
+ def initialize(model, relation, options)
7
+ @model = model
8
+ @relation = [relation].flatten
9
+ @options = options
10
+ end
11
+
12
+ def update_counter(record, operator, changed_record = nil)
13
+ counter_record = record
14
+ klass = model
15
+
16
+ relation.each do |rel|
17
+ unless klass.reflect_on_association(rel)
18
+ raise "Can't find relation #{rel} for #{klass.to_s}"
19
+ end
20
+
21
+ counter_record = counter_record.send(rel)
22
+ klass = counter_record.class
23
+
24
+ return unless counter_record
25
+ end
26
+
27
+ if options[:only]
28
+ unless options[:only].is_a?(Proc)
29
+ raise ArgumentError.new(":only must be a Proc with conditions")
30
+ end
31
+
32
+ return unless options[:only].call(record)
33
+ end
34
+
35
+ if changed_record
36
+ return if options[:only] ? condition_not_changed?(record, changed_record) : relation_id_not_changed?(record)
37
+ end
38
+
39
+ if counter_record
40
+ column = options[:column] || "#{record.class.to_s.tableize}_count"
41
+
42
+ if counter_record.is_a?(ActiveRecord::Associations::CollectionProxy)
43
+ counter_record.each { |r| r.send(operator, column) }
44
+ else
45
+ counter_record.send(operator, column)
46
+ end
47
+ end
48
+ end
49
+
50
+ def recalculate_scope
51
+ options[:recalculate_scope]
52
+ end
53
+
54
+ def relation_chain
55
+ klass = model
56
+
57
+ relation.each_with_object([]) { |rel, chain|
58
+ chain << klass.reflect_on_association(rel).chain.map(&:name).reverse
59
+ klass = rel.to_s.classify.constantize
60
+ }.flatten
61
+ end
62
+
63
+ private
64
+
65
+ def condition_not_changed?(record, changed_record)
66
+ options[:only].call(record) == options[:only].call(changed_record)
67
+ end
68
+
69
+ def relation_id_not_changed?(record)
70
+ relation_fk = model.reflect_on_association(relation_chain.first).foreign_key
71
+ record.saved_changes[relation_fk].blank?
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,79 @@
1
+ module CounterOne
2
+ module Extensions
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ attr_reader :counter_one_cache
8
+
9
+ def counter_one(relation, options = {})
10
+ @counter_one_cache ||= []
11
+
12
+ @counter_one_cache << Counter.new(self, relation, options)
13
+
14
+ on = [options[:on]].flatten.compact
15
+
16
+ after_create :increment_counters if on.empty? || on.include?(:create)
17
+ after_destroy :decrement_counters if on.empty? || on.include?(:destroy)
18
+ after_update :update_counters if on.empty? || on.include?(:update)
19
+ end
20
+
21
+ def counter_one_recalculate(relation = nil)
22
+ counters = relation ? counter_one_cache.select { |counter| counter.relation == [relation].flatten } : counter_one_cache
23
+ counters.each { |counter| recalculate_counters(counter) }
24
+ end
25
+
26
+ private
27
+
28
+ def recalculate_counters(counter)
29
+ counter_relation_chain = counter.relation_chain
30
+
31
+ joins_chain = counter_relation_chain.reverse.inject() { |value, key| { key => value } }
32
+ counter_relation_table_name = counter_relation_chain.last.to_s.classify.constantize.table_name
33
+
34
+ result = self
35
+ .joins(joins_chain)
36
+ .then { |scope| counter.recalculate_scope ? scope.merge(counter.recalculate_scope) : scope }
37
+ .group("#{counter_relation_table_name}.id")
38
+ .count
39
+
40
+ result.each do |key, value|
41
+ column = counter.options[:column] || "#{self.to_s.tableize}_count"
42
+ counter.relation_chain.last.to_s.classify.constantize.find(key).update(column => value)
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ private
49
+
50
+ def increment_counters
51
+ self.class.counter_one_cache.each do |counter|
52
+ counter.update_counter(self, :increment!)
53
+ end
54
+ end
55
+
56
+ def decrement_counters
57
+ self.class.counter_one_cache.each do |counter|
58
+ counter.update_counter(self, :decrement!)
59
+ end
60
+ end
61
+
62
+ def update_counters
63
+ self.class.counter_one_cache.each do |counter|
64
+ counter.update_counter(record_before_save, :decrement!, self)
65
+ counter.update_counter(self, :increment!, record_before_save)
66
+ end
67
+ end
68
+
69
+ def record_before_save
70
+ record = self.dup
71
+
72
+ self.saved_changes.each do |key, value|
73
+ record.assign_attributes(key => value.first)
74
+ end
75
+
76
+ record
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CounterOne
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/concern'
3
+ require 'active_support/lazy_load_hooks'
4
+
5
+ require_relative "counter_one/version"
6
+ require 'counter_one/extensions'
7
+ require 'counter_one/counter'
8
+
9
+ module CounterOne
10
+ class Error < StandardError; end
11
+
12
+ # def self.config
13
+ # yield(self) if block_given?
14
+ # self
15
+ # end
16
+ end
17
+
18
+ # extend ActiveRecord with our own code here
19
+ ActiveSupport.on_load(:active_record) do
20
+ include CounterOne::Extensions
21
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: counter_one
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nikolay Seskin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: CounterOne provides improved counter cache for Rails app with support
56
+ various relationships and conditions.
57
+ email:
58
+ - nseskin@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rspec"
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - lib/counter_one.rb
71
+ - lib/counter_one/counter.rb
72
+ - lib/counter_one/extensions.rb
73
+ - lib/counter_one/version.rb
74
+ homepage: https://github.com/finist/counter_one
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/finist/counter_one
79
+ source_code_uri: https://github.com/finist/counter_one
80
+ changelog_uri: https://github.com/finist/counter_one/CHANGELOG.md
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.4.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.1.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Improved counter cache for Rails app.
100
+ test_files: []