activerecord-precount 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.travis.yml +28 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE +22 -0
  6. data/README.md +91 -0
  7. data/Rakefile +14 -0
  8. data/activerecord-precount.gemspec +31 -0
  9. data/benchmark.rb +42 -0
  10. data/ci/Gemfile.activerecord-4.1.x +4 -0
  11. data/ci/Gemfile.activerecord-4.2.x +4 -0
  12. data/ci/travis.rb +14 -0
  13. data/lib/active_record/associations/builder/count_loader.rb +13 -0
  14. data/lib/active_record/associations/count_loader.rb +27 -0
  15. data/lib/active_record/associations/preloader/count_loader.rb +41 -0
  16. data/lib/active_record/precount/base_extension.rb +7 -0
  17. data/lib/active_record/precount/extend.rb +18 -0
  18. data/lib/active_record/precount/has_many_extension.rb +25 -0
  19. data/lib/active_record/precount/join_dependency_extension.rb +23 -0
  20. data/lib/active_record/precount/preloader_extension.rb +15 -0
  21. data/lib/active_record/precount/reflection_extension.rb +50 -0
  22. data/lib/active_record/precount/relation_extension.rb +42 -0
  23. data/lib/active_record/precount/version.rb +5 -0
  24. data/lib/activerecord-precount.rb +8 -0
  25. data/sample/.gitignore +16 -0
  26. data/sample/Gemfile +19 -0
  27. data/sample/README.md +6 -0
  28. data/sample/Rakefile +6 -0
  29. data/sample/app/assets/images/.keep +0 -0
  30. data/sample/app/assets/javascripts/application.js +16 -0
  31. data/sample/app/assets/stylesheets/application.css +15 -0
  32. data/sample/app/controllers/application_controller.rb +13 -0
  33. data/sample/app/controllers/concerns/.keep +0 -0
  34. data/sample/app/helpers/application_helper.rb +2 -0
  35. data/sample/app/mailers/.keep +0 -0
  36. data/sample/app/models/.keep +0 -0
  37. data/sample/app/models/concerns/.keep +0 -0
  38. data/sample/app/models/favorite.rb +4 -0
  39. data/sample/app/models/tweet.rb +6 -0
  40. data/sample/app/models/user.rb +4 -0
  41. data/sample/app/views/application/index.html.erb +49 -0
  42. data/sample/app/views/layouts/application.html.erb +14 -0
  43. data/sample/bin/bundle +3 -0
  44. data/sample/bin/rails +8 -0
  45. data/sample/bin/rake +8 -0
  46. data/sample/bin/spring +18 -0
  47. data/sample/config.ru +4 -0
  48. data/sample/config/application.rb +30 -0
  49. data/sample/config/boot.rb +4 -0
  50. data/sample/config/database.yml +11 -0
  51. data/sample/config/environment.rb +5 -0
  52. data/sample/config/environments/development.rb +40 -0
  53. data/sample/config/environments/production.rb +82 -0
  54. data/sample/config/environments/test.rb +39 -0
  55. data/sample/config/initializers/assets.rb +8 -0
  56. data/sample/config/initializers/backtrace_silencers.rb +7 -0
  57. data/sample/config/initializers/cookies_serializer.rb +3 -0
  58. data/sample/config/initializers/filter_parameter_logging.rb +4 -0
  59. data/sample/config/initializers/inflections.rb +16 -0
  60. data/sample/config/initializers/mime_types.rb +4 -0
  61. data/sample/config/initializers/session_store.rb +3 -0
  62. data/sample/config/initializers/wrap_parameters.rb +14 -0
  63. data/sample/config/locales/en.yml +23 -0
  64. data/sample/config/routes.rb +3 -0
  65. data/sample/config/secrets.yml +22 -0
  66. data/sample/db/migrate/20141122002518_create_tweets.rb +10 -0
  67. data/sample/db/migrate/20141122002548_create_favorites.rb +10 -0
  68. data/sample/db/migrate/20141122002555_create_users.rb +8 -0
  69. data/sample/db/schema.rb +35 -0
  70. data/sample/db/seeds.rb +12 -0
  71. data/sample/lib/assets/.keep +0 -0
  72. data/sample/lib/tasks/.keep +0 -0
  73. data/sample/log/.keep +0 -0
  74. data/sample/public/404.html +67 -0
  75. data/sample/public/422.html +67 -0
  76. data/sample/public/500.html +66 -0
  77. data/sample/public/favicon.ico +0 -0
  78. data/sample/public/robots.txt +5 -0
  79. data/sample/vendor/assets/javascripts/.keep +0 -0
  80. data/sample/vendor/assets/stylesheets/.keep +0 -0
  81. data/test/cases/associations/eager_load_test.rb +20 -0
  82. data/test/cases/associations/includes_test.rb +30 -0
  83. data/test/cases/associations/precount_test.rb +36 -0
  84. data/test/cases/associations/preload_test.rb +30 -0
  85. data/test/cases/db_config.rb +24 -0
  86. data/test/cases/helper.rb +4 -0
  87. data/test/cases/test_case.rb +86 -0
  88. data/test/config.example.yml +140 -0
  89. data/test/config.rb +5 -0
  90. data/test/models/favorite.rb +3 -0
  91. data/test/models/tweet.rb +4 -0
  92. data/test/schema/schema.rb +16 -0
  93. data/test/support/autorun.rb +5 -0
  94. data/test/support/config.rb +43 -0
  95. data/test/support/connection.rb +15 -0
  96. metadata +293 -0
data/test/config.rb ADDED
@@ -0,0 +1,5 @@
1
+ TEST_ROOT = File.expand_path(File.dirname(__FILE__))
2
+ ASSETS_ROOT = TEST_ROOT + "/assets"
3
+ FIXTURES_ROOT = TEST_ROOT + "/fixtures"
4
+ MIGRATIONS_ROOT = TEST_ROOT + "/migrations"
5
+ SCHEMA_ROOT = TEST_ROOT + "/schema"
@@ -0,0 +1,3 @@
1
+ class Favorite < ActiveRecord::Base
2
+ belongs_to :tweet, counter_cache: :favorites_count_cache
3
+ end
@@ -0,0 +1,4 @@
1
+ class Tweet < ActiveRecord::Base
2
+ has_many :favorites, count_loader: true
3
+ has_many :favs, class_name: 'Favorite'
4
+ end
@@ -0,0 +1,16 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :favorites, force: true do |t|
3
+ t.integer :tweet_id
4
+ t.integer :user_id
5
+ t.timestamps
6
+ end
7
+ add_index :favorites, :tweet_id
8
+
9
+ create_table :tweets, force: true do |t|
10
+ t.integer :in_reply_to_tweet_id
11
+ t.integer :user_id
12
+ t.integer :favorites_count_cache
13
+ t.timestamps
14
+ end
15
+ add_index :tweets, :in_reply_to_tweet_id
16
+ end
@@ -0,0 +1,5 @@
1
+ gem 'minitest'
2
+
3
+ require 'minitest'
4
+
5
+ Minitest.autorun
@@ -0,0 +1,43 @@
1
+ require 'yaml'
2
+ require 'erubis'
3
+ require 'fileutils'
4
+ require 'pathname'
5
+
6
+ module ARTest
7
+ class << self
8
+ def config
9
+ @config ||= read_config
10
+ end
11
+
12
+ private
13
+
14
+ def config_file
15
+ Pathname.new(ENV['ARCONFIG'] || TEST_ROOT + '/config.yml')
16
+ end
17
+
18
+ def read_config
19
+ unless config_file.exist?
20
+ FileUtils.cp TEST_ROOT + '/config.example.yml', config_file
21
+ end
22
+
23
+ erb = Erubis::Eruby.new(config_file.read)
24
+ expand_config(YAML.parse(erb.result(binding)).transform)
25
+ end
26
+
27
+ def expand_config(config)
28
+ config['connections'].each do |adapter, connection|
29
+ dbs = [['arunit', 'activerecord_unittest'], ['arunit2', 'activerecord_unittest2']]
30
+ dbs.each do |name, dbname|
31
+ unless connection[name].is_a?(Hash)
32
+ connection[name] = { 'database' => connection[name] }
33
+ end
34
+
35
+ connection[name]['database'] ||= dbname
36
+ connection[name]['adapter'] ||= adapter
37
+ end
38
+ end
39
+
40
+ config
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ module ARTest
2
+ def self.connection_name
3
+ ENV['ARCONN'] || config['default_connection']
4
+ end
5
+
6
+ def self.connection_config
7
+ config['connections'][connection_name]
8
+ end
9
+
10
+ def self.connect
11
+ puts "Using #{connection_name}"
12
+ ActiveRecord::Base.configurations = connection_config
13
+ ActiveRecord::Base.establish_connection :arunit
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,293 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-precount
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Takashi Kokubun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-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: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
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: rake
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: erubis
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: bundler
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: pry
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
+ - !ruby/object:Gem::Dependency
112
+ name: mysql2
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: postgres
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rbench
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: dalli
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: N+1 count query killer for ActiveRecord
168
+ email:
169
+ - takashikkbn@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".travis.yml"
176
+ - Gemfile
177
+ - LICENSE
178
+ - README.md
179
+ - Rakefile
180
+ - activerecord-precount.gemspec
181
+ - benchmark.rb
182
+ - ci/Gemfile.activerecord-4.1.x
183
+ - ci/Gemfile.activerecord-4.2.x
184
+ - ci/travis.rb
185
+ - lib/active_record/associations/builder/count_loader.rb
186
+ - lib/active_record/associations/count_loader.rb
187
+ - lib/active_record/associations/preloader/count_loader.rb
188
+ - lib/active_record/precount/base_extension.rb
189
+ - lib/active_record/precount/extend.rb
190
+ - lib/active_record/precount/has_many_extension.rb
191
+ - lib/active_record/precount/join_dependency_extension.rb
192
+ - lib/active_record/precount/preloader_extension.rb
193
+ - lib/active_record/precount/reflection_extension.rb
194
+ - lib/active_record/precount/relation_extension.rb
195
+ - lib/active_record/precount/version.rb
196
+ - lib/activerecord-precount.rb
197
+ - sample/.gitignore
198
+ - sample/Gemfile
199
+ - sample/README.md
200
+ - sample/Rakefile
201
+ - sample/app/assets/images/.keep
202
+ - sample/app/assets/javascripts/application.js
203
+ - sample/app/assets/stylesheets/application.css
204
+ - sample/app/controllers/application_controller.rb
205
+ - sample/app/controllers/concerns/.keep
206
+ - sample/app/helpers/application_helper.rb
207
+ - sample/app/mailers/.keep
208
+ - sample/app/models/.keep
209
+ - sample/app/models/concerns/.keep
210
+ - sample/app/models/favorite.rb
211
+ - sample/app/models/tweet.rb
212
+ - sample/app/models/user.rb
213
+ - sample/app/views/application/index.html.erb
214
+ - sample/app/views/layouts/application.html.erb
215
+ - sample/bin/bundle
216
+ - sample/bin/rails
217
+ - sample/bin/rake
218
+ - sample/bin/spring
219
+ - sample/config.ru
220
+ - sample/config/application.rb
221
+ - sample/config/boot.rb
222
+ - sample/config/database.yml
223
+ - sample/config/environment.rb
224
+ - sample/config/environments/development.rb
225
+ - sample/config/environments/production.rb
226
+ - sample/config/environments/test.rb
227
+ - sample/config/initializers/assets.rb
228
+ - sample/config/initializers/backtrace_silencers.rb
229
+ - sample/config/initializers/cookies_serializer.rb
230
+ - sample/config/initializers/filter_parameter_logging.rb
231
+ - sample/config/initializers/inflections.rb
232
+ - sample/config/initializers/mime_types.rb
233
+ - sample/config/initializers/session_store.rb
234
+ - sample/config/initializers/wrap_parameters.rb
235
+ - sample/config/locales/en.yml
236
+ - sample/config/routes.rb
237
+ - sample/config/secrets.yml
238
+ - sample/db/migrate/20141122002518_create_tweets.rb
239
+ - sample/db/migrate/20141122002548_create_favorites.rb
240
+ - sample/db/migrate/20141122002555_create_users.rb
241
+ - sample/db/schema.rb
242
+ - sample/db/seeds.rb
243
+ - sample/lib/assets/.keep
244
+ - sample/lib/tasks/.keep
245
+ - sample/log/.keep
246
+ - sample/public/404.html
247
+ - sample/public/422.html
248
+ - sample/public/500.html
249
+ - sample/public/favicon.ico
250
+ - sample/public/robots.txt
251
+ - sample/vendor/assets/javascripts/.keep
252
+ - sample/vendor/assets/stylesheets/.keep
253
+ - test/cases/associations/eager_load_test.rb
254
+ - test/cases/associations/includes_test.rb
255
+ - test/cases/associations/precount_test.rb
256
+ - test/cases/associations/preload_test.rb
257
+ - test/cases/db_config.rb
258
+ - test/cases/helper.rb
259
+ - test/cases/test_case.rb
260
+ - test/config.example.yml
261
+ - test/config.rb
262
+ - test/models/favorite.rb
263
+ - test/models/tweet.rb
264
+ - test/schema/schema.rb
265
+ - test/support/autorun.rb
266
+ - test/support/config.rb
267
+ - test/support/connection.rb
268
+ homepage: https://github.com/k0kubun/activerecord-precount
269
+ licenses:
270
+ - MIT
271
+ metadata: {}
272
+ post_install_message:
273
+ rdoc_options: []
274
+ require_paths:
275
+ - lib
276
+ required_ruby_version: !ruby/object:Gem::Requirement
277
+ requirements:
278
+ - - ">="
279
+ - !ruby/object:Gem::Version
280
+ version: '2.0'
281
+ required_rubygems_version: !ruby/object:Gem::Requirement
282
+ requirements:
283
+ - - ">="
284
+ - !ruby/object:Gem::Version
285
+ version: '0'
286
+ requirements: []
287
+ rubyforge_project:
288
+ rubygems_version: 2.4.5
289
+ signing_key:
290
+ specification_version: 4
291
+ summary: N+1 count query killer for ActiveRecord
292
+ test_files: []
293
+ has_rdoc: