cache_crispies 1.0.2 → 1.1.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/cache_crispies/controller.rb +18 -7
- data/lib/cache_crispies/version.rb +1 -1
- data/spec/cache_crispies/controller_spec.rb +31 -1
- 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: a30ba1db2e070cd13dac9ad8dcfd6fdfad46d0b70e6337732a505b89f6eb1154
|
4
|
+
data.tar.gz: 4b788edd3dcdbd55e0d65db463504b2483e6b48374f108e550a994dbd037a270
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43fb507ca5d1c7478d1fd19e60f55d8b18bcd3cc8a248561013e8607d5d035637bc477f4e4122b44920dde48f5b642bfaf95f5028504715c3a0a2a6ea26d76c8
|
7
|
+
data.tar.gz: bc578d6a8405ee5613a2644cd0ebf091e13f339108fc248edeb65c747ab2af290de914f05118356c14a52caad26594f9b90ca7a6081efa3696f72e8d89f741a0
|
@@ -16,16 +16,24 @@ module CacheCrispies
|
|
16
16
|
# CacheCrispies::Base
|
17
17
|
# @param cacheable [Object] can be any object. But is typically a Rails
|
18
18
|
# model inheriting from ActiveRecord::Base
|
19
|
-
# @param [
|
20
|
-
# @option options [Symbol] :key the name of the root key to nest the JSON
|
19
|
+
# @param key [Symbol] the name of the root key to nest the JSON
|
21
20
|
# data under
|
22
|
-
# @
|
21
|
+
# @param collection [Boolean] whether to render the data as a
|
23
22
|
# collection/array or a single object
|
24
|
-
# @
|
25
|
-
#
|
23
|
+
# @param status [Integer, Symbol] the HTTP response status code or
|
24
|
+
# Rails-supported symbol. See
|
26
25
|
# https://guides.rubyonrails.org/layouts_and_rendering.html#the-status-option
|
26
|
+
# @param meta [Hash] data to include as metadata under a root key
|
27
|
+
# @param meta_key [Symbol] they key to store the metadata under
|
28
|
+
# @param [Hash] options any optional values from the serializer instance
|
27
29
|
# @return [void]
|
28
|
-
def cache_render(
|
30
|
+
def cache_render(
|
31
|
+
serializer,
|
32
|
+
cacheable,
|
33
|
+
key: nil, collection: nil, status: nil,
|
34
|
+
meta: {}, meta_key: :meta,
|
35
|
+
**options
|
36
|
+
)
|
29
37
|
plan = CacheCrispies::Plan.new(
|
30
38
|
serializer,
|
31
39
|
cacheable,
|
@@ -48,7 +56,10 @@ module CacheCrispies
|
|
48
56
|
plan.cache { serializer.new(cacheable, options).as_json }
|
49
57
|
end
|
50
58
|
|
51
|
-
|
59
|
+
json_hash = plan.wrap(serializer_json)
|
60
|
+
json_hash[meta_key] = meta unless meta.empty?
|
61
|
+
|
62
|
+
render_hash = { json: Oj.dump(json_hash, mode: OJ_MODE) }
|
52
63
|
render_hash[:status] = status if status
|
53
64
|
|
54
65
|
render render_hash
|
@@ -24,7 +24,8 @@ describe CacheCrispies::Controller do
|
|
24
24
|
|
25
25
|
describe '#cache_render' do
|
26
26
|
let(:etags) { false }
|
27
|
-
let(:
|
27
|
+
let(:single_hash) { { cereal: { name: cereal_names.first } } }
|
28
|
+
let(:single_json) { single_hash.to_json }
|
28
29
|
let(:collection_json) {
|
29
30
|
{ cereals: cereal_names.map { |name| { name: name } } }.to_json
|
30
31
|
}
|
@@ -86,5 +87,34 @@ describe CacheCrispies::Controller do
|
|
86
87
|
)
|
87
88
|
end
|
88
89
|
end
|
90
|
+
|
91
|
+
context 'with a meta: option' do
|
92
|
+
it 'adds a meta data hash to the JSON' do
|
93
|
+
expect(subject).to receive(:render).with(
|
94
|
+
json: single_hash.merge(meta: { page: 42 }).to_json
|
95
|
+
)
|
96
|
+
|
97
|
+
subject.cache_render(
|
98
|
+
CerealSerializerForController,
|
99
|
+
collection.first,
|
100
|
+
meta: { page: 42 }
|
101
|
+
)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'with a meta_key: option' do
|
106
|
+
it 'adds a meta data hash to the JSON with the provided key' do
|
107
|
+
expect(subject).to receive(:render).with(
|
108
|
+
json: single_hash.merge(test_meta_data: { page: 42 }).to_json
|
109
|
+
)
|
110
|
+
|
111
|
+
subject.cache_render(
|
112
|
+
CerealSerializerForController,
|
113
|
+
collection.first,
|
114
|
+
meta: { page: 42 },
|
115
|
+
meta_key: :test_meta_data
|
116
|
+
)
|
117
|
+
end
|
118
|
+
end
|
89
119
|
end
|
90
120
|
end
|