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
@@ -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 Precount
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,15 @@
1
+ module ActiveRecord
2
+ module Precount
3
+ module PreloaderExtension
4
+ def preloader_for(reflection, owners, rhs_klass)
5
+ preloader = super(reflection, owners, rhs_klass)
6
+ return preloader if preloader
7
+
8
+ case reflection.macro
9
+ when :count_loader
10
+ Associations::Preloader::CountLoader
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,50 @@
1
+ module ActiveRecord
2
+ module Precount
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
+ if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 2
15
+ Reflection::CountLoaderReflection.new(name, scope, options, ar)
16
+ else
17
+ Reflection::AssociationReflection.new(macro, name, scope, options, ar)
18
+ end
19
+ else
20
+ super(macro, name, scope, options, ar)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ module AssociationReflectionExtension
27
+ def klass
28
+ case macro
29
+ when :count_loader
30
+ @klass ||= active_record.send(:compute_type, options[:class_name] || name_without_count.singularize.classify)
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ def name_without_count
37
+ name.to_s.sub(/_count$/, "")
38
+ end
39
+
40
+ def association_class
41
+ case macro
42
+ when :count_loader
43
+ ActiveRecord::Associations::CountLoader
44
+ else
45
+ super
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,42 @@
1
+ module ActiveRecord
2
+ module Precount
3
+ module RelationExtension
4
+ def precount(*args)
5
+ check_if_method_has_arguments!(:precount, args)
6
+ spawn.precount!(*args)
7
+ end
8
+
9
+ def precount!(*args)
10
+ define_count_loader!(*args)
11
+
12
+ self.preload_values += args.map { |arg| :"#{arg}_count" }
13
+ self
14
+ end
15
+
16
+ private
17
+
18
+ def define_count_loader!(*args)
19
+ args.each do |arg|
20
+ raise ArgumentError, "#{klass} does not have :#{arg} association." unless has_reflection?(arg)
21
+ next if has_reflection?(counter_name = :"#{arg}_count")
22
+
23
+ options = reflection_for(arg).options.slice(*Associations::Builder::CountLoader.valid_options)
24
+ reflection = Associations::Builder::CountLoader.build(klass, counter_name, nil, options)
25
+ Reflection.add_reflection(model, counter_name, reflection)
26
+ end
27
+ end
28
+
29
+ def has_reflection?(name)
30
+ reflection_for(name).present?
31
+ end
32
+
33
+ def reflection_for(name)
34
+ if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 2
35
+ reflections[name.to_s]
36
+ else
37
+ reflections[name.to_sym]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module Precount
3
+ VERSION = "0.4.0"
4
+ end
5
+ end
@@ -0,0 +1,8 @@
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/precount/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.2.0'
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-precount', path: '..'
14
+
15
+ group :development do
16
+ gem 'pry-rails'
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 .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,13 @@
1
+ class ApplicationController < ActionController::Base
2
+ def index
3
+ @tweets = Tweet.all
4
+ if eager_load?
5
+ @tweets = @tweets.precount(:replies).preload(in_reply_to: :favorites_count)
6
+ end
7
+ end
8
+
9
+ def eager_load?
10
+ params[:eager_load] == 'true'
11
+ end
12
+ helper_method :eager_load?
13
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,4 @@
1
+ class Favorite < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :tweet
4
+ end
@@ -0,0 +1,6 @@
1
+ class Tweet < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :in_reply_to, class_name: 'Tweet', foreign_key: :in_reply_to_tweet_id
4
+ has_many :favorites, count_loader: true
5
+ has_many :replies, class_name: 'Tweet', foreign_key: :in_reply_to_tweet_id
6
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :favorites
3
+ has_many :tweets
4
+ end
@@ -0,0 +1,49 @@
1
+ <% start = Time.now %>
2
+ <table border="1">
3
+ <thead>
4
+ <tr>
5
+ <th>
6
+ id
7
+ </th>
8
+ <th>
9
+ replies_count
10
+ </th>
11
+ <th>
12
+ in_reply_to.id
13
+ </th>
14
+ <th>
15
+ in_reply_to.favorites_count
16
+ </th>
17
+ </tr>
18
+ </thead>
19
+ <tbody>
20
+ <% @tweets.each do |tweet| %>
21
+ <tr>
22
+ <td>
23
+ <%= tweet.id %>
24
+ </td>
25
+ <td>
26
+ <%= eager_load? ? tweet.replies_count : tweet.replies.count %>
27
+ </td>
28
+ <td>
29
+ <%= tweet.in_reply_to.try(:id) %>
30
+ </td>
31
+ <td>
32
+ <%= tweet.in_reply_to.try(:favorites_count) %>
33
+ </td>
34
+ </tr>
35
+ <% end %>
36
+ </tbody>
37
+ </table>
38
+ <% finish = Time.now %>
39
+
40
+ <p>
41
+ <%= eager_load? ? 'Eager loaded' : 'N+1 query' %>
42
+ </p>
43
+ <p>
44
+ Time: <%= "%.1f" % ((finish - start) * 1000) %>ms
45
+ </p>
46
+
47
+ <p>
48
+ <%= link_to 'Swtich', url_for(eager_load: !eager_load?) %>
49
+ <p>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Sample</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/sample/bin/bundle ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
data/sample/bin/rails ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ load File.expand_path("../spring", __FILE__)
4
+ rescue LoadError
5
+ end
6
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
7
+ require_relative '../config/boot'
8
+ require 'rails/commands'
data/sample/bin/rake ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ load File.expand_path("../spring", __FILE__)
4
+ rescue LoadError
5
+ end
6
+ require_relative '../config/boot'
7
+ require 'rake'
8
+ Rake.application.run
data/sample/bin/spring ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # This file loads spring without using Bundler, in order to be fast
4
+ # It gets overwritten when you run the `spring binstub` command
5
+
6
+ unless defined?(Spring)
7
+ require "rubygems"
8
+ require "bundler"
9
+
10
+ if match = Bundler.default_lockfile.read.match(/^GEM$.*?^ spring \((.*?)\)$.*?^$/m)
11
+ ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
12
+ ENV["GEM_HOME"] = ""
13
+ Gem.paths = ENV
14
+
15
+ gem "spring", match[1]
16
+ require "spring/binstub"
17
+ end
18
+ end
data/sample/config.ru ADDED
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "active_model/railtie"
5
+ require "active_record/railtie"
6
+ require "action_controller/railtie"
7
+ require "action_mailer/railtie"
8
+ require "action_view/railtie"
9
+ require "sprockets/railtie"
10
+ # require "rails/test_unit/railtie"
11
+
12
+ # Require the gems listed in Gemfile, including any gems
13
+ # you've limited to :test, :development, or :production.
14
+ Bundler.require(*Rails.groups)
15
+
16
+ module Sample
17
+ class Application < Rails::Application
18
+ # Settings in config/environments/* take precedence over those specified here.
19
+ # Application configuration should go into files in config/initializers
20
+ # -- all .rb files in that directory are automatically loaded.
21
+
22
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
23
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
24
+ # config.time_zone = 'Central Time (US & Canada)'
25
+
26
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
27
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
28
+ # config.i18n.default_locale = :de
29
+ end
30
+ end