grape-entity-preloader 0.1.0 → 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/CHANGELOG.md +4 -0
- data/README.md +3 -15
- data/lib/grape/entity/preloader/entity.rb +2 -5
- data/lib/grape/entity/preloader/version.rb +1 -1
- data/spec/n_plus_one_op_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1706cb12b11070d46f62c3b3a4e9aacb797e1aff8659ca4c41d2ee68306b5f1
|
4
|
+
data.tar.gz: 6a060769e40134c573d14b235def6800b931c1fa8b87e84e5d370ce735384e2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f17b3f692873737c4e9a55e9a971324b77d8263ff08e62cb3d71bee414487e949fe8e76a6d9b088bf2442c4ce266f11041ae7bd3d0394fb236a635f6a8bbb951
|
7
|
+
data.tar.gz: 0e7ba460164dcfad7736f3beb8947c4636d54f954227de6968db5836111171eb8b6b4126fa8348d303f29e15b840e9309c096da04c204a692a434bb419c88cec
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.0] - 2025-10-14 UTC
|
4
|
+
|
5
|
+
- ⚠️ [Broken] Remove `:grape_entity_preloader` option functionality, please use `Grape::Entity::Preloader.with_enable` or `Grape::Entity::Preloader.with_disable` instead.
|
6
|
+
|
3
7
|
## [0.1.0] - 2025-10-12 UTC
|
4
8
|
|
5
9
|
- Initial release
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Grape::Entity::Preloader
|
2
2
|
|
3
|
+
[](https://github.com/OuYangJinTing/grape-entity-preloader/actions/workflows/main.yml)
|
4
|
+
[](https://badge.fury.io/rb/grape-entity-preloader)
|
5
|
+
|
3
6
|
Grape::Entity::Preloader allows preload associations and callbacks for avoiding N+1 operations in Grape::Entity.
|
4
7
|
|
5
8
|
## Installation
|
@@ -30,21 +33,6 @@ Grape::Entity::Preloader.enabled!
|
|
30
33
|
#### Local Activation and Deactivation
|
31
34
|
|
32
35
|
You can control preloading for specific `represent` calls.
|
33
|
-
|
34
|
-
##### 1. Using options
|
35
|
-
|
36
|
-
Pass `grape_entity_preloader: :enabled` or `grape_entity_preloader: :disabled` to the options hash. This overrides the global setting.
|
37
|
-
|
38
|
-
```ruby
|
39
|
-
# Locally enable
|
40
|
-
MyEntity.represent(users, grape_entity_preloader: :enabled)
|
41
|
-
|
42
|
-
# Locally disable
|
43
|
-
MyEntity.represent(users, grape_entity_preloader: :disabled)
|
44
|
-
```
|
45
|
-
|
46
|
-
##### 2. Using a block
|
47
|
-
|
48
36
|
For a specific block of code, you can use `with_enable` or `with_disable`. This is useful in contexts like API endpoints or middlewares.
|
49
37
|
|
50
38
|
```ruby
|
@@ -10,11 +10,8 @@ module Grape
|
|
10
10
|
def represent(objects, options = {})
|
11
11
|
options = Grape::Entity::Options.new(options) unless options.is_a?(Grape::Entity::Options)
|
12
12
|
|
13
|
-
options
|
14
|
-
Preloader.
|
15
|
-
options[:grape_entity_preloader] = :finished
|
16
|
-
|
17
|
-
super(objects, options)
|
13
|
+
Preloader.new(root_exposures, objects, options).call if Preloader.enabled?
|
14
|
+
Preloader.with_disable { super(objects, options) }
|
18
15
|
end
|
19
16
|
end
|
20
17
|
end
|
data/spec/n_plus_one_op_spec.rb
CHANGED
@@ -121,7 +121,7 @@ RSpec.describe 'N+1 operation' do # rubocop:disable RSpec/DescribeClass
|
|
121
121
|
}
|
122
122
|
|
123
123
|
expect do
|
124
|
-
User::Entity.represent(users, options
|
124
|
+
User::Entity.represent(users, options)
|
125
125
|
end.to make_database_queries(count: 1)
|
126
126
|
.and make_database_queries(count: 1, matching: /user_books_by_association/)
|
127
127
|
|
@@ -129,7 +129,7 @@ RSpec.describe 'N+1 operation' do # rubocop:disable RSpec/DescribeClass
|
|
129
129
|
|
130
130
|
options[:only] << { books_by_association: [:tags_by_association] }
|
131
131
|
expect do
|
132
|
-
User::Entity.represent(users, options
|
132
|
+
User::Entity.represent(users, options)
|
133
133
|
end.to make_database_queries(count: 2)
|
134
134
|
.and make_database_queries(count: 1, matching: /user_books_by_association/)
|
135
135
|
.and make_database_queries(count: 1, matching: /book_tags_by_association/)
|