ar_lazy_preload 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 853f4f5f10ba5f9aa5e6530eb767f9d4063d62745b2b332776c7ebb44c753700
4
- data.tar.gz: ff2f6a626e8e944734dcab46eef9677bda77582027f1e687b27b616a5cdac0cb
3
+ metadata.gz: c318927cb52d775d3a85e91fb24b7102e9fcb8004ced72fc1482ec0c7c4d1269
4
+ data.tar.gz: 8ceb003e63266df93da708925b261fab06334758366ce68b361609c2dbd07682
5
5
  SHA512:
6
- metadata.gz: f873a03ba3abc319042f601e512b48b64d638f3b6358e722768e92745992c654f4aef229e792690c0c250563c9f28ae2d69fd77931c9af27db9a09f719f6341f
7
- data.tar.gz: 521c1bf8d19bc8bc2c5e71c8fd2918ac400102368016a9d6448b19426bcefc569b51fda0e13b205d86a872553f08a619a436fb3c7821bb4b866056c771065121
6
+ metadata.gz: 9d52b4b8293a9e0c2eaa47bf21b603d1066d7b18b738e724a3b16b20188a56326bd4a52d9397481ef81fcbec368e1929ff4679fe7cfb47ba3d704421eda2ad83
7
+ data.tar.gz: 716b102784e7c22119320d7cc5ae513dcb8f2097eb5dba31e50fbbe64940f96324eff7ed2410eea6171a899880b4fc81683d2b43233607ea12740693b8358b69
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # ArLazyPreload [![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](https://cultofmartians.com/tasks/activerecord-lazy-preload.html) [![Gem Version](https://badge.fury.io/rb/ar_lazy_preload.svg)](https://rubygems.org/gems/ar_lazy_preload) [![Build Status](https://github.com/DmitryTsepelev/ar_lazy_preload/actions/workflows/rspec.yml/badge.svg)](https://github.com/DmitryTsepelev/ar_lazy_preload/actions/workflows/rspec.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/00d04595661820dfba80/maintainability)](https://codeclimate.com/github/DmitryTsepelev/ar_lazy_preload/maintainability) [![Coverage Status](https://coveralls.io/repos/github/DmitryTsepelev/ar_lazy_preload/badge.svg?branch=master)](https://coveralls.io/github/DmitryTsepelev/ar_lazy_preload?branch=master)
1
+ # ArLazyPreload [![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](https://cultofmartians.com/tasks/activerecord-lazy-preload.html) [![Gem Version](https://badge.fury.io/rb/ar_lazy_preload.svg)](https://rubygems.org/gems/ar_lazy_preload) [![Build Status](https://github.com/DmitryTsepelev/ar_lazy_preload/actions/workflows/rspec.yml/badge.svg)](https://github.com/DmitryTsepelev/ar_lazy_preload/actions/workflows/rspec.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/00d04595661820dfba80/maintainability)](https://codeclimate.com/github/DmitryTsepelev/ar_lazy_preload/maintainability) [![Coverage Status](https://coveralls.io/repos/github/DmitryTsepelev/ar_lazy_preload/badge.svg?branch=master)](https://coveralls.io/github/DmitryTsepelev/ar_lazy_preload?branch=master) ![](https://ruby-gem-downloads-badge.herokuapp.com/ar_lazy_preload?type=total)
2
2
 
3
3
  **ArLazyPreload** is a gem that brings association lazy load functionality to your Rails applications. There is a number of built-in methods to solve [N+1 problem](https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations), but sometimes a list of associations to preload is not obvious–this is when you can get most of this gem.
4
4
 
@@ -7,12 +7,6 @@
7
7
  - **Perfect fit for GraphQL**. Define a list of associations to load at the top-level resolver and let the gem do its job
8
8
  - **Auto-preload support**. If you don't want to specify the association list–set `ArLazyPreload.config.auto_preload` to `true`
9
9
 
10
- <p align="center">
11
- <a href="https://evilmartians.com/?utm_source=ar_lazy_preload">
12
- <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
13
- </a>
14
- </p>
15
-
16
10
  ## Why should I use it?
17
11
 
18
12
  Lazy loading is super helpful when the list of associations to load is determined dynamically. For instance, in GraphQL this list comes from the API client, and you'll have to inspect the selection set to find out what associations are going to be used.
@@ -50,6 +44,8 @@ If you want to turn automatic preload off for a specific record, you can call `.
50
44
  users.first.skip_preload.posts # => SELECT * FROM posts WHERE user_id = ?
51
45
  ```
52
46
 
47
+ > *Warning* : Using the `ArLazyPreload.config.auto_preload` feature makes ArLazyPreload try to preload *every* association target throughout your app, and in any other gem that makes association target calls. When enabling the setting in an existing app, you may find some edge cases where previous working queries now fail, and you should test most of your app paths to ensure that there are no such issues.
48
+
53
49
  ### Relation auto preloading
54
50
 
55
51
  Another alternative for auto preloading is using relation `#preload_associations_lazily` method
@@ -81,6 +77,10 @@ Add this line to your application's Gemfile, and you're all set:
81
77
  gem "ar_lazy_preload"
82
78
  ```
83
79
 
80
+ ## Credits
81
+
82
+ Initially sponsored by [Evil Martians](http://evilmartians.com).
83
+
84
84
  ## License
85
85
 
86
86
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -6,16 +6,27 @@ module ArLazyPreload
6
6
  def self.included(base)
7
7
  base.class.delegate :lazy_preload, to: :all
8
8
  base.class.delegate :preload_associations_lazily, to: :all
9
+ base.after_create { try_setup_auto_preload_context }
9
10
  end
10
11
 
11
12
  attr_accessor :lazy_preload_context
12
13
 
13
14
  delegate :try_preload_lazily, to: :lazy_preload_context, allow_nil: true
14
15
 
16
+ def reload
17
+ super.tap { try_setup_auto_preload_context }
18
+ end
19
+
15
20
  def skip_preload
16
21
  lazy_preload_context&.records&.delete(self)
17
22
  self.lazy_preload_context = nil
18
23
  self
19
24
  end
25
+
26
+ private
27
+
28
+ def try_setup_auto_preload_context
29
+ ArLazyPreload::Context.register(records: [self]) if ArLazyPreload.config.auto_preload?
30
+ end
20
31
  end
21
32
  end
@@ -8,7 +8,7 @@ require "ar_lazy_preload/contexts/temporary_preload_config"
8
8
  module ArLazyPreload
9
9
  class Context
10
10
  # Initiates lazy preload context for given records
11
- def self.register(records:, association_tree:, auto_preload: false)
11
+ def self.register(records:, association_tree: nil, auto_preload: false)
12
12
  return if records.empty?
13
13
 
14
14
  if ArLazyPreload.config.auto_preload? || auto_preload
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArLazyPreload
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
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: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-29 00:00:00.000000000 Z
11
+ date: 2022-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  - !ruby/object:Gem::Version
228
228
  version: '0'
229
229
  requirements: []
230
- rubygems_version: 3.1.4
230
+ rubygems_version: 3.1.6
231
231
  signing_key:
232
232
  specification_version: 4
233
233
  summary: lazy_preload implementation for ActiveRecord models