grape-entity-preloader 0.2.0 → 0.3.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/lib/grape/entity/preloader/version.rb +1 -1
- data/lib/grape/entity/preloader.rb +22 -18
- data/spec/grape/entity/preloader_spec.rb +28 -0
- 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: 4435d853f0ac2438cbd9def7d195a78e4ce66dcfc0db4015b29f20c5102846b6
|
|
4
|
+
data.tar.gz: 2bde5dbb73a91201040bc0e21da30507638c1b61e164e494a6187dacc1ab328d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 36b9c7316b3cb852024039a2db9ffe0c55215f49c60614b49c95a23a1bf307432b8e564c8c09cd3b56b71a4ed5f212e5fbd756e87675901407acb8acd5a8b642
|
|
7
|
+
data.tar.gz: 8f9c9b1ed336765001069d34ab27e7b80e5a94d6a829ffd276ff501785ed15d4a8f43d9cdf762c6a79603487e16ddff00669e0a2222833c091e8ff8ebd7a6532
|
|
@@ -26,18 +26,20 @@ module Grape
|
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def self.with_enable
|
|
29
|
+
def self.with_enable # rubocop:disable Metrics/MethodLength
|
|
30
30
|
return yield if enabled?
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
begin
|
|
33
|
+
old_value = ActiveSupport::IsolatedExecutionState[:grape_entity_preloader]
|
|
34
|
+
ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = true
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
yield
|
|
37
|
+
ensure
|
|
38
|
+
if old_value.nil?
|
|
39
|
+
ActiveSupport::IsolatedExecutionState.delete(:grape_entity_preloader)
|
|
40
|
+
else
|
|
41
|
+
ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = old_value
|
|
42
|
+
end
|
|
41
43
|
end
|
|
42
44
|
end
|
|
43
45
|
|
|
@@ -49,18 +51,20 @@ module Grape
|
|
|
49
51
|
!enabled?
|
|
50
52
|
end
|
|
51
53
|
|
|
52
|
-
def self.with_disable
|
|
54
|
+
def self.with_disable # rubocop:disable Metrics/MethodLength
|
|
53
55
|
return yield if disabled?
|
|
54
56
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
begin
|
|
58
|
+
old_value = ActiveSupport::IsolatedExecutionState[:grape_entity_preloader]
|
|
59
|
+
ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = false
|
|
57
60
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
yield
|
|
62
|
+
ensure
|
|
63
|
+
if old_value.nil?
|
|
64
|
+
ActiveSupport::IsolatedExecutionState.delete(:grape_entity_preloader)
|
|
65
|
+
else
|
|
66
|
+
ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = old_value
|
|
67
|
+
end
|
|
64
68
|
end
|
|
65
69
|
end
|
|
66
70
|
|
|
@@ -4,4 +4,32 @@ RSpec.describe Grape::Entity::Preloader do
|
|
|
4
4
|
it 'has a version number' do
|
|
5
5
|
expect(Grape::Entity::Preloader::VERSION).not_to be_nil
|
|
6
6
|
end
|
|
7
|
+
|
|
8
|
+
describe '.with_enable' do
|
|
9
|
+
it 'returns the value of the block' do
|
|
10
|
+
result = described_class.with_enable { 42 }
|
|
11
|
+
expect(result).to eq(42)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'returns the value of the block when already enabled' do
|
|
15
|
+
described_class.enabled!
|
|
16
|
+
result = described_class.with_enable { 'hello' }
|
|
17
|
+
expect(result).to eq('hello')
|
|
18
|
+
ensure
|
|
19
|
+
described_class.disabled!
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
describe '.with_disable' do
|
|
24
|
+
it 'returns the value of the block' do
|
|
25
|
+
result = described_class.with_disable { 42 }
|
|
26
|
+
expect(result).to eq(42)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it 'returns the value of the block when already disabled' do
|
|
30
|
+
described_class.disabled!
|
|
31
|
+
result = described_class.with_disable { 'hello' }
|
|
32
|
+
expect(result).to eq('hello')
|
|
33
|
+
end
|
|
34
|
+
end
|
|
7
35
|
end
|
data/spec/n_plus_one_op_spec.rb
CHANGED
|
@@ -105,7 +105,7 @@ RSpec.describe 'N+1 operation' do # rubocop:disable RSpec/DescribeClass
|
|
|
105
105
|
User::Entity.represent(
|
|
106
106
|
users,
|
|
107
107
|
serializable: true,
|
|
108
|
-
only: [books_by_association: %i[tags_by_association tags_by_callback]]
|
|
108
|
+
only: [{ books_by_association: %i[tags_by_association tags_by_callback] }]
|
|
109
109
|
)
|
|
110
110
|
end.to make_database_queries(count: 3)
|
|
111
111
|
.and make_database_queries(count: 1, matching: /user_books_by_association/)
|
|
@@ -149,7 +149,7 @@ RSpec.describe 'N+1 operation' do # rubocop:disable RSpec/DescribeClass
|
|
|
149
149
|
User::Entity.represent(
|
|
150
150
|
users,
|
|
151
151
|
serializable: true,
|
|
152
|
-
only: [books_by_callback: %i[tags_by_association tags_by_callback]]
|
|
152
|
+
only: [{ books_by_callback: %i[tags_by_association tags_by_callback] }]
|
|
153
153
|
)
|
|
154
154
|
end.to make_database_queries(count: 3)
|
|
155
155
|
.and make_database_queries(count: 1, matching: /books_by_callback/)
|