activerecord-count_loader 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/README.md +72 -0
  6. data/Rakefile +1 -0
  7. data/activerecord-count_loader.gemspec +27 -0
  8. data/lib/active_record/associations/builder/count_loader.rb +13 -0
  9. data/lib/active_record/associations/count_loader.rb +17 -0
  10. data/lib/active_record/associations/preloader/count_loader.rb +54 -0
  11. data/lib/active_record/count_loader/extend.rb +8 -0
  12. data/lib/active_record/count_loader/has_many_extension.rb +25 -0
  13. data/lib/active_record/count_loader/join_dependency_extension.rb +23 -0
  14. data/lib/active_record/count_loader/reflection_extension.rb +46 -0
  15. data/lib/active_record/count_loader/version.rb +5 -0
  16. data/lib/activerecord-count_loader.rb +11 -0
  17. data/sample/.gitignore +16 -0
  18. data/sample/Gemfile +19 -0
  19. data/sample/README.md +6 -0
  20. data/sample/Rakefile +6 -0
  21. data/sample/app/assets/images/.keep +0 -0
  22. data/sample/app/assets/javascripts/application.js +16 -0
  23. data/sample/app/assets/stylesheets/application.css +15 -0
  24. data/sample/app/controllers/application_controller.rb +13 -0
  25. data/sample/app/controllers/concerns/.keep +0 -0
  26. data/sample/app/helpers/application_helper.rb +2 -0
  27. data/sample/app/mailers/.keep +0 -0
  28. data/sample/app/models/.keep +0 -0
  29. data/sample/app/models/concerns/.keep +0 -0
  30. data/sample/app/models/favorite.rb +4 -0
  31. data/sample/app/models/tweet.rb +6 -0
  32. data/sample/app/models/user.rb +4 -0
  33. data/sample/app/views/application/index.html.erb +49 -0
  34. data/sample/app/views/layouts/application.html.erb +14 -0
  35. data/sample/bin/bundle +3 -0
  36. data/sample/bin/rails +8 -0
  37. data/sample/bin/rake +8 -0
  38. data/sample/bin/spring +18 -0
  39. data/sample/config.ru +4 -0
  40. data/sample/config/application.rb +30 -0
  41. data/sample/config/boot.rb +4 -0
  42. data/sample/config/database.yml +11 -0
  43. data/sample/config/environment.rb +5 -0
  44. data/sample/config/environments/development.rb +40 -0
  45. data/sample/config/environments/production.rb +82 -0
  46. data/sample/config/environments/test.rb +39 -0
  47. data/sample/config/initializers/assets.rb +8 -0
  48. data/sample/config/initializers/backtrace_silencers.rb +7 -0
  49. data/sample/config/initializers/cookies_serializer.rb +3 -0
  50. data/sample/config/initializers/filter_parameter_logging.rb +4 -0
  51. data/sample/config/initializers/inflections.rb +16 -0
  52. data/sample/config/initializers/mime_types.rb +4 -0
  53. data/sample/config/initializers/session_store.rb +3 -0
  54. data/sample/config/initializers/wrap_parameters.rb +14 -0
  55. data/sample/config/locales/en.yml +23 -0
  56. data/sample/config/routes.rb +3 -0
  57. data/sample/config/secrets.yml +22 -0
  58. data/sample/db/migrate/20141122002518_create_tweets.rb +10 -0
  59. data/sample/db/migrate/20141122002548_create_favorites.rb +10 -0
  60. data/sample/db/migrate/20141122002555_create_users.rb +8 -0
  61. data/sample/db/schema.rb +35 -0
  62. data/sample/db/seeds.rb +12 -0
  63. data/sample/lib/assets/.keep +0 -0
  64. data/sample/lib/tasks/.keep +0 -0
  65. data/sample/log/.keep +0 -0
  66. data/sample/public/404.html +67 -0
  67. data/sample/public/422.html +67 -0
  68. data/sample/public/500.html +66 -0
  69. data/sample/public/favicon.ico +0 -0
  70. data/sample/public/robots.txt +5 -0
  71. data/sample/vendor/assets/javascripts/.keep +0 -0
  72. data/sample/vendor/assets/stylesheets/.keep +0 -0
  73. metadata +213 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b18fe399d79f7718389245f46d6c8344489c7797
4
+ data.tar.gz: 41ce60a38bb937a114c0edcff1b79e183e1d94d5
5
+ SHA512:
6
+ metadata.gz: fab66eaef7777f10c7ec1778ee58f1cc2588187d8194bcb4698b6c458452e7822fd9e5fd4c47dd054f2410dde1f234788d77f292d44e32777f69516ab4b63cf7
7
+ data.tar.gz: 7ae932c1f0ef58173a8f679863a6292e3a8f9195445476671857b29656d51b2e85aa20edaaceb6f2e45551b775ad1f208d3c683b7352fc42d8741e75dbd12244
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.sqlite3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-count_loader.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Takashi Kokubun
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # ActiveRecord::CountLoader
2
+
3
+ N+1 count query killer for ActiveRecord.
4
+ ActiveRecord::CountLoader allows you to cache count of associated records by eager loading.
5
+
6
+ ## Why ActiveRecord::CountLoader?
7
+ Rails provides a way to resolve N+1 count query, which is [belongs\_to's counter\_cache option](http://guides.rubyonrails.org/association_basics.html#counter-cache).
8
+ It requires a column to cache the count. But adding a column just for count cache is overkill.
9
+
10
+ Thus this plugin enables you to preload counts in the same way as `has_many` and `belongs_to`.
11
+ `count_loader` is an ActiveRecord's association, which is preloadable by `preload` or `includes`.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'activerecord-count_loader'
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Enable count\_loader option
24
+ First, enable your has\_many association's count\_loader option.
25
+
26
+ ```diff
27
+ class Tweet
28
+ - has_many :favorites
29
+ + has_many :favorites, count_loader: true
30
+ end
31
+ ```
32
+
33
+ The option defines an additional association whose name is `favorites_count`.
34
+ Its association type is not an ordinary one (i.e. `has_many`, `belongs_to`) but `count_loader`.
35
+
36
+ ### Preload the association
37
+ This association works well by default.
38
+
39
+ ```rb
40
+ @tweets = Tweet.all
41
+ @tweets.each do |tweet|
42
+ p tweets.favorites_count # same as tweets.favorites.count
43
+ end
44
+ ```
45
+
46
+ You can eagerly load `count_loader` association by `includes` or `preload`.
47
+
48
+ ```rb
49
+ @tweets = Tweet.preload(:favorites_count)
50
+ @tweets.each do |tweet|
51
+ p tweets.favorites_count # this line doesn't execute an additional query
52
+ end
53
+ ```
54
+
55
+ Since it is association, you can preload nested `count_loader` association.
56
+
57
+ ```rb
58
+ @users = User.preload(tweets: :favorites_count).all
59
+ @users.each do |user|
60
+ user.tweets.each do |tweet|
61
+ p tweet.favorites_count # this line doesn't execute an additional query
62
+ end
63
+ end
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( https://github.com/k0kubun/activerecord-count_loader/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'active_record/count_loader/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "activerecord-count_loader"
7
+ spec.version = ActiveRecord::CountLoader::VERSION
8
+ spec.authors = ["Takashi Kokubun"]
9
+ spec.email = ["takashikkbn@gmail.com"]
10
+ spec.summary = %q{N+1 count query killer for ActiveRecord}
11
+ spec.description = %q{N+1 count query killer for ActiveRecord}
12
+ spec.homepage = "https://github.com/k0kubun/activerecord-count_loader"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.test_files = spec.files.grep(%r{^spec/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.required_ruby_version = ">= 1.9.2"
20
+ spec.add_runtime_dependency "activerecord", ">= 3.2.0"
21
+ spec.add_development_dependency "rspec", "~> 3.0.0"
22
+ spec.add_development_dependency "factory_girl", "~> 4.2.0"
23
+ spec.add_development_dependency "sqlite3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "pry"
27
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveRecord::Associations::Builder
2
+ class CountLoader < SingularAssociation
3
+ self.valid_options = [:class_name, :foreign_key]
4
+
5
+ def macro
6
+ :count_loader
7
+ end
8
+
9
+ def self.valid_dependent_options
10
+ []
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveRecord
2
+ module Associations
3
+ class CountLoader < SingularAssociation
4
+ # Not preloaded behaviour of count_loader association
5
+ # When this method is called, it will be N+1 query
6
+ def load_target
7
+ count_target = reflection.name_without_count.to_sym
8
+ @target = owner.association(count_target).count
9
+
10
+ loaded! unless loaded?
11
+ target
12
+ rescue ActiveRecord::RecordNotFound
13
+ reset
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ module ActiveRecord
2
+ module Associations
3
+ class Preloader
4
+ class CountLoader < SingularAssociation
5
+ def association_key_name
6
+ reflection.foreign_key
7
+ end
8
+
9
+ def owner_key_name
10
+ reflection.active_record_primary_key
11
+ end
12
+
13
+ private
14
+
15
+ def preload(preloader)
16
+ associated_records_by_owner(preloader).each do |owner, associated_records|
17
+ count = associated_records.count
18
+
19
+ association = owner.association(reflection.name)
20
+ association.target = count
21
+ end
22
+ end
23
+
24
+ def load_slices(slices)
25
+ @preloaded_records = slices.flat_map { |slice|
26
+ records_for(slice)
27
+ }
28
+
29
+ @preloaded_records.map { |record|
30
+ key = record
31
+ [record, key]
32
+ }
33
+ end
34
+
35
+ def query_scope(ids)
36
+ scope.where(association_key.in(ids)).pluck(association_key_name)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def preloader_for_with_count_loader(reflection, owners, rhs_klass)
43
+ preloader = preloader_for_without_count_loader(reflection, owners, rhs_klass)
44
+ return preloader if preloader
45
+
46
+ case reflection.macro
47
+ when :count_loader
48
+ CountLoader
49
+ end
50
+ end
51
+ alias_method_chain :preloader_for, :count_loader
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ ActiveSupport.on_load(:active_record) do
2
+ module ActiveRecord
3
+ Reflection.prepend(CountLoader::ReflectionExtension)
4
+ Associations::JoinDependency.prepend(CountLoader::JoinDependencyExtension)
5
+ Associations::Builder::HasMany.prepend(CountLoader::Builder::HasManyExtension)
6
+ Reflection::AssociationReflection.prepend(CountLoader::AssociationReflectionExtension)
7
+ end
8
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveRecord
2
+ module CountLoader
3
+ module Builder
4
+ module HasManyExtension
5
+ def valid_options
6
+ super + [:count_loader]
7
+ end
8
+
9
+ def build(model)
10
+ define_count_loader(model) if options[:count_loader]
11
+ super
12
+ end
13
+
14
+ def define_count_loader(model)
15
+ name_with_count = :"#{name}_count"
16
+ name_with_count = options[:count_loader] if options[:count_loader].is_a?(Symbol)
17
+
18
+ valid_options = options.slice(*Associations::Builder::CountLoader.valid_options)
19
+ reflection = Associations::Builder::CountLoader.build(model, name_with_count, nil, valid_options)
20
+ Reflection.add_reflection(model, name_with_count, reflection)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module ActiveRecord
2
+ # This imitates EagerLoadPolymorphicError
3
+ class EagerLoadCountLoaderError < ActiveRecordError
4
+ def initialize(reflection)
5
+ super("Cannot eagerly load the count_loader association #{reflection.name.inspect}")
6
+ end
7
+ end
8
+
9
+ module CountLoader
10
+ module JoinDependencyExtension
11
+ def build(associations, base_klass)
12
+ associations.map do |name, right|
13
+ reflection = find_reflection base_klass, name
14
+ if reflection.macro == :count_loader
15
+ raise EagerLoadCountLoaderError.new(reflection)
16
+ end
17
+ end
18
+
19
+ super(associations, base_klass)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ module ActiveRecord
2
+ module CountLoader
3
+ module ReflectionExtension
4
+ def self.prepended(base)
5
+ class << base
6
+ prepend ClassMethods
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def create(macro, name, scope, options, ar)
12
+ case macro
13
+ when :count_loader
14
+ Reflection::AssociationReflection.new(macro, name, scope, options, ar)
15
+ else
16
+ super(macro, name, scope, options, ar)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ module AssociationReflectionExtension
23
+ def klass
24
+ case macro
25
+ when :count_loader
26
+ @klass ||= active_record.send(:compute_type, options[:class_name] || name_without_count.singularize.classify)
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ def name_without_count
33
+ name.to_s.sub(/_count$/, "")
34
+ end
35
+
36
+ def association_class
37
+ case macro
38
+ when :count_loader
39
+ ActiveRecord::Associations::CountLoader
40
+ else
41
+ super
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module CountLoader
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ require "active_record"
2
+ require "active_support/lazy_load_hooks"
3
+
4
+ require "active_record/associations/count_loader"
5
+ require "active_record/associations/builder/count_loader"
6
+ require "active_record/associations/preloader/count_loader"
7
+
8
+ require "active_record/count_loader/has_many_extension"
9
+ require "active_record/count_loader/join_dependency_extension"
10
+ require "active_record/count_loader/reflection_extension"
11
+ require "active_record/count_loader/extend"
data/sample/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
data/sample/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '4.1.5'
4
+ gem 'mysql2'
5
+ gem 'sass-rails', '~> 4.0.3'
6
+ gem 'uglifier', '>= 1.3.0'
7
+ gem 'coffee-rails', '~> 4.0.0'
8
+ gem 'jquery-rails'
9
+ gem 'turbolinks'
10
+ gem 'jbuilder', '~> 2.0'
11
+ gem 'sdoc', '~> 0.4.0', group: :doc
12
+
13
+ gem 'activerecord-count_loader', path: '..'
14
+
15
+ group :development do
16
+ gem 'pry'
17
+ gem 'spring'
18
+ gem 'silencer'
19
+ end
data/sample/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # sample
2
+
3
+ ```bash
4
+ $ bundle exec rake db:create db:migrate
5
+ $ bundle exec rake db:seed
6
+ ```
data/sample/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .