active_record_association_query_economizer 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 +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +36 -0
- data/active_record_association_query_economizer.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gemfiles/activerecord-42.gemfile +5 -0
- data/gemfiles/activerecord-50.gemfile +5 -0
- data/gemfiles/activerecord-51.gemfile +5 -0
- data/lib/active_record_association_query_economizer.rb +11 -0
- data/lib/active_record_association_query_economizer/rails4-2/associations.rb +60 -0
- data/lib/active_record_association_query_economizer/rails5/associations.rb +47 -0
- data/lib/active_record_association_query_economizer/version.rb +3 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a37d4e68d13db2c12393514216621e56fa2bde304f7efdafb041c980b8990dad
|
4
|
+
data.tar.gz: 3195f945400f046a5a6bced0d4fb6db840e89b7d4bd8125a485004b13f382eb6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c9cb87a348df517cbf6d3c8a621568f73c9d278abc2b9c65783795de44a604f28307d9a16d280b2ae12bfc9c3249d002dc9288e1e89a73e2c598896924e23b77
|
7
|
+
data.tar.gz: 1f4bf6b4b3dd73f0656945f2cbe52e37c34125420a74a01ef505bba1b08a55316691b33cebce47952a9bb64ec3e8ed4eb23513a32439b46bb7b5842114118379
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 toru.yagi
|
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,57 @@
|
|
1
|
+
# ActiveRecordAssociationQueryEconomizer
|
2
|
+
|
3
|
+
Enables to define preloading conditions in the arguments of `has_many`.
|
4
|
+
|
5
|
+
Condition is defined as an array of symbolized method name or Proc object.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'active_record_association_query_economizer'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install active_record_association_query_economizer
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```
|
26
|
+
class SampleObject < ActiveRecord::Base
|
27
|
+
has_many :sample_associations, preload_if: [:one_condition?, :another_condition?]
|
28
|
+
|
29
|
+
def one_condition?
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
def another_condition?
|
34
|
+
false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
(Proc)
|
40
|
+
|
41
|
+
```
|
42
|
+
class SampleObject < ActiveRecord::Base
|
43
|
+
has_many :sample_associations, preload_if: -> (record) {
|
44
|
+
true
|
45
|
+
}
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record_association_query_economizer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
52
|
+
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
57
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs = ["test"]
|
7
|
+
t.pattern = "test/**/*_test.rb"
|
8
|
+
t.ruby_opts = ['-w']
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :test
|
12
|
+
|
13
|
+
pwd = File.expand_path('../', __FILE__)
|
14
|
+
|
15
|
+
gemfiles = %w(
|
16
|
+
activerecord-42
|
17
|
+
activerecord-50
|
18
|
+
activerecord-51
|
19
|
+
)
|
20
|
+
|
21
|
+
namespace :test do
|
22
|
+
gemfiles.each do |gemfile|
|
23
|
+
desc "Run Tests by #{gemfile}.gemfile"
|
24
|
+
task gemfile do
|
25
|
+
sh "BUNDLE_GEMFILE='#{pwd}/gemfiles/#{gemfile}.gemfile' bundle install --path #{pwd}/.bundle"
|
26
|
+
sh "BUNDLE_GEMFILE='#{pwd}/gemfiles/#{gemfile}.gemfile' bundle exec rake -t test"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Run All Tests"
|
31
|
+
task :all do
|
32
|
+
gemfiles.each do |gemfile|
|
33
|
+
Rake::Task["test:#{gemfile}"].invoke
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,29 @@
|
|
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_association_query_economizer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_record_association_query_economizer"
|
8
|
+
spec.version = ActiveRecordAssociationQueryEconomizer::VERSION
|
9
|
+
spec.authors = ["toru.yagi"]
|
10
|
+
|
11
|
+
spec.summary = %q{Optimize queries of ActiveRecord preload functions.}
|
12
|
+
spec.homepage = "https://github.com/negito6"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency 'minitest', '~> 3'
|
25
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'activerecord', '>= 4.2', '<= 5.1.1'
|
28
|
+
|
29
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_record_association_query_economizer"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "active_record_association_query_economizer/version"
|
2
|
+
|
3
|
+
activerecord_version = ActiveRecord.version
|
4
|
+
case activerecord_version
|
5
|
+
when Gem::Requirement.create(['>= 4.2', '< 5'])
|
6
|
+
require 'active_record_association_query_economizer/rails4-2/associations'
|
7
|
+
when Gem::Requirement.create(['>= 5.0', '<= 5.1.1'])
|
8
|
+
require 'active_record_association_query_economizer/rails5/associations'
|
9
|
+
else
|
10
|
+
puts "active_record_association_query_economizer doesn't yet do anything on ActiveRecord #{activerecord_version}"
|
11
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'active_record/associations/builder/association'
|
2
|
+
require 'active_record/associations/preloader/association'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module Associations
|
6
|
+
module Builder
|
7
|
+
class Association #:nodoc:
|
8
|
+
self.valid_options += [:preload_if]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
class Preloader
|
12
|
+
class Association #:nodoc:
|
13
|
+
def preload_filters
|
14
|
+
[options[:preload_if]].flatten.compact
|
15
|
+
end
|
16
|
+
|
17
|
+
def owners_filtered
|
18
|
+
# owners_by_key is: {1=>[#<SourceModel ...>, 2=>[#<SourceModel ...>], ...}
|
19
|
+
owners_by_key.select do |key, records|
|
20
|
+
preload_filters.each do |filter|
|
21
|
+
case filter
|
22
|
+
when Proc
|
23
|
+
records.select!(&filter)
|
24
|
+
when Symbol
|
25
|
+
records.select! { |record| record.send(filter) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
records.present?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
remove_method :associated_records_by_owner
|
33
|
+
def associated_records_by_owner(preloader)
|
34
|
+
owners_map = owners_filtered
|
35
|
+
owner_keys = owners_map.keys.compact
|
36
|
+
|
37
|
+
# Each record may have multiple owners, and vice-versa
|
38
|
+
records_by_owner = owners.each_with_object({}) do |owner,h|
|
39
|
+
h[owner] = []
|
40
|
+
end
|
41
|
+
|
42
|
+
if owner_keys.any?
|
43
|
+
# Some databases impose a limit on the number of ids in a list (in Oracle it's 1000)
|
44
|
+
# Make several smaller queries if necessary or make one query if the adapter supports it
|
45
|
+
sliced = owner_keys.each_slice(klass.connection.in_clause_length || owner_keys.size)
|
46
|
+
|
47
|
+
records = load_slices sliced
|
48
|
+
records.each do |record, owner_key|
|
49
|
+
owners_map[owner_key].each do |owner|
|
50
|
+
records_by_owner[owner] << record
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
records_by_owner
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'active_record/associations/builder/association'
|
2
|
+
require 'active_record/associations/preloader/association'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module Associations
|
6
|
+
module Builder
|
7
|
+
class Association #:nodoc:
|
8
|
+
VALID_OPTIONS += [:preload_if]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Preloader
|
13
|
+
class Association #:nodoc:
|
14
|
+
def preload_filters
|
15
|
+
[options[:preload_if]].flatten.compact
|
16
|
+
end
|
17
|
+
|
18
|
+
def owners_filtered
|
19
|
+
unless defined?(@owners_filtered)
|
20
|
+
@owners_filtered = owners.dup
|
21
|
+
preload_filters.each do |filter|
|
22
|
+
case filter
|
23
|
+
when Proc
|
24
|
+
@owners_filtered.select!(&filter)
|
25
|
+
when Symbol
|
26
|
+
@owners_filtered.select! { |record| record.send(filter) }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
@owners_filtered
|
31
|
+
end
|
32
|
+
|
33
|
+
remove_method :owner_keys
|
34
|
+
def owner_keys
|
35
|
+
unless defined?(@owner_keys)
|
36
|
+
@owner_keys = owners_filtered.map do |owner|
|
37
|
+
owner[owner_key_name]
|
38
|
+
end
|
39
|
+
@owner_keys.uniq!
|
40
|
+
@owner_keys.compact!
|
41
|
+
end
|
42
|
+
@owner_keys
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_association_query_economizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- toru.yagi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.2'
|
76
|
+
- - "<="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 5.1.1
|
79
|
+
type: :runtime
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '4.2'
|
86
|
+
- - "<="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 5.1.1
|
89
|
+
description:
|
90
|
+
email:
|
91
|
+
executables: []
|
92
|
+
extensions: []
|
93
|
+
extra_rdoc_files: []
|
94
|
+
files:
|
95
|
+
- ".gitignore"
|
96
|
+
- ".travis.yml"
|
97
|
+
- Gemfile
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- active_record_association_query_economizer.gemspec
|
102
|
+
- bin/console
|
103
|
+
- bin/setup
|
104
|
+
- gemfiles/activerecord-42.gemfile
|
105
|
+
- gemfiles/activerecord-50.gemfile
|
106
|
+
- gemfiles/activerecord-51.gemfile
|
107
|
+
- lib/active_record_association_query_economizer.rb
|
108
|
+
- lib/active_record_association_query_economizer/rails4-2/associations.rb
|
109
|
+
- lib/active_record_association_query_economizer/rails5/associations.rb
|
110
|
+
- lib/active_record_association_query_economizer/version.rb
|
111
|
+
homepage: https://github.com/negito6
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.6.10
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Optimize queries of ActiveRecord preload functions.
|
135
|
+
test_files: []
|