mongoid-cached-json 1.6.0 → 1.7.1
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/.github/workflows/danger-comment.yml +19 -0
- data/.github/workflows/danger.yml +20 -0
- data/.github/workflows/lint.yml +29 -0
- data/.github/workflows/release.yml +31 -0
- data/.github/workflows/test.yml +73 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +143 -3
- data/.rubocop_todo.yml +181 -106
- data/.spellcheck.txt +84 -0
- data/CHANGELOG.md +64 -66
- data/CONTRIBUTING.md +20 -19
- data/Dangerfile +5 -0
- data/Gemfile +26 -17
- data/README.md +138 -69
- data/RELEASING.md +35 -0
- data/Rakefile +22 -3
- data/UPGRADING.md +59 -0
- data/lib/mongoid-cached-json/cached_json.rb +62 -17
- data/lib/mongoid-cached-json/version.rb +1 -1
- data/mongoid-cached-json.gemspec +1 -2
- data/script/release.rb +16 -0
- data/spec/all_fields_spec.rb +47 -0
- data/spec/cache_version_spec.rb +58 -0
- data/spec/cached_json_spec.rb +20 -11
- data/spec/coverage_spec.rb +44 -0
- data/spec/dalli_spec.rb +3 -4
- data/spec/field_cache_spec.rb +73 -0
- data/spec/model_cache_spec.rb +64 -0
- data/spec/mongoid_criteria_spec.rb +2 -1
- data/spec/release/release_spec.rb +77 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/json_employee.rb +1 -0
- data/spec/support/json_manager.rb +6 -1
- metadata +23 -8
- data/.travis.yml +0 -40
data/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
Mongoid::CachedJson
|
|
2
|
-
===================
|
|
1
|
+
# Mongoid::CachedJson
|
|
3
2
|
|
|
4
|
-
[](
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
[](https://badge.fury.io/rb/mongoid-cached-json) [](https://github.com/mongoid/mongoid-cached-json/actions/workflows/test.yml?query=branch%3Amaster) [](https://coveralls.io/github/mongoid/mongoid-cached-json?branch=master)
|
|
4
|
+
|
|
5
|
+
The Ruby 4.0 CI job measures library line coverage with SimpleCov and submits `coverage/.resultset.json` to Coveralls using GitHub's automatic token. Other matrix jobs do not submit duplicate reports. Run `COVERAGE=true bundle install` followed by `COVERAGE=true bundle exec rake spec` for a local report. The default-branch badge requires Coveralls to accept an upstream `master` build; configuring this workflow does not activate the external service.
|
|
7
6
|
|
|
8
7
|
Typical `as_json` definitions may involve lots of database point queries and method calls. When returning collections of objects, a single call may yield hundreds of database queries that can take seconds. This library mitigates the problem by implementing a module called `CachedJson`.
|
|
9
8
|
|
|
@@ -11,19 +10,21 @@ Typical `as_json` definitions may involve lots of database point queries and met
|
|
|
11
10
|
|
|
12
11
|
Using `Mongoid::CachedJson` we were able to cut our JSON API average response time by about a factor of 10.
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
-------------
|
|
13
|
+
## Resources
|
|
16
14
|
|
|
17
|
-
|
|
15
|
+
* [Need Help? Google Group](https://groups.google.com/g/mongoid-cached-json)
|
|
16
|
+
* [Source Code](https://github.com/mongoid/mongoid-cached-json)
|
|
17
|
+
* [Upgrading](UPGRADING.md)
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
---------
|
|
19
|
+
## Installation
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
* [Source Code](http://github.com/mongoid/mongoid-cached-json)
|
|
21
|
+
Add the gem to your Gemfile and run `bundle install`.
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
```ruby
|
|
24
|
+
gem 'mongoid-cached-json'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quickstart
|
|
27
28
|
|
|
28
29
|
Add `mongoid-cached-json` to your Gemfile.
|
|
29
30
|
|
|
@@ -31,7 +32,7 @@ Add `mongoid-cached-json` to your Gemfile.
|
|
|
31
32
|
|
|
32
33
|
Include `Mongoid::CachedJson` in your models.
|
|
33
34
|
|
|
34
|
-
```
|
|
35
|
+
```ruby
|
|
35
36
|
class Gadget
|
|
36
37
|
include Mongoid::CachedJson
|
|
37
38
|
|
|
@@ -43,7 +44,6 @@ class Gadget
|
|
|
43
44
|
json_fields \
|
|
44
45
|
name: {},
|
|
45
46
|
extras: { properties: :public }
|
|
46
|
-
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
class Widget
|
|
@@ -55,13 +55,12 @@ class Widget
|
|
|
55
55
|
json_fields \
|
|
56
56
|
name: {},
|
|
57
57
|
gadgets: { type: :reference, properties: :public }
|
|
58
|
-
|
|
59
58
|
end
|
|
60
59
|
```
|
|
61
60
|
|
|
62
61
|
Invoke `as_json`.
|
|
63
62
|
|
|
64
|
-
```
|
|
63
|
+
```ruby
|
|
65
64
|
widget = Widget.first
|
|
66
65
|
|
|
67
66
|
# the `:short` version of the JSON, `gadgets` not included
|
|
@@ -77,12 +76,11 @@ widget.as_json(properties: :public)
|
|
|
77
76
|
widget.as_json(properties: :all)
|
|
78
77
|
```
|
|
79
78
|
|
|
80
|
-
Configuration
|
|
81
|
-
-------------
|
|
79
|
+
## Configuration
|
|
82
80
|
|
|
83
81
|
By default `Mongoid::CachedJson` will use an instance of `ActiveSupport::Cache::MemoryStore` in a non-Rails and `Rails.cache` in a Rails environment. You can configure it to use any other cache store.
|
|
84
82
|
|
|
85
|
-
```
|
|
83
|
+
```ruby
|
|
86
84
|
Mongoid::CachedJson.configure do |config|
|
|
87
85
|
config.cache = ActiveSupport::Cache::FileStore.new
|
|
88
86
|
end
|
|
@@ -90,18 +88,17 @@ end
|
|
|
90
88
|
|
|
91
89
|
The default JSON version returned from `as_json` is `:unspecified`. If you wish to redefine this, set `Mongoid::CachedJson.config.default_version`.
|
|
92
90
|
|
|
93
|
-
```
|
|
91
|
+
```ruby
|
|
94
92
|
Mongoid::CachedJson.configure do |config|
|
|
95
93
|
config.default_version = :v2
|
|
96
94
|
end
|
|
97
95
|
```
|
|
98
96
|
|
|
99
|
-
Defining Fields
|
|
100
|
-
---------------
|
|
97
|
+
## Defining Fields
|
|
101
98
|
|
|
102
99
|
`Mongoid::CachedJson` supports the following options:
|
|
103
100
|
|
|
104
|
-
* `:hide_as_child_json_when` is an optional function that
|
|
101
|
+
* `:hide_as_child_json_when` is an optional function that omits the child key from `as_json` parent objects when it returns true, eg. `cached_json hide_as_child_json_when: lambda { |instance| ! instance.secret? }`. A missing relationship remains a `nil` value.
|
|
105
102
|
|
|
106
103
|
`Mongoid::CachedJson` field definitions support the following options:
|
|
107
104
|
|
|
@@ -110,23 +107,78 @@ Defining Fields
|
|
|
110
107
|
* `:properties` can be one of `:short`, `:public`, `:all`, in this order
|
|
111
108
|
* `:version` can be a single version for this field to appear in
|
|
112
109
|
* `:versions` can be an array of versions for this field to appear in
|
|
110
|
+
* `:cache` defaults to `true`; set it to `false` to recompute a field on every serialization (see below)
|
|
113
111
|
* `:reference_properties` can be one of `:short`, `:public`, `:all`, default will select the reference properties format dynamically (see below)
|
|
114
112
|
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
## Per-Field Caching
|
|
114
|
+
|
|
115
|
+
Use `cache: false` for a field whose value should be evaluated on every `as_json` call, even when the rest of the object's JSON comes from the cache. The field is excluded from the cached fragment and merged into the response after that fragment is fetched. Its `definition`, property/version filters, and configured transformations still apply.
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
class Product
|
|
119
|
+
include Mongoid::Document
|
|
120
|
+
include Mongoid::CachedJson
|
|
121
|
+
|
|
122
|
+
field :name
|
|
123
|
+
field :stock, type: Integer
|
|
124
|
+
|
|
125
|
+
json_fields \
|
|
126
|
+
name: {},
|
|
127
|
+
stock: { cache: false }
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
product = Product.create!(name: 'Widget', stock: 10)
|
|
131
|
+
product.as_json # => { name: 'Widget', stock: 10 }
|
|
132
|
+
|
|
133
|
+
# Unsaved changes illustrate which fields are recomputed on a cache hit.
|
|
134
|
+
product.name = 'Renamed widget'
|
|
135
|
+
product.stock = 9
|
|
136
|
+
product.as_json # => { name: 'Widget', stock: 9 }
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The cached `name` stays unchanged until its cache entry expires or is invalidated (for example, by saving the product); `stock` is read from the current model instance each time. This option does not reload the model from MongoDB: reload a stale instance yourself if another process has changed its stored values.
|
|
140
|
+
|
|
141
|
+
For a `type: :reference` field, `cache: false` bypasses reads and writes of the referenced object's own JSON fragment through that relationship. The parent's other fields remain cached:
|
|
142
|
+
|
|
143
|
+
```ruby
|
|
144
|
+
class Order
|
|
145
|
+
include Mongoid::Document
|
|
146
|
+
include Mongoid::CachedJson
|
|
147
|
+
|
|
148
|
+
field :number
|
|
149
|
+
belongs_to :product
|
|
150
|
+
|
|
151
|
+
json_fields \
|
|
152
|
+
number: {},
|
|
153
|
+
product: { type: :reference, cache: false }
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
order = Order.create!(number: '123', product: product)
|
|
157
|
+
order.as_json # => { number: '123', product: { name: 'Renamed widget', stock: 9 } }
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
The `product` reference is recomputed even if the product has a cached JSON fragment from another serialization. This does not disable caching on the `Product` model itself: `product.as_json` and other relationships to that product still use their normal cache policies. References nested inside the product also retain their own cache policies; the bypass is not recursive.
|
|
161
|
+
|
|
162
|
+
## All Stored Fields
|
|
163
|
+
|
|
164
|
+
Use `json_fields :all, display_name: { definition: :display_name }` to expand every Mongoid field declared so far (including `_id`) into an ordinary JSON field, then merge explicit definitions. An explicit definition overrides the generated definition for the same symbol key; for example, `json_fields :all, secret: { properties: :all }` restricts that field to the `:all` representation.
|
|
165
|
+
|
|
166
|
+
Declare Mongoid fields before `json_fields :all`. Later declarations, dynamic attributes, and associations are not added automatically; declare references explicitly with `type: :reference`. This is opt-in and may expose sensitive stored data or foreign keys, so prefer an explicit allowlist for public APIs. Runtime `as_json(methods: ...)` selection remains unsupported: declare computed methods in `json_fields`.
|
|
167
|
+
|
|
168
|
+
## Reference Properties
|
|
117
169
|
|
|
118
170
|
When calling `as_json` on a model that contains references to other models the value of the `:properties` option passed into the `as_json` call will be chosen as follows:
|
|
119
171
|
|
|
120
172
|
* Use the value of the `:reference_properties` option, if specified.
|
|
121
173
|
* For `:short` JSON, use `:short`.
|
|
122
|
-
* For `:public` JSON, use `:
|
|
174
|
+
* For `:public` JSON, use `:public`.
|
|
123
175
|
* For `:all` JSON, use `:all`.
|
|
124
176
|
|
|
125
177
|
The dynamic selection where `:public` generates `:short` references allows to return smaller embedded collections, while `:all` allows to fetch deep data. Another way of looking at this is to say that a field in a `:short` JSON appears in collections, a field declared in the `:public` JSON appears for all users and the field declared in the `:all` JSON appears for object owners only.
|
|
126
178
|
|
|
127
179
|
To override this behavior and always return the `:short` JSON for a child reference, use `:reference_properties`. In the following example we would want `Person.as_json(properties: :all)` to return the social security number for that person, but not for all their friends.
|
|
128
180
|
|
|
129
|
-
```
|
|
181
|
+
```ruby
|
|
130
182
|
class Person
|
|
131
183
|
include Mongoid::Document
|
|
132
184
|
include Mongoid::CachedJson
|
|
@@ -139,16 +191,14 @@ class Person
|
|
|
139
191
|
name: {},
|
|
140
192
|
ssn: { properties: :all },
|
|
141
193
|
friends: { properties: :public, reference_properties: :short }
|
|
142
|
-
|
|
143
194
|
end
|
|
144
195
|
```
|
|
145
196
|
|
|
146
|
-
Versioning
|
|
147
|
-
----------
|
|
197
|
+
## Versioning
|
|
148
198
|
|
|
149
199
|
You can set an optional `version` or `versions` attribute on JSON fields. Consider the following definition where the first version defined `:name`, then split it into `:first`, `:middle` and `:last` in version `:v2` and introduced a date of birth in `:v3`.
|
|
150
200
|
|
|
151
|
-
```
|
|
201
|
+
```ruby
|
|
152
202
|
class Person
|
|
153
203
|
include Mongoid::Document
|
|
154
204
|
include Mongoid::CachedJson
|
|
@@ -157,32 +207,37 @@ class Person
|
|
|
157
207
|
field :last
|
|
158
208
|
|
|
159
209
|
def name
|
|
160
|
-
[
|
|
210
|
+
[first, middle, last].compact.join(' ')
|
|
161
211
|
end
|
|
162
212
|
|
|
163
213
|
json_fields \
|
|
164
|
-
first: { versions: [
|
|
165
|
-
last: { versions: [
|
|
166
|
-
middle: { versions: [
|
|
214
|
+
first: { versions: %i[v2 v3] },
|
|
215
|
+
last: { versions: %i[v2 v3] },
|
|
216
|
+
middle: { versions: %i[v2 v3] },
|
|
167
217
|
born: { versions: :v3 },
|
|
168
218
|
name: { definition: :name }
|
|
169
|
-
|
|
170
219
|
end
|
|
171
220
|
```
|
|
172
221
|
|
|
173
|
-
```
|
|
222
|
+
```ruby
|
|
174
223
|
person = Person.create(first: 'John', middle: 'F.', last: 'Kennedy', born: 'May 29, 1917')
|
|
175
224
|
person.as_json # { name: 'John F. Kennedy' }
|
|
176
225
|
person.as_json(version: :v2) # { first: 'John', middle: 'F.', last: 'Kennedy', name: 'John F. Kennedy' }
|
|
177
|
-
person.as_json(version: :v3)
|
|
226
|
+
person.as_json(version: :v3)
|
|
227
|
+
# { first: 'John', middle: 'F.', last: 'Kennedy', name: 'John F. Kennedy', born: 'May 29, 1917' }
|
|
178
228
|
```
|
|
179
229
|
|
|
180
|
-
|
|
181
|
-
|
|
230
|
+
## Schema Cache Versions
|
|
231
|
+
|
|
232
|
+
Declare `json_fields cache_version: 2, name: {}` to add a model-specific schema namespace to its cache keys. Increment it when changing fields, computed values, or transformations in a deployment. This is independent of the API `version:` passed to `as_json`; callers need not change. Omitting `cache_version` preserves existing keys.
|
|
233
|
+
|
|
234
|
+
New code stops reading old fragments, including referenced fragments of that model. Old entries are not deleted and remain until eviction or a cache-store TTL. The gem does not set a default TTL; configure `expires_in` on your cache store if needed. Updates expire only the current namespace. For rolling deployments or rollbacks, do not reuse a namespace containing stale values: coordinate invalidation or choose another version.
|
|
235
|
+
|
|
236
|
+
## Transformations
|
|
182
237
|
|
|
183
238
|
You can define global transformations on all JSON values with `Mongoid::CachedJson.config.transform`. Each transformation must return a value. In the following example we extend the JSON definition with an application-specific `:trusted` field and encode any content that is not trusted.
|
|
184
239
|
|
|
185
|
-
```
|
|
240
|
+
```ruby
|
|
186
241
|
class Widget
|
|
187
242
|
include Mongoid::Document
|
|
188
243
|
include Mongoid::CachedJson
|
|
@@ -196,29 +251,46 @@ class Widget
|
|
|
196
251
|
end
|
|
197
252
|
```
|
|
198
253
|
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
end
|
|
254
|
+
```ruby
|
|
255
|
+
Mongoid::CachedJson.config.transform do |field, definition, value|
|
|
256
|
+
definition[:trusted] ? value : CGI.escapeHTML(value)
|
|
257
|
+
end
|
|
204
258
|
```
|
|
205
259
|
|
|
206
|
-
Mixing with Standard as_json
|
|
207
|
-
----------------------------
|
|
260
|
+
## Mixing with Standard `as_json`
|
|
208
261
|
|
|
209
262
|
Taking part in the `Mongoid::CachedJson` `json_fields` scheme is optional: you can still write `as_json` methods where it makes sense.
|
|
210
263
|
|
|
211
|
-
Turning Caching Off
|
|
212
|
-
-------------------
|
|
264
|
+
## Turning Caching Off
|
|
213
265
|
|
|
214
266
|
You can set `Mongoid::CachedJson.config.disable_caching = true`. It may be a good idea to set it to `ENV['DISABLE_JSON_CACHING']`, in case this turns out not to be The Solution To All Of Your Performance Problems (TM).
|
|
215
267
|
|
|
216
|
-
|
|
217
|
-
|
|
268
|
+
To bypass shared-cache reads and writes for one model while retaining its JSON fields, versions, and property hierarchy, declare `json_fields disable_caching: true, name: {}`. This applies when the model is serialized directly, in collections, or as a reference. Other models, including its parent and referenced children, keep their own caching policy. Changes to an uncached child are visible even when its parent's scalar fields are cached.
|
|
269
|
+
|
|
270
|
+
## Cache Invalidation Boundaries
|
|
271
|
+
|
|
272
|
+
Normal model updates and destroys expire that document's JSON fragments through Mongoid callbacks. Bulk or raw collection updates that bypass model callbacks cannot invalidate the cache automatically. Explicitly expire each affected document after such a write:
|
|
273
|
+
|
|
274
|
+
```ruby
|
|
275
|
+
User.where(id: user_id).update_all(status: 'active')
|
|
276
|
+
User.find(user_id).expire_cached_json
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Concurrent writes have a separate boundary: an older in-memory instance can repopulate a fragment after another instance has saved newer values and expired the cache. Reload stale instances before serializing them, and expire their fragments if a stale writer may already have repopulated the cache:
|
|
280
|
+
|
|
281
|
+
```ruby
|
|
282
|
+
user.reload
|
|
283
|
+
user.expire_cached_json
|
|
284
|
+
user.as_json
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Reloading and expiring are not an atomic cross-process operation. This gem does not provide a distributed revision or compare-and-set protocol for cache stores. Applications requiring strict consistency across concurrent writers should coordinate persistence and cache invalidation themselves or disable caching for the affected data.
|
|
288
|
+
|
|
289
|
+
## Testing JSON
|
|
218
290
|
|
|
219
291
|
This library overrides `as_json`, hence testing JSON results can be done at model level.
|
|
220
292
|
|
|
221
|
-
```
|
|
293
|
+
```ruby
|
|
222
294
|
describe 'as_json' do
|
|
223
295
|
before :each do
|
|
224
296
|
@person = Person.create!(first: 'John', last: 'Doe')
|
|
@@ -231,7 +303,7 @@ end
|
|
|
231
303
|
|
|
232
304
|
It's also common to test the results of the API using the [Pathy](https://github.com/twoism/pathy) library.
|
|
233
305
|
|
|
234
|
-
```
|
|
306
|
+
```ruby
|
|
235
307
|
describe 'as_json' do
|
|
236
308
|
before :each do
|
|
237
309
|
person = Person.create!(first: 'John', last: 'Doe')
|
|
@@ -243,14 +315,13 @@ describe 'as_json' do
|
|
|
243
315
|
end
|
|
244
316
|
```
|
|
245
317
|
|
|
246
|
-
Testing Cache Invalidation
|
|
247
|
-
--------------------------
|
|
318
|
+
## Testing Cache Invalidation
|
|
248
319
|
|
|
249
320
|
Cache is invalidated by calling `:expire_cached_json` on an instance.
|
|
250
321
|
|
|
251
|
-
```
|
|
322
|
+
```ruby
|
|
252
323
|
describe 'updating a person' do
|
|
253
|
-
before :each
|
|
324
|
+
before :each do
|
|
254
325
|
@person = Person.create!(name: 'John Doe')
|
|
255
326
|
end
|
|
256
327
|
it 'invalidates cache' do
|
|
@@ -259,6 +330,7 @@ describe 'updating a person' do
|
|
|
259
330
|
end
|
|
260
331
|
end
|
|
261
332
|
```
|
|
333
|
+
|
|
262
334
|
You may also want to use [this RSpec matcher](spec/support/matchers/invalidate.rb).
|
|
263
335
|
|
|
264
336
|
```ruby
|
|
@@ -271,12 +343,11 @@ describe 'updating a person' do
|
|
|
271
343
|
end
|
|
272
344
|
```
|
|
273
345
|
|
|
274
|
-
Performance
|
|
275
|
-
-----------
|
|
346
|
+
## Performance
|
|
276
347
|
|
|
277
348
|
This gem implements two interesting optimizations.
|
|
278
349
|
|
|
279
|
-
### Bulk Reference Resolving
|
|
350
|
+
### Bulk Reference Resolving with a Local Store
|
|
280
351
|
|
|
281
352
|
Consider an array of Mongoid instances, each with numerous references to other objects. It's typical to see such instances reference the same object. `Mongoid::CachedJson` first collects all JSON references, then resolves them after suppressing duplicates. This significantly reduces the number of cache queries.
|
|
282
353
|
|
|
@@ -284,14 +355,12 @@ Consider an array of Mongoid instances, each with numerous references to other o
|
|
|
284
355
|
|
|
285
356
|
Various cache stores, including Memcached, support bulk read operations. The [Dalli](https://github.com/mperham/dalli) gem exposes this via the `read_multi` method. `Mongoid::CachedJson` will always invoke `read_multi` where available, which significantly reduces the number of network roundtrips to the cache servers.
|
|
286
357
|
|
|
287
|
-
Contributing
|
|
288
|
-
------------
|
|
358
|
+
## Contributing
|
|
289
359
|
|
|
290
|
-
See [CONTRIBUTING](CONTRIBUTING.md).
|
|
360
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md), [CHANGELOG.md](CHANGELOG.md), and [UPGRADING.md](UPGRADING.md).
|
|
291
361
|
|
|
292
|
-
Copyright and License
|
|
293
|
-
---------------------
|
|
362
|
+
## Copyright and License
|
|
294
363
|
|
|
295
|
-
MIT License, see [LICENSE](
|
|
364
|
+
MIT License, see [LICENSE.md](LICENSE.md) for details.
|
|
296
365
|
|
|
297
366
|
(c) 2012-2014 [Artsy](https://artsy.net) and [Contributors](CHANGELOG.md)
|
data/RELEASING.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Releasing Mongoid::CachedJson
|
|
2
|
+
|
|
3
|
+
Releases use [ruby-gem-release](https://github.com/dblock/ruby-gem-release) 0.9.0 through a manually dispatched [Release workflow](.github/workflows/release.yml). Keep the `ruby-gem-release` gem in [Gemfile](Gemfile) and the reusable workflow in `release.yml` pinned to the same version. The workflow publishes through RubyGems Trusted Publishing, so no RubyGems API key or one-time password is needed. It only runs from `master` in `mongoid/mongoid-cached-json`. The local Bundler `rake release`, `release:rubygem_push`, and `release:source_control_push` tasks are disabled.
|
|
4
|
+
|
|
5
|
+
## One-Time Setup
|
|
6
|
+
|
|
7
|
+
Before the first automated release, a maintainer must:
|
|
8
|
+
|
|
9
|
+
1. Create the GitHub environment `release` in `mongoid/mongoid-cached-json`, for example with `gh api --method PUT repos/mongoid/mongoid-cached-json/environments/release`, and restrict its deployment branches to `master`. Add required reviewers to require maintainer approval before publication.
|
|
10
|
+
2. At [mongoid-cached-json's trusted publishers](https://rubygems.org/gems/mongoid-cached-json/trusted_publishers), add a GitHub Actions publisher with repository owner `mongoid`, repository `mongoid-cached-json`, workflow filename `release.yml`, environment `release`, and the separate workflow repository owner `dblock` and repository `ruby-gem-release`.
|
|
11
|
+
3. Allow GitHub Actions to push release commits and `v*` tags to `master`. See the upstream [setup and permissions guidance](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/RELEASING.md#one-time-setup).
|
|
12
|
+
|
|
13
|
+
Verify the setup without publishing with a [dry run](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/actions/release.md#dry-run), which never edits files, builds, pushes, or publishes:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
gh workflow run release.yml --repo mongoid/mongoid-cached-json --ref master -f dry-run=true
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Prepare
|
|
20
|
+
|
|
21
|
+
Start with a clean, up-to-date `master` and verify that the latest [CI checks](https://github.com/mongoid/mongoid-cached-json/actions) pass. Follow semantic versioning: patch for fixes, minor for backward-compatible features, major for breaking changes. Check `VERSION` in [lib/mongoid-cached-json/version.rb](lib/mongoid-cached-json/version.rb) and the matching `(Next)` heading in [CHANGELOG.md](CHANGELOG.md), and change both in a pull request if needed, or pass a `version` override when dispatching. The workflow dates the `(Next)` heading using `YYYY/MM/DD` and removes the `* Your contribution here.` placeholder; a section without notes cannot be released.
|
|
22
|
+
|
|
23
|
+
Release automation requires MRI Ruby 3.3 or newer; the release dependency and tasks are skipped on older Ruby versions and JRuby. Release specs in [spec/release](spec/release) run the actual release tasks against a local fixture with the [ruby-gem-release RSpec helpers](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/testing.md), without contacting RubyGems or GitHub.
|
|
24
|
+
|
|
25
|
+
## Publish
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
gh workflow run release.yml --repo mongoid/mongoid-cached-json --ref master
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Pass `-f version=x.y.z` to release a different version. Dispatching starts a real release; do not merge into `master` while it runs. The workflow prepares the release commit, starts MongoDB 8.0, runs lint and specs on Ruby 3.3, builds the gem, preserves recovery artifacts, publishes to RubyGems, pushes the release commit and `v` tag, and creates a GitHub Release with the changelog notes and the gem attached. It then increments the patch version, adds a new `(Next)` section, and pushes the next development commit. Verify the published version on [RubyGems](https://rubygems.org/gems/mongoid-cached-json), the tag, the [GitHub Release](https://github.com/mongoid/mongoid-cached-json/releases), and the next development commit.
|
|
32
|
+
|
|
33
|
+
## Recover a Partial Release
|
|
34
|
+
|
|
35
|
+
Inspect the failed run, RubyGems, remote commits and tags, and the GitHub Release before retrying. Publication may have succeeded even if a later step failed. Never move a published tag or republish a version with different contents. Use the run's retained recovery artifacts and the upstream [recovery instructions](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/actions/release.md#recovery-artifacts).
|
data/Rakefile
CHANGED
|
@@ -6,6 +6,18 @@ require File.expand_path('lib/mongoid-cached-json/version', __dir__)
|
|
|
6
6
|
Bundler.setup(:default, :development)
|
|
7
7
|
Bundler::GemHelper.install_tasks
|
|
8
8
|
|
|
9
|
+
if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
|
|
10
|
+
Gem.loaded_specs.key?('ruby-gem-release')
|
|
11
|
+
require_relative 'script/release'
|
|
12
|
+
MongoidCachedJsonRelease.workflow.install_tasks
|
|
13
|
+
|
|
14
|
+
%w[release release:rubygem_push release:source_control_push].each do |name|
|
|
15
|
+
Rake::Task[name].clear
|
|
16
|
+
desc 'Disabled; releases run through GitHub Actions, see RELEASING.md'
|
|
17
|
+
task(name) { abort "#{name} is disabled; releases run through GitHub Actions, see RELEASING.md" }
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
9
21
|
require 'rake'
|
|
10
22
|
|
|
11
23
|
require 'rspec/core'
|
|
@@ -28,7 +40,14 @@ Rake::RDocTask.new do |rdoc|
|
|
|
28
40
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
29
41
|
end
|
|
30
42
|
|
|
31
|
-
|
|
32
|
-
|
|
43
|
+
lint = RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
|
|
44
|
+
Gem.loaded_specs.key?('rubocop')
|
|
45
|
+
if lint
|
|
46
|
+
require 'rubocop/rake_task'
|
|
47
|
+
RuboCop::RakeTask.new(:rubocop)
|
|
48
|
+
|
|
49
|
+
desc 'Lint Ruby, Markdown, YAML, and spelling'
|
|
50
|
+
task lint: :rubocop
|
|
51
|
+
end
|
|
33
52
|
|
|
34
|
-
task default: [
|
|
53
|
+
task default: lint ? %i[rubocop spec] : %i[spec]
|
data/UPGRADING.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Upgrading
|
|
2
|
+
|
|
3
|
+
## Upgrading To 1.7.0
|
|
4
|
+
|
|
5
|
+
### Hidden Child References
|
|
6
|
+
|
|
7
|
+
[#35](https://github.com/mongoid/mongoid-cached-json/pull/35) changes how a single child reference is serialized when the child's `hide_as_child_json_when` predicate returns `true`. Previously, an existing but hidden child and a missing child both appeared as `child: nil` (`"child": null` in JSON), so clients could not distinguish them. Now, an existing but hidden child's key is omitted; a missing child still appears as `child: nil`.
|
|
8
|
+
|
|
9
|
+
Here, `parent` is a `SecretParent` document with a `has_one` relationship to a `SometimesSecret` document. The parent exposes that relationship as the JSON field `child`; the hiding predicate is defined on the child model:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
class SecretParent
|
|
13
|
+
include Mongoid::Document
|
|
14
|
+
include Mongoid::CachedJson
|
|
15
|
+
|
|
16
|
+
field :name
|
|
17
|
+
has_one :sometimes_secret
|
|
18
|
+
|
|
19
|
+
json_fields \
|
|
20
|
+
name: {},
|
|
21
|
+
child: { definition: :sometimes_secret, type: :reference }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class SometimesSecret
|
|
25
|
+
include Mongoid::Document
|
|
26
|
+
include Mongoid::CachedJson
|
|
27
|
+
|
|
28
|
+
field :secret
|
|
29
|
+
field :should_tell_secret, type: Boolean
|
|
30
|
+
belongs_to :secret_parent
|
|
31
|
+
|
|
32
|
+
json_fields \
|
|
33
|
+
hide_as_child_json_when: ->(child) { !child.should_tell_secret },
|
|
34
|
+
secret: {}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
parent = SecretParent.create!(name: 'Parent')
|
|
38
|
+
parent.create_sometimes_secret!(secret: 'Afraid of the dark', should_tell_secret: false)
|
|
39
|
+
|
|
40
|
+
# The child exists, but its hiding predicate returns true.
|
|
41
|
+
# Before 1.7.0:
|
|
42
|
+
parent.as_json(properties: :short)
|
|
43
|
+
# => { name: 'Parent', child: nil }
|
|
44
|
+
|
|
45
|
+
# From 1.7.0:
|
|
46
|
+
parent.as_json(properties: :short)
|
|
47
|
+
# => { name: 'Parent' }
|
|
48
|
+
|
|
49
|
+
# By contrast, a parent with no child still includes an explicit nil.
|
|
50
|
+
parent_without_child = SecretParent.create!(name: 'No child')
|
|
51
|
+
parent_without_child.as_json(properties: :short)
|
|
52
|
+
# => { name: 'No child', child: nil }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
A genuinely missing relationship still includes `child: nil`. Requests using `properties: :all` and serialization of the child itself are unchanged.
|
|
56
|
+
|
|
57
|
+
Update clients and response schemas that require the child key to allow its absence. In Ruby, `json[:child]` still returns `nil` for either case; use `json.key?(:child)` to distinguish an omitted key from an explicit `nil`.
|
|
58
|
+
|
|
59
|
+
Invalidate existing cached JSON fragments when deploying this change so cached results use the new representation. If the cache is shared with other applications, invalidate only this application's cached JSON entries rather than clearing the entire store.
|