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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dde77903d49df53ae283abc8d1c0b93e9f8d849b8d1696d39fced6a664b005dd
4
- data.tar.gz: e656b7ce6ce5e74076380b67fb5d86b6fe731a4df80edbb953c5e63694ce40de
3
+ metadata.gz: a30ba1db2e070cd13dac9ad8dcfd6fdfad46d0b70e6337732a505b89f6eb1154
4
+ data.tar.gz: 4b788edd3dcdbd55e0d65db463504b2483e6b48374f108e550a994dbd037a270
5
5
  SHA512:
6
- metadata.gz: fb83071875231fa5ec31244d47e2594de335787515dafe75fd9ce433060e151b38469c7b06e2a5428c6e1746855ced29ca6cf25970bd874c3dae056936347478
7
- data.tar.gz: be8908410852bc16ac94efe31b9972d384b82932fb791aad2e9e98df646386131fc260317e3b1d7d3c94e19f20ffc5e3c5951d0a87be67d68da88c7d8eb41cc4
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 [Hash] options any optional values from the serializer instance
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
- # @option options [Boolean] :collection whether to render the data as a
21
+ # @param collection [Boolean] whether to render the data as a
23
22
  # collection/array or a single object
24
- # @option options [Integer, Symbol] :status the HTTP response status code
25
- # or Rails-supported symbol. See
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(serializer, cacheable, key: nil, collection: nil, status: nil, **options)
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
- render_hash = { json: Oj.dump(plan.wrap(serializer_json), mode: OJ_MODE) }
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CacheCrispies
4
4
  # The version of the gem
5
- VERSION = '1.0.2'
5
+ VERSION = '1.1.0'
6
6
  end
@@ -24,7 +24,8 @@ describe CacheCrispies::Controller do
24
24
 
25
25
  describe '#cache_render' do
26
26
  let(:etags) { false }
27
- let(:single_json) { { cereal: { name: cereal_names.first } }.to_json }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_crispies
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Crownoble