ar_lazy_preload 0.1.1 → 0.2.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 +4 -4
- data/README.md +20 -6
- data/lib/ar_lazy_preload/configuration.rb +17 -0
- data/lib/ar_lazy_preload/context.rb +5 -1
- data/lib/ar_lazy_preload/version.rb +1 -1
- data/lib/ar_lazy_preload.rb +7 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88bbf9af048997996a06aa5292f648fc9a5583a3335a4c088451bc7cb883f33f
|
4
|
+
data.tar.gz: 784ef74c3ab97149fb237c52ce663be4d0c7f5254e2181e1967b3f8b50e37949
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bda01ddd3c35f9b1e4f03966566a8c14fcffe9c64b69a9b49227b6ff481bf73d348d0576aed8919e9363ab2914fa001a1ba3fd7da5569767ea714d38704d4cc
|
7
|
+
data.tar.gz: 15769ecfdd7a41f1860483aab2418c1c2f3167bcb2f67c491f120ac3d4cb00d5bb53b3540f0a3574a575f9062181f7a8c2b16fa50a4265f3e45bc032f9f63e47
|
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[](https://cultofmartians.com/tasks/activerecord-lazy-preload.html)
|
2
|
+
[](https://rubygems.org/gems/ar_lazy_preload)
|
1
3
|
[](https://travis-ci.org/DmitryTsepelev/ar_lazy_preload)
|
2
4
|
[](https://codeclimate.com/github/DmitryTsepelev/ar_lazy_preload/maintainability)
|
3
5
|
[](https://coveralls.io/github/DmitryTsepelev/ar_lazy_preload?branch=master)
|
@@ -8,13 +10,23 @@ Lazy loading associations for the ActiveRecord models. `#includes`, `#eager_load
|
|
8
10
|
|
9
11
|
This gem allows to set up _lazy_ preloading for associations - it won't load anything until association is called for a first time, but when it happens - it loads all the associated records for all records from the initial relation in a single query.
|
10
12
|
|
11
|
-
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile, and you're all set:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem "ar_lazy_preload"
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
For example, if we define the following relation
|
12
24
|
|
13
25
|
```ruby
|
14
26
|
users = User.lazy_preload(:posts).limit(10)
|
15
27
|
```
|
16
28
|
|
17
|
-
and use it in
|
29
|
+
and use it in the following way
|
18
30
|
|
19
31
|
```ruby
|
20
32
|
users.map(&:first_name)
|
@@ -26,7 +38,7 @@ there will be one query because we've never accessed posts:
|
|
26
38
|
SELECT * FROM users LIMIT 10
|
27
39
|
```
|
28
40
|
|
29
|
-
|
41
|
+
However, when we try to load posts
|
30
42
|
|
31
43
|
```ruby
|
32
44
|
users.map(&:posts)
|
@@ -38,13 +50,15 @@ there will be one more request for posts:
|
|
38
50
|
SELECT * FROM posts WHERE user_id in (...)
|
39
51
|
```
|
40
52
|
|
41
|
-
##
|
53
|
+
## Auto preloading
|
42
54
|
|
43
|
-
|
55
|
+
If you want the gem to be even more lazy - you can configure it to load all the associations lazily without specifying them explicitly. In order to do that you'll need to change the configuration in the following way:
|
44
56
|
|
45
57
|
```ruby
|
46
|
-
|
58
|
+
ArLazyPreload.config.auto_preload = true
|
47
59
|
```
|
48
60
|
|
61
|
+
After that there is no need to call `lazy_preload` on the association, everything would be loaded lazily.
|
62
|
+
|
49
63
|
## License
|
50
64
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ArLazyPreload
|
4
|
+
# ArLazyPreload configuration:
|
5
|
+
#
|
6
|
+
# - `auto_preload` - load all the associations lazily without
|
7
|
+
# an explicit lazy_preload call
|
8
|
+
class Configuration
|
9
|
+
attr_accessor :auto_preload
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@auto_preload = false
|
13
|
+
end
|
14
|
+
|
15
|
+
alias auto_preload? auto_preload
|
16
|
+
end
|
17
|
+
end
|
@@ -11,7 +11,8 @@ module ArLazyPreload
|
|
11
11
|
class Context
|
12
12
|
# Initiates lazy preload context for given records
|
13
13
|
def self.register(records:, association_tree:)
|
14
|
-
return if records.empty? || association_tree.empty?
|
14
|
+
return if records.empty? || association_tree.empty? && !ArLazyPreload.config.auto_preload?
|
15
|
+
|
15
16
|
ArLazyPreload::Context.new(records: records, association_tree: association_tree)
|
16
17
|
end
|
17
18
|
|
@@ -30,6 +31,7 @@ module ArLazyPreload
|
|
30
31
|
# objects in the context it if needed.
|
31
32
|
def try_preload_lazily(association_name)
|
32
33
|
return unless association_needs_preload?(association_name)
|
34
|
+
|
33
35
|
preloader.preload(records, association_name)
|
34
36
|
AssociatedContextBuilder.prepare(parent_context: self, association_name: association_name)
|
35
37
|
end
|
@@ -37,6 +39,8 @@ module ArLazyPreload
|
|
37
39
|
private
|
38
40
|
|
39
41
|
def association_needs_preload?(association_name)
|
42
|
+
return true if ArLazyPreload.config.auto_preload?
|
43
|
+
|
40
44
|
association_tree.any? do |node|
|
41
45
|
if node.is_a?(Symbol)
|
42
46
|
node == association_name
|
data/lib/ar_lazy_preload.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "ar_lazy_preload/configuration"
|
3
4
|
require "ar_lazy_preload/ext/base"
|
4
5
|
require "ar_lazy_preload/ext/relation"
|
5
6
|
require "ar_lazy_preload/ext/association"
|
@@ -7,6 +8,12 @@ require "ar_lazy_preload/ext/merger"
|
|
7
8
|
require "ar_lazy_preload/ext/association_relation"
|
8
9
|
|
9
10
|
module ArLazyPreload
|
11
|
+
class << self
|
12
|
+
def config
|
13
|
+
@config ||= Configuration.new
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
10
17
|
ActiveRecord::Base.include(ArLazyPreload::Base)
|
11
18
|
|
12
19
|
ActiveRecord::Relation.prepend(ArLazyPreload::Relation)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_lazy_preload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- lib/ar_lazy_preload.rb
|
122
122
|
- lib/ar_lazy_preload/associated_context_builder.rb
|
123
123
|
- lib/ar_lazy_preload/association_tree_builder.rb
|
124
|
+
- lib/ar_lazy_preload/configuration.rb
|
124
125
|
- lib/ar_lazy_preload/context.rb
|
125
126
|
- lib/ar_lazy_preload/ext/association.rb
|
126
127
|
- lib/ar_lazy_preload/ext/association_relation.rb
|