auto_preload 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/gem-push.yml +2 -2
- data/.solargraph.yml +25 -0
- data/CHANGELOG.md +23 -0
- data/Gemfile +2 -0
- data/lib/auto_preload/active_record.rb +17 -3
- data/lib/auto_preload/adapters/active_record.rb +2 -0
- data/lib/auto_preload/adapters/serializer.rb +5 -0
- data/lib/auto_preload/adapters.rb +1 -0
- data/lib/auto_preload/config.rb +1 -1
- data/lib/auto_preload/resolver.rb +6 -1
- data/lib/auto_preload/version.rb +2 -1
- data/lib/auto_preload.rb +8 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83981d329237a816c396eeeb96058bf601531562846d5d365b664a25cbc683b6
|
4
|
+
data.tar.gz: 7090bcf8922549d45cd8410a98d09d841a17cdbd822dae21e1f69d8abd918890
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61bb4dcb606073730f9edbf2d8888d9e28bdafaf2e22a409866a95bd771462ca83ce08a6c1828f14c65bb540d9f3e4bd3968e04bef3df40d23c49e3ab4e2b7f8
|
7
|
+
data.tar.gz: 4fff43c826eeb13787beb5f7b1c179c7d05045ea8090a91257c91264ff15f23144d8276137d0771a5f682952916cc8feeb2073b146b74397f1e6a267fe4d7316
|
data/.solargraph.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
---
|
2
|
+
include:
|
3
|
+
- "**/*.rb"
|
4
|
+
exclude:
|
5
|
+
- spec/**/*
|
6
|
+
- test/**/*
|
7
|
+
- vendor/**/*
|
8
|
+
- ".bundle/**/*"
|
9
|
+
require:
|
10
|
+
- activesupport
|
11
|
+
- activerecord
|
12
|
+
domains: []
|
13
|
+
reporters:
|
14
|
+
- rubocop
|
15
|
+
- require_not_found
|
16
|
+
- typecheck
|
17
|
+
formatter:
|
18
|
+
rubocop:
|
19
|
+
cops: safe
|
20
|
+
except: []
|
21
|
+
only: []
|
22
|
+
extra_args: []
|
23
|
+
require_paths: []
|
24
|
+
plugins: []
|
25
|
+
max_files: 5000
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [0.1.2] - 2022-12-10
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- Documentation to all methods
|
12
|
+
- `solargraph` gem for development
|
13
|
+
|
14
|
+
## [0.1.1] - 2022-11-30
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
- Prevent crash when passing a non-existing association
|
18
|
+
|
19
|
+
## [0.1.0] - 2022-11-30
|
20
|
+
|
21
|
+
### Added
|
22
|
+
|
23
|
+
- First release
|
data/Gemfile
CHANGED
@@ -1,32 +1,46 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module AutoPreload
|
4
|
+
# Extensions to ActiveRecord::Base
|
4
5
|
module ActiveRecord
|
5
6
|
extend ActiveSupport::Concern
|
6
7
|
|
7
8
|
included do
|
9
|
+
# @param [String, Array<String>] inclusions
|
10
|
+
# @param [Hash] options
|
11
|
+
# @return [ActiveRecord::Relation]
|
8
12
|
scope :auto_includes, lambda { |inclusions, options = {}|
|
9
13
|
if inclusions.present?
|
10
|
-
|
14
|
+
resolved = Resolver.new(options).resolve(self, inclusions)
|
15
|
+
resolved.empty? ? self : includes(*resolved)
|
11
16
|
else
|
12
17
|
self
|
13
18
|
end
|
14
19
|
}
|
20
|
+
# @param [String, Array<String>] inclusions
|
21
|
+
# @param [Hash] options
|
22
|
+
# @return [ActiveRecord::Relation]
|
15
23
|
scope :auto_preload, lambda { |inclusions, options = {}|
|
16
24
|
if inclusions.present?
|
17
|
-
|
25
|
+
resolved = Resolver.new(options).resolve(self, inclusions)
|
26
|
+
resolved.empty? ? self : preload(*resolved)
|
18
27
|
else
|
19
28
|
self
|
20
29
|
end
|
21
30
|
}
|
31
|
+
# @param [String, Array<String>] inclusions
|
32
|
+
# @param [Hash] options
|
33
|
+
# @return [ActiveRecord::Relation]
|
22
34
|
scope :auto_eager_load, lambda { |inclusions, options = {}|
|
23
35
|
if inclusions.present?
|
24
|
-
|
36
|
+
resolved = Resolver.new(options).resolve(self, inclusions)
|
37
|
+
resolved.empty? ? self : eager_load(*resolved)
|
25
38
|
else
|
26
39
|
self
|
27
40
|
end
|
28
41
|
}
|
29
42
|
|
43
|
+
# @return [nil, Array<Symbol>]
|
30
44
|
class_attribute :auto_preloadable
|
31
45
|
end
|
32
46
|
end
|
@@ -4,6 +4,8 @@ module AutoPreload
|
|
4
4
|
module Adapters
|
5
5
|
# This class takes a model and finds all the preloadable associations.
|
6
6
|
class ActiveRecord
|
7
|
+
# @param [ActiveRecord::Base] model
|
8
|
+
# @return [Array<ActiveRecord::Reflection>] The preloadable associations.
|
7
9
|
def resolve_preloadables(model, _options = {})
|
8
10
|
if model.auto_preloadable
|
9
11
|
model.auto_preloadable.map { |w| model.reflect_on_association(w) }.compact
|
@@ -24,6 +24,11 @@ module AutoPreload
|
|
24
24
|
end.compact
|
25
25
|
end
|
26
26
|
|
27
|
+
# @param model [ActiveRecord::Base]
|
28
|
+
# @param options [Hash]
|
29
|
+
# @option options [Boolean] :root
|
30
|
+
# @option options [ActiveModel::Serializer] :serializer
|
31
|
+
# @return [ActiveModel::Serializer]
|
27
32
|
def resolve_serializer(model, options = {})
|
28
33
|
if options[:root]
|
29
34
|
options[:serializer] || ActiveModel::Serializer.serializer_for(model)
|
data/lib/auto_preload/config.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module AutoPreload
|
4
4
|
# This class handles the gem configurations.
|
5
5
|
class Config
|
6
|
-
# @
|
6
|
+
# @return [AutoPreload::Adapters::ActiveRecord, AutoPreload::Adapters::Serializer] The adapter to use.
|
7
7
|
attr_writer :adapter
|
8
8
|
|
9
9
|
# @return [AutoPreload::Adapters::ActiveRecord, AutoPreload::Adapters::Serializer] The adapter to use.
|
@@ -3,8 +3,13 @@
|
|
3
3
|
module AutoPreload
|
4
4
|
# This class parses a string in the format "articles,comments" and returns an array of symbols.
|
5
5
|
class Resolver
|
6
|
+
# Default max number of iterations
|
7
|
+
# @return [Integer]
|
6
8
|
MAX_ITERATIONS = 100
|
7
9
|
|
10
|
+
# @param options [Hash]
|
11
|
+
# @option options [Integer] :max_iterations
|
12
|
+
# @option options [AutoPreload::Adapters::ActiveRecord, AutoPreload::Adapters::Serializer] :adapter
|
8
13
|
def initialize(options = {})
|
9
14
|
@iterations = 0
|
10
15
|
@options = options
|
@@ -43,7 +48,7 @@ module AutoPreload
|
|
43
48
|
objects.present? ? (symbols << objects) : symbols
|
44
49
|
end
|
45
50
|
|
46
|
-
# @param
|
51
|
+
# @param objects [Array<Hash>]
|
47
52
|
# @return [Hash]
|
48
53
|
def merge(objects)
|
49
54
|
objects.reduce({}) do |result, object|
|
data/lib/auto_preload/version.rb
CHANGED
data/lib/auto_preload.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require "active_support"
|
4
4
|
require "active_record"
|
5
5
|
|
6
|
+
# Provides methods to run `preload`/`includes`/`eager_load` on your model
|
7
|
+
# from a JSON::API include string.
|
6
8
|
module AutoPreload
|
7
9
|
extend ActiveSupport::Autoload
|
8
10
|
|
@@ -11,13 +13,18 @@ module AutoPreload
|
|
11
13
|
autoload :Resolver
|
12
14
|
autoload :Config
|
13
15
|
|
16
|
+
# @yield [AutoPreload::Config]
|
17
|
+
# @return [AutoPreload::Config]
|
14
18
|
def self.configure
|
15
19
|
yield(config)
|
16
20
|
end
|
17
21
|
|
22
|
+
# @return [AutoPreload::Config]
|
18
23
|
def self.config
|
19
24
|
@config ||= Config.new
|
20
25
|
end
|
21
26
|
end
|
22
27
|
|
23
|
-
|
28
|
+
# rubocop:disable Lint/SendWithMixinArgument
|
29
|
+
ActiveRecord::Base.send(:include, AutoPreload::ActiveRecord)
|
30
|
+
# rubocop:enable Lint/SendWithMixinArgument
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auto_preload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mònade
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11
|
11
|
+
date: 2022-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -64,6 +64,8 @@ files:
|
|
64
64
|
- ".gitignore"
|
65
65
|
- ".rspec"
|
66
66
|
- ".rubocop.yml"
|
67
|
+
- ".solargraph.yml"
|
68
|
+
- CHANGELOG.md
|
67
69
|
- Gemfile
|
68
70
|
- LICENSE
|
69
71
|
- README.md
|