activerecord-precounter 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
+ SHA1:
3
+ metadata.gz: e486607a409dc25b776267ae02f8bd0ef8f801f5
4
+ data.tar.gz: 38667cea1241dc1820e77289185a6102b031af3e
5
+ SHA512:
6
+ metadata.gz: 98e0d0d94a90f233250936bb69ce3e5f9407f30687097c10afff7168710a05bb249de0611ca916700ac04400690f2c5187b694bd14b476a11d13a776a4530d03
7
+ data.tar.gz: 8c9bdebda2f932c1f7ed4b699020b4528d918a24b5335a7954a4a24bec269ababa1d6cb24484aeff4b0a6ede03f7ea17a50736b9c9b7742cf3de6c8d62f74cc6
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,33 @@
1
+ script: ci/travis.rb
2
+ language: ruby
3
+ sudo: false
4
+ cache: bundler
5
+ branches:
6
+ only:
7
+ - master
8
+ matrix:
9
+ include:
10
+ - rvm: 2.1.10
11
+ env: TASK=test ARCONN=mysql2
12
+ gemfile: ci/Gemfile.activerecord-4.2.x
13
+ - rvm: 2.2.7
14
+ env: TASK=test ARCONN=mysql2
15
+ gemfile: ci/Gemfile.activerecord-5.1.x
16
+ - rvm: 2.3.4
17
+ env: TASK=test ARCONN=mysql2
18
+ gemfile: ci/Gemfile.activerecord-5.1.x
19
+ - rvm: 2.4.1
20
+ env: TASK=test ARCONN=mysql2
21
+ gemfile: ci/Gemfile.activerecord-4.2.x
22
+ - rvm: 2.4.1
23
+ env: TASK=test ARCONN=mysql2
24
+ gemfile: ci/Gemfile.activerecord-5.0.x
25
+ - rvm: 2.4.1
26
+ env: TASK=test ARCONN=sqlite3
27
+ gemfile: ci/Gemfile.activerecord-5.1.x
28
+ - rvm: 2.4.1
29
+ env: TASK=test ARCONN=mysql2
30
+ gemfile: ci/Gemfile.activerecord-5.1.x
31
+ - rvm: 2.4.1
32
+ env: TASK=test ARCONN=postgresql
33
+ gemfile: ci/Gemfile.activerecord-5.1.x
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-precounter.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'pry'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Takashi Kokubun
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,65 @@
1
+ # ActiveRecord::Precounter [![Build Status](https://travis-ci.org/k0kubun/activerecord-precounter.svg?branch=master)](https://travis-ci.org/k0kubun/activerecord-precounter)
2
+
3
+ Yet Another N+1 COUNT Query Killer for ActiveRecord, counter\_cache alternative.
4
+ ActiveRecord::Precounter allows you to cache count of associated records by eager loading.
5
+
6
+ This is another version of [activerecord-precount](https://github.com/k0kubun/activerecord-precount),
7
+ which is not elegant but designed to have no monkey-patch to ActiveRecord internal APIs for maintainability.
8
+
9
+ ## Synopsis
10
+
11
+ ### N+1 count query
12
+
13
+ Sometimes you may see many count queries for one association.
14
+ You can use counter\_cache to solve it, but you need to ALTER table and concern about dead lock to use counter\_cache.
15
+
16
+ ```rb
17
+ tweets = Tweet.all
18
+ tweets.each do |tweet|
19
+ p tweet.favorites.count
20
+ end
21
+ # SELECT `tweets`.* FROM `tweets`
22
+ # SELECT COUNT(*) FROM `favorites` WHERE `favorites`.`tweet_id` = 1
23
+ # SELECT COUNT(*) FROM `favorites` WHERE `favorites`.`tweet_id` = 2
24
+ # SELECT COUNT(*) FROM `favorites` WHERE `favorites`.`tweet_id` = 3
25
+ # SELECT COUNT(*) FROM `favorites` WHERE `favorites`.`tweet_id` = 4
26
+ # SELECT COUNT(*) FROM `favorites` WHERE `favorites`.`tweet_id` = 5
27
+ ```
28
+
29
+ ### Count eager loading
30
+
31
+ #### precount
32
+
33
+ With activerecord-precounter gem installed, you can use `ActiveRecord::Precounter#precount` method
34
+ to eagerly load counts of associated records.
35
+ Like `preload`, it loads counts by multiple queries
36
+
37
+ ```rb
38
+ tweets = Tweet.all
39
+ ActiveRecord::Precounter.new(tweets, :favorites).precount
40
+ tweets.each do |tweet|
41
+ p tweet.favorites_count
42
+ end
43
+ # SELECT `tweets`.* FROM `tweets`
44
+ # SELECT COUNT(`favorites`.`tweet_id`), `favorites`.`tweet_id` FROM `favorites` WHERE `favorites`.`tweet_id` IN (1, 2, 3, 4, 5) GROUP BY `favorites`.`tweet_id`
45
+ ```
46
+
47
+ ## Installation
48
+
49
+ Add this line to your application's Gemfile:
50
+
51
+ ```ruby
52
+ gem 'activerecord-precounter'
53
+ ```
54
+
55
+ ## Limitation
56
+
57
+ Target `has_many` association must have inversed `belongs_to`.
58
+ i.e. `ActiveRecord::Precounter.new(tweets, :favorites).precount` needs both `Tweet.has_many(:favorites)` and `Favorite.belongs_to(:tweet)`.
59
+
60
+ Unlike [activerecord-precount](https://github.com/k0kubun/activerecord-precount), the cache store is not ActiveRecord association and it does not utilize ActiveRecord preloader.
61
+ Thus you can't use `preload` to eager load counts for nested associations. And currently there's no JOIN support.
62
+
63
+ ## License
64
+
65
+ MIT License
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/precounter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'activerecord-precounter'
8
+ spec.version = ActiveRecord::Precounter::VERSION
9
+ spec.authors = ['Takashi Kokubun']
10
+ spec.email = ['takashikkbn@gmail.com']
11
+
12
+ spec.summary = %q{Yet Another N+1 COUNT Query Killer for ActiveRecord}
13
+ spec.description = %q{Yet Another N+1 COUNT Query Killer for ActiveRecord}
14
+ spec.homepage = 'https://github.com/k0kubun/activerecord-precounter'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'activerecord'
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'mysql2'
27
+ spec.add_development_dependency 'postgres'
28
+ spec.add_development_dependency 'rake'
29
+ spec.add_development_dependency 'rspec'
30
+ spec.add_development_dependency 'sqlite3'
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord/precounter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rails', '~> 4.2.0'
4
+ gemspec path: '..'
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rails', '~> 5.0.0'
4
+ gemspec path: '..'
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rails', '~> 5.1.0'
4
+ gemspec path: '..'
data/ci/travis.rb ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ commands = [
4
+ 'mysql -e "create database activerecord_unittest;"',
5
+ 'mysql -e "create database activerecord_unittest2;"',
6
+ 'psql -c "create database activerecord_unittest;" -U postgres',
7
+ 'psql -c "create database activerecord_unittest2;" -U postgres'
8
+ ]
9
+
10
+ commands.each do |command|
11
+ system("#{command} > /dev/null 2>&1")
12
+ end
13
+
14
+ exit system("bundle exec rake $TASK")
@@ -0,0 +1,56 @@
1
+ require 'active_record/precounter/version'
2
+
3
+ module ActiveRecord
4
+ class Precounter
5
+ class MissingInverseOf < StandardError; end
6
+
7
+ # @param [ActiveRecord::Relation] relation - Parent resources relation.
8
+ # @param [Array<String,Symbol>] association_names - Eager loaded association names. e.g. `[:users, :likes]`
9
+ def initialize(relation, *association_names)
10
+ @relation = relation
11
+ @association_names = association_names.map(&:to_s)
12
+ end
13
+
14
+ # @return [Array<ActiveRecord::Base>]
15
+ def precount
16
+ records = @relation.to_a
17
+ return if records.empty?
18
+
19
+ @association_names.each do |association_name|
20
+ reflection = @relation.klass.reflections.fetch(association_name)
21
+ if reflection.inverse_of.nil?
22
+ raise MissingInverseOf.new(
23
+ "`#{reflection.klass}` does not have inverse of `#{@relation.klass}##{reflection.name}`. "\
24
+ "Probably missing to call `#{reflection.klass}.belongs_to #{@relation.name.underscore.to_sym.inspect}`?"
25
+ )
26
+ end
27
+
28
+ count_by_id = reflection.klass.where(reflection.inverse_of.name => @relation).group(
29
+ reflection.inverse_of.foreign_key
30
+ ).count
31
+
32
+ writer = define_count_accessor(records.first, association_name)
33
+ records.each do |record|
34
+ record.public_send(writer, count_by_id.fetch(record.id, 0))
35
+ end
36
+ end
37
+ records
38
+ end
39
+
40
+ private
41
+
42
+ # @param [ActiveRecord::Base] record
43
+ # @param [String] association_name
44
+ # @return [String] writer method name
45
+ def define_count_accessor(record, association_name)
46
+ reader_name = "#{association_name}_count"
47
+ writer_name = "#{reader_name}="
48
+
49
+ if !record.respond_to?(reader_name) && !record.respond_to?(writer_name)
50
+ record.class.send(:attr_accessor, reader_name)
51
+ end
52
+
53
+ writer_name
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ class Precounter
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'active_record/precounter'
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-precounter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takashi Kokubun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-12 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
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
+ - !ruby/object:Gem::Dependency
56
+ name: postgres
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Yet Another N+1 COUNT Query Killer for ActiveRecord
112
+ email:
113
+ - takashikkbn@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - activerecord-precounter.gemspec
126
+ - bin/console
127
+ - bin/setup
128
+ - ci/Gemfile.activerecord-4.2.x
129
+ - ci/Gemfile.activerecord-5.0.x
130
+ - ci/Gemfile.activerecord-5.1.x
131
+ - ci/travis.rb
132
+ - lib/active_record/precounter.rb
133
+ - lib/active_record/precounter/version.rb
134
+ - lib/activerecord-precounter.rb
135
+ homepage: https://github.com/k0kubun/activerecord-precounter
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.5.2
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Yet Another N+1 COUNT Query Killer for ActiveRecord
159
+ test_files: []