smart_preloads 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +106 -0
- data/LICENSE.txt +20 -0
- data/README.md +61 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/gemfiles/3.2.22.5.gemfile +7 -0
- data/gemfiles/4.0.gemfile +7 -0
- data/gemfiles/4.1.gemfile +7 -0
- data/gemfiles/5.0.gemfile +7 -0
- data/lib/smart_preloads/adapters/active_record_adapter.rb +11 -0
- data/lib/smart_preloads/item.rb +26 -0
- data/lib/smart_preloads/list.rb +39 -0
- data/lib/smart_preloads/loader.rb +38 -0
- data/lib/smart_preloads.rb +7 -0
- data/smart_preloads.gemspec +79 -0
- data/spec/smart_preloads_spec.rb +81 -0
- data/spec/spec_helper.rb +63 -0
- metadata +164 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5203b8b2b5dbd92bc5643f6688a3e73571150d85
|
4
|
+
data.tar.gz: 504790d10fa6b9e43733d5e8ae012fcf2c8e5c03
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 76ca0adc8537ffcf01e1459185ecd2c0143f65d6b74d060d478c0289a310e35a4befaf9b793758d1ff4ae486c11266eba430bfe38edfdc4d96d378915a646fe1
|
7
|
+
data.tar.gz: d5d259616f5b62597f3eeeeedfa2c7191b04ccee0238817ca2a8b44897472a985e5f586469a57199859dbc1793110adf4766db857ca36c1900895d8df7c781e7
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.3
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
# Add dependencies to develop your gem here.
|
4
|
+
# Include everything needed to run rake, tests, features, etc.
|
5
|
+
group :development do
|
6
|
+
gem 'rspec', '~> 3.3'
|
7
|
+
gem 'rdoc', '~> 3.12'
|
8
|
+
gem 'bundler', '~> 1.0'
|
9
|
+
gem 'jeweler', '~> 2.1.0'
|
10
|
+
gem 'simplecov', '>= 0'
|
11
|
+
gem 'sqlite3'
|
12
|
+
gem 'activerecord'
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (4.2.6)
|
5
|
+
activesupport (= 4.2.6)
|
6
|
+
builder (~> 3.1)
|
7
|
+
activerecord (4.2.6)
|
8
|
+
activemodel (= 4.2.6)
|
9
|
+
activesupport (= 4.2.6)
|
10
|
+
arel (~> 6.0)
|
11
|
+
activesupport (4.2.6)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
json (~> 1.7, >= 1.7.7)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
addressable (2.5.0)
|
18
|
+
public_suffix (~> 2.0, >= 2.0.2)
|
19
|
+
arel (6.0.3)
|
20
|
+
builder (3.2.2)
|
21
|
+
descendants_tracker (0.0.4)
|
22
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
23
|
+
diff-lcs (1.2.5)
|
24
|
+
docile (1.1.5)
|
25
|
+
faraday (0.9.2)
|
26
|
+
multipart-post (>= 1.2, < 3)
|
27
|
+
git (1.3.0)
|
28
|
+
github_api (0.11.3)
|
29
|
+
addressable (~> 2.3)
|
30
|
+
descendants_tracker (~> 0.0.1)
|
31
|
+
faraday (~> 0.8, < 0.10)
|
32
|
+
hashie (>= 1.2)
|
33
|
+
multi_json (>= 1.7.5, < 2.0)
|
34
|
+
nokogiri (~> 1.6.0)
|
35
|
+
oauth2
|
36
|
+
hashie (3.4.6)
|
37
|
+
highline (1.7.8)
|
38
|
+
i18n (0.7.0)
|
39
|
+
jeweler (2.1.2)
|
40
|
+
builder
|
41
|
+
bundler (>= 1.0)
|
42
|
+
git (>= 1.2.5)
|
43
|
+
github_api (~> 0.11.0)
|
44
|
+
highline (>= 1.6.15)
|
45
|
+
nokogiri (>= 1.5.10)
|
46
|
+
rake
|
47
|
+
rdoc
|
48
|
+
semver
|
49
|
+
json (1.8.3)
|
50
|
+
jwt (1.5.6)
|
51
|
+
mini_portile2 (2.1.0)
|
52
|
+
minitest (5.9.0)
|
53
|
+
multi_json (1.12.1)
|
54
|
+
multi_xml (0.6.0)
|
55
|
+
multipart-post (2.0.0)
|
56
|
+
nokogiri (1.6.8.1)
|
57
|
+
mini_portile2 (~> 2.1.0)
|
58
|
+
oauth2 (1.2.0)
|
59
|
+
faraday (>= 0.8, < 0.10)
|
60
|
+
jwt (~> 1.0)
|
61
|
+
multi_json (~> 1.3)
|
62
|
+
multi_xml (~> 0.5)
|
63
|
+
rack (>= 1.2, < 3)
|
64
|
+
public_suffix (2.0.4)
|
65
|
+
rack (2.0.1)
|
66
|
+
rake (12.0.0)
|
67
|
+
rdoc (3.12.2)
|
68
|
+
json (~> 1.4)
|
69
|
+
rspec (3.5.0)
|
70
|
+
rspec-core (~> 3.5.0)
|
71
|
+
rspec-expectations (~> 3.5.0)
|
72
|
+
rspec-mocks (~> 3.5.0)
|
73
|
+
rspec-core (3.5.4)
|
74
|
+
rspec-support (~> 3.5.0)
|
75
|
+
rspec-expectations (3.5.0)
|
76
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
77
|
+
rspec-support (~> 3.5.0)
|
78
|
+
rspec-mocks (3.5.0)
|
79
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
80
|
+
rspec-support (~> 3.5.0)
|
81
|
+
rspec-support (3.5.0)
|
82
|
+
semver (1.0.1)
|
83
|
+
simplecov (0.11.2)
|
84
|
+
docile (~> 1.1.0)
|
85
|
+
json (~> 1.8)
|
86
|
+
simplecov-html (~> 0.10.0)
|
87
|
+
simplecov-html (0.10.0)
|
88
|
+
sqlite3 (1.3.11)
|
89
|
+
thread_safe (0.3.5)
|
90
|
+
tzinfo (1.2.2)
|
91
|
+
thread_safe (~> 0.1)
|
92
|
+
|
93
|
+
PLATFORMS
|
94
|
+
ruby
|
95
|
+
|
96
|
+
DEPENDENCIES
|
97
|
+
activerecord
|
98
|
+
bundler (~> 1.0)
|
99
|
+
jeweler (~> 2.1.0)
|
100
|
+
rdoc (~> 3.12)
|
101
|
+
rspec (~> 3.3)
|
102
|
+
simplecov
|
103
|
+
sqlite3
|
104
|
+
|
105
|
+
BUNDLED WITH
|
106
|
+
1.13.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2017 Diego Aguir Selzlein
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# smart_preloads
|
2
|
+
|
3
|
+
Avoid N + 1 queries without having to worry about it at all! It is also useful
|
4
|
+
when you do not have control over which associations are going to be used by
|
5
|
+
the view, like when you provide users customizable views with
|
6
|
+
[Liquid](https://github.com/chamnap/liquid-rails).
|
7
|
+
|
8
|
+
## How it Works
|
9
|
+
|
10
|
+
You have to call `smart_preloads` at the end of your association. This will
|
11
|
+
generate a smart list of items that will load associations **if and when**
|
12
|
+
they are needed.
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
@authors = Author.all.smart_preloads
|
16
|
+
|
17
|
+
@authors.each do |author|
|
18
|
+
puts author.name
|
19
|
+
end
|
20
|
+
#=> SELECT "authors".* FROM "authors"
|
21
|
+
|
22
|
+
@authors.each do |author|
|
23
|
+
author.books.each do |book|
|
24
|
+
puts "#{author.name} authored #{book.name}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
#=> SELECT "books".* FROM "books" WHERE "books"."author_id" IN (1, 2)
|
28
|
+
```
|
29
|
+
|
30
|
+
Note that when `books` is called for the first record, that association will be
|
31
|
+
loaded for all records at once. This works for nested associations too:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
@authors.each do |author|
|
35
|
+
author.books.each do |book|
|
36
|
+
puts "#{author.name} authored #{book.name} (#{book.category.name})"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
#=> SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
|
40
|
+
```
|
41
|
+
|
42
|
+
## Installation
|
43
|
+
|
44
|
+
You can use `gem install smart_preloads` to install it manually or use Bundler:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
gem 'smart_preloads'
|
48
|
+
```
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
* Fork the project.
|
53
|
+
* Start a feature/bugfix branch.
|
54
|
+
* Commit and push until you are happy with your contribution.
|
55
|
+
* Make sure to add tests for it. This is important so I don't break it in a
|
56
|
+
future version unintentionally.
|
57
|
+
* Create a pull request.
|
58
|
+
|
59
|
+
## Copyright
|
60
|
+
|
61
|
+
Copyright (c) 2017 Diego Aguir Selzlein. See LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification...
|
17
|
+
# see http://guides.rubygems.org/specification-reference/ for more options
|
18
|
+
gem.name = 'smart_preloads'
|
19
|
+
gem.homepage = 'http://github.com/nerde/smart_preloads'
|
20
|
+
gem.license = 'MIT'
|
21
|
+
gem.summary = 'Smart association loading for ActiveRecord.'
|
22
|
+
gem.description = 'Avoid N + 1 queries without having to worry about it at all!'
|
23
|
+
gem.email = 'diegoselzlein@gmail.com'
|
24
|
+
gem.authors = ['Diego Aguir Selzlein']
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Code coverage detail'
|
35
|
+
task :simplecov do
|
36
|
+
ENV['COVERAGE'] = 'true'
|
37
|
+
Rake::Task['spec'].execute
|
38
|
+
end
|
39
|
+
|
40
|
+
task default: :spec
|
41
|
+
|
42
|
+
require 'rdoc/task'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "smart_preloads #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SmartPreloads
|
2
|
+
class Item < SimpleDelegator
|
3
|
+
def initialize(record, loader: nil)
|
4
|
+
super(record)
|
5
|
+
@loader = loader || Loader.new([record])
|
6
|
+
|
7
|
+
record.class.reflections.each do |key, association|
|
8
|
+
original = method(key)
|
9
|
+
singleton_class.class_exec do
|
10
|
+
body = nil
|
11
|
+
if %i(has_many has_and_belongs_to_many).include?(association.macro)
|
12
|
+
body = lambda do
|
13
|
+
List.new(original.call, loader: @loader.nested(key.to_sym))
|
14
|
+
end
|
15
|
+
else
|
16
|
+
body = lambda do
|
17
|
+
@loader.load(key.to_sym)
|
18
|
+
original.call
|
19
|
+
end
|
20
|
+
end
|
21
|
+
define_method(key, &body)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module SmartPreloads
|
2
|
+
class List
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(collection, loader: nil)
|
6
|
+
@collection = collection
|
7
|
+
@loader = loader || Loader.new(@collection)
|
8
|
+
end
|
9
|
+
|
10
|
+
def each
|
11
|
+
loaded_collection.each do |resource|
|
12
|
+
yield Item.new(resource, loader: @loader)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def ==(other)
|
17
|
+
to_a == other
|
18
|
+
end
|
19
|
+
|
20
|
+
def size
|
21
|
+
to_a.size
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def loaded_collection
|
27
|
+
@loaded_collection ||= fetch_loaded_collection
|
28
|
+
end
|
29
|
+
|
30
|
+
def fetch_loaded_collection
|
31
|
+
preload_defaults
|
32
|
+
@collection
|
33
|
+
end
|
34
|
+
|
35
|
+
def preload_defaults
|
36
|
+
@loader.load_default
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module SmartPreloads
|
2
|
+
class Loader
|
3
|
+
attr_reader :collection
|
4
|
+
|
5
|
+
def initialize(collection, nested_associations = nil)
|
6
|
+
@collection = collection
|
7
|
+
@nested_associations = nested_associations
|
8
|
+
end
|
9
|
+
|
10
|
+
def load_default
|
11
|
+
load(nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
def load(association)
|
15
|
+
association = nest(association)
|
16
|
+
return unless association
|
17
|
+
ActiveRecord::Associations::Preloader.new.preload(@collection, association)
|
18
|
+
end
|
19
|
+
|
20
|
+
def nested(nested_associations)
|
21
|
+
self.class.new(@collection, Array(@nested_associations) +
|
22
|
+
Array(nested_associations))
|
23
|
+
end
|
24
|
+
|
25
|
+
def nest(association)
|
26
|
+
return association unless @nested_associations
|
27
|
+
result = association
|
28
|
+
Array(@nested_associations).reverse.each do |parent|
|
29
|
+
if result
|
30
|
+
result = { parent => result }
|
31
|
+
else
|
32
|
+
result = parent
|
33
|
+
end
|
34
|
+
end
|
35
|
+
result
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: smart_preloads 0.0.1 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "smart_preloads".freeze
|
9
|
+
s.version = "0.0.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Diego Aguir Selzlein".freeze]
|
14
|
+
s.date = "2017-05-02"
|
15
|
+
s.description = "Avoid N + 1 queries without having to worry about it at all!".freeze
|
16
|
+
s.email = "diegoselzlein@gmail.com".freeze
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
".ruby-version",
|
25
|
+
"Gemfile",
|
26
|
+
"Gemfile.lock",
|
27
|
+
"LICENSE.txt",
|
28
|
+
"README.md",
|
29
|
+
"Rakefile",
|
30
|
+
"VERSION",
|
31
|
+
"gemfiles/3.2.22.5.gemfile",
|
32
|
+
"gemfiles/4.0.gemfile",
|
33
|
+
"gemfiles/4.1.gemfile",
|
34
|
+
"gemfiles/5.0.gemfile",
|
35
|
+
"lib/smart_preloads.rb",
|
36
|
+
"lib/smart_preloads/adapters/active_record_adapter.rb",
|
37
|
+
"lib/smart_preloads/item.rb",
|
38
|
+
"lib/smart_preloads/list.rb",
|
39
|
+
"lib/smart_preloads/loader.rb",
|
40
|
+
"smart_preloads.gemspec",
|
41
|
+
"spec/smart_preloads_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
s.homepage = "http://github.com/nerde/smart_preloads".freeze
|
45
|
+
s.licenses = ["MIT".freeze]
|
46
|
+
s.rubygems_version = "2.5.2".freeze
|
47
|
+
s.summary = "Smart association loading for ActiveRecord.".freeze
|
48
|
+
|
49
|
+
if s.respond_to? :specification_version then
|
50
|
+
s.specification_version = 4
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_development_dependency(%q<rspec>.freeze, ["~> 3.3"])
|
54
|
+
s.add_development_dependency(%q<rdoc>.freeze, ["~> 3.12"])
|
55
|
+
s.add_development_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
56
|
+
s.add_development_dependency(%q<jeweler>.freeze, ["~> 2.1.0"])
|
57
|
+
s.add_development_dependency(%q<simplecov>.freeze, [">= 0"])
|
58
|
+
s.add_development_dependency(%q<sqlite3>.freeze, [">= 0"])
|
59
|
+
s.add_development_dependency(%q<activerecord>.freeze, [">= 0"])
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rspec>.freeze, ["~> 3.3"])
|
62
|
+
s.add_dependency(%q<rdoc>.freeze, ["~> 3.12"])
|
63
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
64
|
+
s.add_dependency(%q<jeweler>.freeze, ["~> 2.1.0"])
|
65
|
+
s.add_dependency(%q<simplecov>.freeze, [">= 0"])
|
66
|
+
s.add_dependency(%q<sqlite3>.freeze, [">= 0"])
|
67
|
+
s.add_dependency(%q<activerecord>.freeze, [">= 0"])
|
68
|
+
end
|
69
|
+
else
|
70
|
+
s.add_dependency(%q<rspec>.freeze, ["~> 3.3"])
|
71
|
+
s.add_dependency(%q<rdoc>.freeze, ["~> 3.12"])
|
72
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
73
|
+
s.add_dependency(%q<jeweler>.freeze, ["~> 2.1.0"])
|
74
|
+
s.add_dependency(%q<simplecov>.freeze, [">= 0"])
|
75
|
+
s.add_dependency(%q<sqlite3>.freeze, [">= 0"])
|
76
|
+
s.add_dependency(%q<activerecord>.freeze, [">= 0"])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class Category < ActiveRecord::Base
|
4
|
+
has_many :books
|
5
|
+
end
|
6
|
+
|
7
|
+
class Author < ActiveRecord::Base
|
8
|
+
has_many :books
|
9
|
+
end
|
10
|
+
|
11
|
+
class Book < ActiveRecord::Base
|
12
|
+
belongs_to :author
|
13
|
+
belongs_to :category
|
14
|
+
has_many :tags, as: :taggable
|
15
|
+
end
|
16
|
+
|
17
|
+
class Tag < ActiveRecord::Base
|
18
|
+
belongs_to :taggable, polymorphic: true
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'SmartPreloads' do
|
22
|
+
it 'smartly loads has_many associations' do
|
23
|
+
Book.create!(name: 'Rework')
|
24
|
+
Book.create!(name: 'Lean Startup')
|
25
|
+
|
26
|
+
list = Book.all.smart_preloads.to_a
|
27
|
+
|
28
|
+
expect(list.size).to eq(2)
|
29
|
+
expect(list.first.association(:tags)).to_not be_loaded
|
30
|
+
|
31
|
+
list.first.tags
|
32
|
+
|
33
|
+
expect(list.first.association(:tags)).to_not be_loaded
|
34
|
+
|
35
|
+
list.first.tags.first # forcing it to be loaded in the first item
|
36
|
+
|
37
|
+
expect(list.first.association(:tags)).to be_loaded
|
38
|
+
expect(list.last.association(:tags)).to be_loaded
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'smartly loads belongs_to associations' do
|
42
|
+
Book.create!(name: 'Rework')
|
43
|
+
Book.create!(name: 'Lean Startup')
|
44
|
+
|
45
|
+
list = Book.all.smart_preloads.to_a
|
46
|
+
|
47
|
+
expect(list.size).to eq(2)
|
48
|
+
expect(list.first.association(:author)).to_not be_loaded
|
49
|
+
|
50
|
+
list.first.author # forcing it to be loaded in the first item
|
51
|
+
|
52
|
+
expect(list.first.association(:author)).to be_loaded
|
53
|
+
expect(list.last.association(:author)).to be_loaded
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'smartly loads nested associations' do
|
57
|
+
a1 = Author.create!(name: 'John')
|
58
|
+
a2 = Author.create!(name: 'Robb')
|
59
|
+
c1 = Category.create!(name: 'Humor')
|
60
|
+
c2 = Category.create!(name: 'Horror')
|
61
|
+
Book.create!(name: 'Rework', author_id: a1.id, category_id: c1.id)
|
62
|
+
Book.create!(name: 'Lean Startup', author_id: a2.id, category_id: c2.id)
|
63
|
+
|
64
|
+
list = Author.all.smart_preloads.to_a
|
65
|
+
|
66
|
+
expect(list.size).to eq(2)
|
67
|
+
expect(list.first.association(:books)).to_not be_loaded
|
68
|
+
|
69
|
+
list.first.books.first # forcing it to be loaded in the first item
|
70
|
+
|
71
|
+
expect(list.first.association(:books)).to be_loaded
|
72
|
+
expect(list.last.association(:books)).to be_loaded
|
73
|
+
expect(list.first.books.first.association(:category)).to_not be_loaded
|
74
|
+
expect(list.last.books.first.association(:category)).to_not be_loaded
|
75
|
+
|
76
|
+
list.first.books.first.category # forcing nested association to be loaded
|
77
|
+
|
78
|
+
expect(list.first.books.first.association(:category)).to be_loaded
|
79
|
+
expect(list.last.books.first.association(:category)).to be_loaded
|
80
|
+
end
|
81
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
if ENV['COVERAGE']
|
2
|
+
require 'simplecov'
|
3
|
+
|
4
|
+
module SimpleCov::Configuration
|
5
|
+
def clean_filters
|
6
|
+
@filters = []
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
SimpleCov.configure do
|
11
|
+
clean_filters
|
12
|
+
load_profile 'test_frameworks'
|
13
|
+
end
|
14
|
+
|
15
|
+
SimpleCov.start do
|
16
|
+
add_filter '/.rvm/'
|
17
|
+
add_filter '/.rbenv/'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
21
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
22
|
+
|
23
|
+
require 'active_record'
|
24
|
+
require 'smart_preloads'
|
25
|
+
require 'rspec'
|
26
|
+
|
27
|
+
# Requires supporting files with custom matchers and macros, etc,
|
28
|
+
# in ./support/ and its subdirectories.
|
29
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
30
|
+
|
31
|
+
RSpec.configure do |config|
|
32
|
+
config.before(:suite) do
|
33
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3',
|
34
|
+
database: ':memory:'
|
35
|
+
ActiveRecord::Schema.define do
|
36
|
+
create_table :books do |t|
|
37
|
+
t.string :name
|
38
|
+
t.integer :category_id
|
39
|
+
t.integer :author_id
|
40
|
+
end
|
41
|
+
|
42
|
+
create_table :authors do |t|
|
43
|
+
t.string :name
|
44
|
+
end
|
45
|
+
|
46
|
+
create_table :categories do |t|
|
47
|
+
t.string :name
|
48
|
+
end
|
49
|
+
|
50
|
+
create_table :tags do |t|
|
51
|
+
t.string :name
|
52
|
+
t.belongs_to :taggable, polymorphic: true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
config.around(:each) do |example|
|
58
|
+
ActiveRecord::Base.transaction do
|
59
|
+
example.run
|
60
|
+
raise ActiveRecord::Rollback
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_preloads
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Diego Aguir Selzlein
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rdoc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: jeweler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.1.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.1.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
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: sqlite3
|
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: activerecord
|
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: Avoid N + 1 queries without having to worry about it at all!
|
112
|
+
email: diegoselzlein@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files:
|
116
|
+
- LICENSE.txt
|
117
|
+
- README.md
|
118
|
+
files:
|
119
|
+
- ".document"
|
120
|
+
- ".rspec"
|
121
|
+
- ".ruby-version"
|
122
|
+
- Gemfile
|
123
|
+
- Gemfile.lock
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- VERSION
|
128
|
+
- gemfiles/3.2.22.5.gemfile
|
129
|
+
- gemfiles/4.0.gemfile
|
130
|
+
- gemfiles/4.1.gemfile
|
131
|
+
- gemfiles/5.0.gemfile
|
132
|
+
- lib/smart_preloads.rb
|
133
|
+
- lib/smart_preloads/adapters/active_record_adapter.rb
|
134
|
+
- lib/smart_preloads/item.rb
|
135
|
+
- lib/smart_preloads/list.rb
|
136
|
+
- lib/smart_preloads/loader.rb
|
137
|
+
- smart_preloads.gemspec
|
138
|
+
- spec/smart_preloads_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
homepage: http://github.com/nerde/smart_preloads
|
141
|
+
licenses:
|
142
|
+
- MIT
|
143
|
+
metadata: {}
|
144
|
+
post_install_message:
|
145
|
+
rdoc_options: []
|
146
|
+
require_paths:
|
147
|
+
- lib
|
148
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
requirements: []
|
159
|
+
rubyforge_project:
|
160
|
+
rubygems_version: 2.5.2
|
161
|
+
signing_key:
|
162
|
+
specification_version: 4
|
163
|
+
summary: Smart association loading for ActiveRecord.
|
164
|
+
test_files: []
|