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
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
module Mongoid
|
|
3
3
|
module CachedJson
|
|
4
|
+
HIDDEN_JSON = :__mongoid_cached_json_hidden__
|
|
5
|
+
|
|
4
6
|
extend ActiveSupport::Concern
|
|
5
7
|
|
|
6
8
|
included do
|
|
7
9
|
class_attribute :all_json_properties
|
|
8
10
|
class_attribute :all_json_versions
|
|
9
11
|
class_attribute :cached_json_field_defs
|
|
12
|
+
class_attribute :cached_json_uncached_field_defs
|
|
10
13
|
class_attribute :cached_json_reference_defs
|
|
11
14
|
class_attribute :hide_as_child_json_when
|
|
15
|
+
class_attribute :cached_json_cache_version
|
|
16
|
+
class_attribute :cached_json_disable_caching
|
|
12
17
|
end
|
|
13
18
|
|
|
14
19
|
module ClassMethods
|
|
@@ -17,11 +22,17 @@ module Mongoid
|
|
|
17
22
|
# @param [ hash ] defs JSON field definition.
|
|
18
23
|
#
|
|
19
24
|
# @since 1.0
|
|
20
|
-
def json_fields(defs)
|
|
25
|
+
def json_fields(defs, additions = {})
|
|
26
|
+
if defs == :all
|
|
27
|
+
defs = Hash[fields.keys.map { |field| [field.to_sym, {}] }].merge(additions)
|
|
28
|
+
end
|
|
29
|
+
self.cached_json_cache_version = defs.delete(:cache_version)
|
|
30
|
+
self.cached_json_disable_caching = defs.delete(:disable_caching)
|
|
21
31
|
self.hide_as_child_json_when = defs.delete(:hide_as_child_json_when) || lambda { |_a| false }
|
|
22
32
|
self.all_json_properties = [:short, :public, :all]
|
|
23
33
|
cached_json_defs = Hash[defs.map { |k, v| [k, { type: :callable, properties: :short, definition: k }.merge(v)] }]
|
|
24
34
|
self.cached_json_field_defs = {}
|
|
35
|
+
self.cached_json_uncached_field_defs = {}
|
|
25
36
|
self.cached_json_reference_defs = {}
|
|
26
37
|
# Collect all versions for clearing cache
|
|
27
38
|
self.all_json_versions = cached_json_defs.map do |_field, definition|
|
|
@@ -29,7 +40,10 @@ module Mongoid
|
|
|
29
40
|
end.flatten.compact.uniq
|
|
30
41
|
all_json_properties.each_with_index do |property, i|
|
|
31
42
|
cached_json_field_defs[property] = Hash[cached_json_defs.find_all do |_field, definition|
|
|
32
|
-
all_json_properties.find_index(definition[:properties]) <= i && definition[:type] == :callable
|
|
43
|
+
all_json_properties.find_index(definition[:properties]) <= i && definition[:type] == :callable && definition[:cache] != false
|
|
44
|
+
end]
|
|
45
|
+
cached_json_uncached_field_defs[property] = Hash[cached_json_defs.find_all do |_field, definition|
|
|
46
|
+
all_json_properties.find_index(definition[:properties]) <= i && definition[:type] == :callable && definition[:cache] == false
|
|
33
47
|
end]
|
|
34
48
|
cached_json_reference_defs[property] = Hash[cached_json_defs.find_all do |_field, definition|
|
|
35
49
|
all_json_properties.find_index(definition[:properties]) <= i && definition[:type] == :reference
|
|
@@ -49,8 +63,10 @@ module Mongoid
|
|
|
49
63
|
def materialize_cached_json(clazz, id, object_reference, options)
|
|
50
64
|
is_top_level_json = options[:is_top_level_json] || false
|
|
51
65
|
object_reference = clazz.where(_id: id).first unless object_reference
|
|
52
|
-
if !object_reference
|
|
66
|
+
if !object_reference
|
|
53
67
|
nil
|
|
68
|
+
elsif !is_top_level_json && options[:properties] != :all && clazz.hide_as_child_json_when.call(object_reference)
|
|
69
|
+
HIDDEN_JSON
|
|
54
70
|
else
|
|
55
71
|
Hash[clazz.cached_json_field_defs[options[:properties]].map do |field, definition|
|
|
56
72
|
# version match
|
|
@@ -65,6 +81,22 @@ module Mongoid
|
|
|
65
81
|
end
|
|
66
82
|
end
|
|
67
83
|
|
|
84
|
+
# Materialize JSON fields that opt out of shared caching.
|
|
85
|
+
def materialize_uncached_json(clazz, id, object_reference, options)
|
|
86
|
+
object_reference = clazz.where(_id: id).first unless object_reference
|
|
87
|
+
return {} unless object_reference
|
|
88
|
+
|
|
89
|
+
Hash[clazz.cached_json_uncached_field_defs[options[:properties]].map do |field, definition|
|
|
90
|
+
versions = ([definition[:version]] | Array(definition[:versions])).compact
|
|
91
|
+
next unless versions.empty? || versions.include?(options[:version])
|
|
92
|
+
json_value = (definition[:definition].is_a?(Symbol) ? object_reference.send(definition[:definition]) : definition[:definition].call(object_reference))
|
|
93
|
+
Mongoid::CachedJson.config.transform.each do |t|
|
|
94
|
+
json_value = t.call(field, definition, json_value)
|
|
95
|
+
end
|
|
96
|
+
[field, json_value]
|
|
97
|
+
end.compact]
|
|
98
|
+
end
|
|
99
|
+
|
|
68
100
|
# Given an object definition in the form of either an object or a class, id pair,
|
|
69
101
|
# grab the as_json representation from the cache if possible, otherwise create
|
|
70
102
|
# the as_json representation by loading the object from the database. For any
|
|
@@ -84,7 +116,8 @@ module Mongoid
|
|
|
84
116
|
id = object_def[:id]
|
|
85
117
|
end
|
|
86
118
|
key = cached_json_key(options, clazz, id)
|
|
87
|
-
json = { _ref: { _clazz: self, _key: key,
|
|
119
|
+
json = { _ref: { _clazz: self, _key: key, _disable_caching: clazz.cached_json_disable_caching,
|
|
120
|
+
_materialize_cached_json: [clazz, id, object_reference, options] } }
|
|
88
121
|
keys = KeyReferences.new
|
|
89
122
|
keys.set_and_add(key, json)
|
|
90
123
|
reference_defs = clazz.cached_json_reference_defs[options[:properties]]
|
|
@@ -98,6 +131,12 @@ module Mongoid
|
|
|
98
131
|
ref[:_parent] = json
|
|
99
132
|
ref[:_field] = field
|
|
100
133
|
end
|
|
134
|
+
if definition[:cache] == false
|
|
135
|
+
references = reference.is_a?(Array) ? reference : [reference]
|
|
136
|
+
references.each do |item|
|
|
137
|
+
item[:_ref][:_disable_caching] = true if item.is_a?(Hash) && item[:_ref]
|
|
138
|
+
end
|
|
139
|
+
end
|
|
101
140
|
keys.merge_set(reference_keys)
|
|
102
141
|
[field, reference]
|
|
103
142
|
end])
|
|
@@ -109,7 +148,8 @@ module Mongoid
|
|
|
109
148
|
# Cache key.
|
|
110
149
|
def cached_json_key(options, cached_class, cached_id)
|
|
111
150
|
base_class_name = cached_class.collection_name.to_s.singularize.camelize
|
|
112
|
-
"as_json/#{options[:version]}/#{base_class_name}/#{cached_id}/#{options[:properties]}/#{!!options[:is_top_level_json]}"
|
|
151
|
+
key = "as_json/#{options[:version]}/#{base_class_name}/#{cached_id}/#{options[:properties]}/#{!!options[:is_top_level_json]}"
|
|
152
|
+
cached_class.cached_json_cache_version.nil? ? key : "#{key}/schema/#{cached_class.cached_json_cache_version}"
|
|
113
153
|
end
|
|
114
154
|
|
|
115
155
|
# If the reference is a symbol, we may be lucky and be able to figure out the as_json
|
|
@@ -151,13 +191,11 @@ module Mongoid
|
|
|
151
191
|
reference_def_definition = reference_def[:definition]
|
|
152
192
|
reference = reference_def_definition.is_a?(Symbol) ? object.send(reference_def_definition) : reference_def_definition.call(object)
|
|
153
193
|
reference_json = nil
|
|
154
|
-
if reference
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
reference_json = reference.as_json(options)
|
|
160
|
-
end
|
|
194
|
+
if reference.respond_to?(:as_json_partial)
|
|
195
|
+
reference_keys, reference_json = reference.as_json_partial(options)
|
|
196
|
+
keys = keys ? keys.merge_set(reference_keys) : reference_keys
|
|
197
|
+
else
|
|
198
|
+
reference_json = reference.as_json(options)
|
|
161
199
|
end
|
|
162
200
|
end
|
|
163
201
|
[keys, reference_json]
|
|
@@ -166,8 +204,10 @@ module Mongoid
|
|
|
166
204
|
|
|
167
205
|
# Check whether the cache supports :read_multi and prefetch the data if it does.
|
|
168
206
|
def self.materialize_json_references_with_read_multi(key_refs, partial_json)
|
|
169
|
-
|
|
170
|
-
|
|
207
|
+
if key_refs
|
|
208
|
+
unfrozen_keys = key_refs.select { |_key, refs| refs.any? { |ref| !ref[:_ref][:_disable_caching] } }.keys.map(&:dup)
|
|
209
|
+
end
|
|
210
|
+
read_multi = unfrozen_keys && !unfrozen_keys.empty? && Mongoid::CachedJson.config.cache.respond_to?(:read_multi)
|
|
171
211
|
local_cache = read_multi ? Mongoid::CachedJson.config.cache.read_multi(*unfrozen_keys) : {}
|
|
172
212
|
Mongoid::CachedJson.materialize_json_references(key_refs, local_cache, read_multi) if key_refs
|
|
173
213
|
partial_json
|
|
@@ -180,7 +220,9 @@ module Mongoid
|
|
|
180
220
|
_ref = ref.delete(:_ref)
|
|
181
221
|
key = _ref[:_key]
|
|
182
222
|
fetched_json = local_cache[key] if local_cache.key?(key)
|
|
183
|
-
|
|
223
|
+
if _ref[:_disable_caching]
|
|
224
|
+
fetched_json = _ref[:_clazz].materialize_cached_json(* _ref[:_materialize_cached_json])
|
|
225
|
+
elsif !fetched_json
|
|
184
226
|
if read_multi
|
|
185
227
|
# no value in cache, materialize and write
|
|
186
228
|
fetched_json = (local_cache[key] = _ref[:_clazz].materialize_cached_json(* _ref[:_materialize_cached_json]))
|
|
@@ -192,8 +234,11 @@ module Mongoid
|
|
|
192
234
|
end)
|
|
193
235
|
end
|
|
194
236
|
end
|
|
195
|
-
if fetched_json
|
|
237
|
+
if fetched_json == HIDDEN_JSON
|
|
238
|
+
_ref[:_parent].delete(_ref[:_field]) if _ref[:_parent]
|
|
239
|
+
elsif fetched_json
|
|
196
240
|
ref.merge! fetched_json
|
|
241
|
+
ref.merge! _ref[:_clazz].materialize_uncached_json(* _ref[:_materialize_cached_json])
|
|
197
242
|
elsif _ref[:_parent]
|
|
198
243
|
# a single _ref that resolved to a nil
|
|
199
244
|
_ref[:_parent][_ref[:_field]] = nil
|
|
@@ -249,7 +294,7 @@ module Mongoid
|
|
|
249
294
|
# config.cache = Rails.cache
|
|
250
295
|
# end
|
|
251
296
|
#
|
|
252
|
-
# @return [ Config ] The configuration
|
|
297
|
+
# @return [ Config ] The configuration object.
|
|
253
298
|
def configure
|
|
254
299
|
block_given? ? yield(Mongoid::CachedJson::Config) : Mongoid::CachedJson::Config
|
|
255
300
|
end
|
data/mongoid-cached-json.gemspec
CHANGED
data/script/release.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'ruby-gem-release/changelog'
|
|
4
|
+
require 'ruby-gem-release/workflow/ruby_gem'
|
|
5
|
+
|
|
6
|
+
# Builds the ruby-gem-release workflow used by the Rakefile, the Release GitHub Actions workflow, and spec/release.
|
|
7
|
+
module MongoidCachedJsonRelease
|
|
8
|
+
def self.workflow(**options)
|
|
9
|
+
RubyGemRelease::Workflow::RubyGem.new(
|
|
10
|
+
version: RubyGemRelease::Version::RubyConstant.new(path: 'lib/mongoid-cached-json/version.rb',
|
|
11
|
+
constant: 'VERSION'),
|
|
12
|
+
changelog: RubyGemRelease::Changelog::Intridea.new(placeholder: '* Your contribution here.'),
|
|
13
|
+
**options
|
|
14
|
+
)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Declaring all stored fields' do
|
|
4
|
+
before do
|
|
5
|
+
class AllFieldsDocument
|
|
6
|
+
include Mongoid::Document
|
|
7
|
+
include Mongoid::CachedJson
|
|
8
|
+
field :name
|
|
9
|
+
field :secret
|
|
10
|
+
|
|
11
|
+
def display_name
|
|
12
|
+
name.upcase
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
json_fields :all, secret: { properties: :all }, display_name: {}
|
|
16
|
+
field :added_later
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
after do
|
|
21
|
+
AllFieldsDocument.delete_all
|
|
22
|
+
Object.send(:remove_const, :AllFieldsDocument)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'expands fields at declaration time and adds computed fields' do
|
|
26
|
+
document = AllFieldsDocument.create!(name: 'example', secret: 'hidden', added_later: 'not declared')
|
|
27
|
+
expected = { _id: document.id, name: 'example', display_name: 'EXAMPLE' }
|
|
28
|
+
expect(document.as_json).to eq(expected)
|
|
29
|
+
expect([document].as_json).to eq([expected])
|
|
30
|
+
expect(document.as_json(properties: :all)).to eq(expected.merge(secret: 'hidden'))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'retains the normal field cache and invalidation behavior' do
|
|
34
|
+
document = AllFieldsDocument.create!(name: 'example')
|
|
35
|
+
document.as_json
|
|
36
|
+
document.name = 'unsaved'
|
|
37
|
+
expect(document.as_json[:name]).to eq('example')
|
|
38
|
+
document.save!
|
|
39
|
+
expect(document.as_json[:name]).to eq('unsaved')
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it 'does not expose extra fields for ordinary explicit declarations' do
|
|
43
|
+
document = JsonFoobar.create!(foo: 'visible', bar: 'private')
|
|
44
|
+
expect(document.as_json).not_to have_key(:bar)
|
|
45
|
+
expect(document.as_json).not_to have_key(:_id)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Schema cache versions' do
|
|
4
|
+
before do
|
|
5
|
+
class VersionedDocument
|
|
6
|
+
include Mongoid::Document
|
|
7
|
+
include Mongoid::CachedJson
|
|
8
|
+
field :value
|
|
9
|
+
json_fields cache_version: 1, value: {}
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
after do
|
|
14
|
+
VersionedDocument.delete_all
|
|
15
|
+
Object.send(:remove_const, :VersionedDocument)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
[true, false].each do |bulk|
|
|
19
|
+
context "with bulk reads #{bulk}" do
|
|
20
|
+
before do
|
|
21
|
+
@store = ActiveSupport::Cache::MemoryStore.new
|
|
22
|
+
@store.singleton_class.send(:undef_method, :read_multi) unless bulk
|
|
23
|
+
Mongoid::CachedJson.config.cache = @store
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'separates new schema fragments without changing the API version' do
|
|
27
|
+
document = VersionedDocument.create!(value: 'old')
|
|
28
|
+
expect(document.as_json).to eq(value: 'old')
|
|
29
|
+
document.value = 'new'
|
|
30
|
+
expect(document.as_json).to eq(value: 'old')
|
|
31
|
+
VersionedDocument.cached_json_cache_version = 2
|
|
32
|
+
expect(document.as_json).to eq(value: 'new')
|
|
33
|
+
VersionedDocument.cached_json_cache_version = 1
|
|
34
|
+
expect(document.as_json).to eq(value: 'old')
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'invalidates the current namespace on a model update' do
|
|
38
|
+
document = VersionedDocument.create!(value: 'old')
|
|
39
|
+
[1, 2].each do |version|
|
|
40
|
+
VersionedDocument.cached_json_cache_version = version
|
|
41
|
+
document.as_json
|
|
42
|
+
end
|
|
43
|
+
document.update_attributes!(value: 'new')
|
|
44
|
+
expect(document.as_json).to eq(value: 'new')
|
|
45
|
+
VersionedDocument.cached_json_cache_version = 1
|
|
46
|
+
expect(document.as_json).to eq(value: 'old')
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'uses the schema version for child fragments too' do
|
|
50
|
+
document = VersionedDocument.create!(value: 'old')
|
|
51
|
+
options = { properties: :short, version: :unspecified, is_top_level_json: false }
|
|
52
|
+
key = VersionedDocument.cached_json_key(options, VersionedDocument, document.id)
|
|
53
|
+
expect(key).to end_with('/false/schema/1')
|
|
54
|
+
expect([document].as_json).to eq([{ value: 'old' }])
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
data/spec/cached_json_spec.rb
CHANGED
|
@@ -6,7 +6,7 @@ describe Mongoid::CachedJson do
|
|
|
6
6
|
expect(Mongoid::CachedJson::VERSION.to_f).to be > 0
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
[
|
|
9
|
+
[MEMCACHE_STORE, :memory_store].each do |cache_store|
|
|
10
10
|
context "#{cache_store}" do
|
|
11
11
|
before :each do
|
|
12
12
|
@cache = Mongoid::CachedJson::Config.cache
|
|
@@ -95,7 +95,7 @@ describe Mongoid::CachedJson do
|
|
|
95
95
|
3.times do
|
|
96
96
|
3.times do
|
|
97
97
|
manager_short_json = manager.as_json(properties: :short)
|
|
98
|
-
expect(manager_short_json.length).to eq(
|
|
98
|
+
expect(manager_short_json.length).to eq(3)
|
|
99
99
|
expect(manager_short_json[:name]).to eq('Boss')
|
|
100
100
|
expect(manager_short_json[:employees].member?(name: 'Peon')).to be_truthy
|
|
101
101
|
expect(manager_short_json[:employees].member?(name: 'Indentured servant')).to be_truthy
|
|
@@ -104,7 +104,7 @@ describe Mongoid::CachedJson do
|
|
|
104
104
|
end
|
|
105
105
|
3.times do
|
|
106
106
|
manager_public_json = manager.as_json(properties: :public)
|
|
107
|
-
expect(manager_public_json.length).to eq(
|
|
107
|
+
expect(manager_public_json.length).to eq(3)
|
|
108
108
|
expect(manager_public_json[:name]).to eq('Boss')
|
|
109
109
|
expect(manager_public_json[:employees].member?(name: 'Peon')).to be_truthy
|
|
110
110
|
expect(manager_public_json[:employees].member?(name: 'Indentured servant')).to be_truthy
|
|
@@ -113,7 +113,7 @@ describe Mongoid::CachedJson do
|
|
|
113
113
|
end
|
|
114
114
|
3.times do
|
|
115
115
|
manager_all_json = manager.as_json(properties: :all)
|
|
116
|
-
expect(manager_all_json.length).to eq(
|
|
116
|
+
expect(manager_all_json.length).to eq(4)
|
|
117
117
|
expect(manager_all_json[:name]).to eq('Boss')
|
|
118
118
|
expect(manager_all_json[:ssn]).to eq('123-45-6789')
|
|
119
119
|
expect(manager_all_json[:employees].member?(name: 'Peon', nickname: 'My Favorite')).to be_truthy
|
|
@@ -132,15 +132,15 @@ describe Mongoid::CachedJson do
|
|
|
132
132
|
manager = JsonManager.create!(name: 'JsonManager')
|
|
133
133
|
employee = manager.json_employees.create!(name: 'JsonEmployee')
|
|
134
134
|
3.times do
|
|
135
|
-
expect(manager.as_json(properties: :short)).to eq(name: 'JsonManager', employees: [{ name: 'JsonEmployee' }])
|
|
135
|
+
expect(manager.as_json(properties: :short)).to eq(name: 'JsonManager', employees: [{ name: 'JsonEmployee' }], has_left_handed_employee: false)
|
|
136
136
|
expect(employee.as_json(properties: :short)).to eq(name: 'JsonEmployee')
|
|
137
137
|
end
|
|
138
138
|
manager.name = 'New JsonManager'
|
|
139
139
|
manager.save
|
|
140
|
-
3.times { expect(manager.as_json(properties: :short)).to eq(name: 'New JsonManager', employees: [{ name: 'JsonEmployee' }]) }
|
|
140
|
+
3.times { expect(manager.as_json(properties: :short)).to eq(name: 'New JsonManager', employees: [{ name: 'JsonEmployee' }], has_left_handed_employee: false) }
|
|
141
141
|
employee.name = 'New JsonEmployee'
|
|
142
142
|
employee.save
|
|
143
|
-
3.times { expect(manager.as_json(properties: :short)).to eq(name: 'New JsonManager', employees: [{ name: 'New JsonEmployee' }]) }
|
|
143
|
+
3.times { expect(manager.as_json(properties: :short)).to eq(name: 'New JsonManager', employees: [{ name: 'New JsonEmployee' }], has_left_handed_employee: false) }
|
|
144
144
|
end
|
|
145
145
|
context 'reference_properties' do
|
|
146
146
|
it 'limits the json fields of a child relationship' do
|
|
@@ -220,6 +220,7 @@ describe Mongoid::CachedJson do
|
|
|
220
220
|
end
|
|
221
221
|
it 'should yield JSON without an instance of a child' do
|
|
222
222
|
p = SecretParent.create(name: 'Parent')
|
|
223
|
+
expect(p.as_json(properties: :short)).to eq(name: 'Parent', child: nil)
|
|
223
224
|
expect(p.as_json(properties: :all)[:child]).to be_nil
|
|
224
225
|
end
|
|
225
226
|
it 'should yield child JSON when as_json is called on the parent and hide_as_child_json_when returns false on an instance' do
|
|
@@ -230,8 +231,8 @@ describe Mongoid::CachedJson do
|
|
|
230
231
|
it 'should not yield child JSON when as_json is called on the parent and hide_as_child_json_when returns true on an instance' do
|
|
231
232
|
p = SecretParent.create(name: 'Parent')
|
|
232
233
|
p.create_sometimes_secret(should_tell_secret: false)
|
|
233
|
-
expect(p.as_json(properties: :short)).to eq(name: 'Parent'
|
|
234
|
-
expect(p.as_json(properties: :short)
|
|
234
|
+
expect(p.as_json(properties: :short)).to eq(name: 'Parent')
|
|
235
|
+
expect(p.as_json(properties: :short)).not_to have_key(:child)
|
|
235
236
|
end
|
|
236
237
|
end
|
|
237
238
|
context 'relationships with a multi-level hierarchy' do
|
|
@@ -353,9 +354,9 @@ describe Mongoid::CachedJson do
|
|
|
353
354
|
example = JsonFoobar.create(foo: 'FOO', baz: 'BAZ', bar: 'BAR')
|
|
354
355
|
key = "as_json/unspecified/JsonFoobar/#{example.id}/short/true"
|
|
355
356
|
case cache_store
|
|
356
|
-
when :memory_store
|
|
357
|
+
when :memory_store
|
|
357
358
|
expect(Mongoid::CachedJson.config.cache).to receive(:fetch).with(key, force: true).twice
|
|
358
|
-
when :dalli_store
|
|
359
|
+
when :dalli_store, :mem_cache_store
|
|
359
360
|
expect(Mongoid::CachedJson.config.cache).not_to receive(:write)
|
|
360
361
|
else
|
|
361
362
|
fail ArgumentError, "invalid cache store: #{cache_store}"
|
|
@@ -496,6 +497,14 @@ describe Mongoid::CachedJson do
|
|
|
496
497
|
expect(@image.as_json(properties: :all)).to eq(name: 'Image', urls: [])
|
|
497
498
|
end
|
|
498
499
|
end
|
|
500
|
+
context 'evaluating a false-y value in a reference type' do
|
|
501
|
+
it 'correctly returns false instead of nil' do
|
|
502
|
+
manager = JsonManager.create!(name: 'Boss')
|
|
503
|
+
manager.json_employees.create!(name: 'Matt')
|
|
504
|
+
manager_short_json = manager.as_json(properties: :short)
|
|
505
|
+
expect(manager_short_json[:has_left_handed_employee]).to eq(false)
|
|
506
|
+
end
|
|
507
|
+
end
|
|
499
508
|
end
|
|
500
509
|
end
|
|
501
510
|
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Cached JSON boundary contracts' do
|
|
4
|
+
before do
|
|
5
|
+
stub_const('PlainCoverageDocument', Class.new)
|
|
6
|
+
PlainCoverageDocument.class_eval do
|
|
7
|
+
include Mongoid::Document
|
|
8
|
+
field :name, type: String
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
after do
|
|
13
|
+
PlainCoverageDocument.delete_all
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'serializes criteria for models that do not include CachedJson' do
|
|
17
|
+
document = PlainCoverageDocument.create!(name: 'plain')
|
|
18
|
+
json = PlainCoverageDocument.where(id: document.id).as_json
|
|
19
|
+
expect(json.first['name']).to eq('plain')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'applies configured transforms to uncached fields' do
|
|
23
|
+
stub_const('TransformCoverageDocument', Class.new)
|
|
24
|
+
TransformCoverageDocument.class_eval do
|
|
25
|
+
include Mongoid::Document
|
|
26
|
+
include Mongoid::CachedJson
|
|
27
|
+
field :value
|
|
28
|
+
json_fields value: { cache: false }
|
|
29
|
+
end
|
|
30
|
+
begin
|
|
31
|
+
Mongoid::CachedJson.config.transform << ->(_field, _definition, value) { value.upcase }
|
|
32
|
+
document = TransformCoverageDocument.create!(value: 'fresh')
|
|
33
|
+
expect(document.as_json).to eq(value: 'FRESH')
|
|
34
|
+
ensure
|
|
35
|
+
TransformCoverageDocument.delete_all
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe Mongoid::CachedJson do
|
|
41
|
+
it 'defines a semantic version' do
|
|
42
|
+
expect(described_class::VERSION).to match(/\A\d+\.\d+\.\d+\z/)
|
|
43
|
+
end
|
|
44
|
+
end
|
data/spec/dalli_spec.rb
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
|
-
require 'active_support/cache/dalli_store'
|
|
3
2
|
|
|
4
|
-
describe
|
|
3
|
+
describe MEMCACHE_STORE_CLASS do
|
|
5
4
|
before :each do
|
|
6
5
|
@cache = Mongoid::CachedJson::Config.cache
|
|
7
6
|
Mongoid::CachedJson.configure do |config|
|
|
8
|
-
config.cache = ActiveSupport::Cache.lookup_store(
|
|
7
|
+
config.cache = ActiveSupport::Cache.lookup_store(MEMCACHE_STORE)
|
|
9
8
|
end
|
|
10
9
|
end
|
|
11
10
|
after :each do
|
|
12
11
|
Mongoid::CachedJson::Config.cache = @cache
|
|
13
12
|
end
|
|
14
13
|
it 'uses dalli_store' do
|
|
15
|
-
expect(Mongoid::CachedJson.config.cache).to be_a
|
|
14
|
+
expect(Mongoid::CachedJson.config.cache).to be_a MEMCACHE_STORE_CLASS
|
|
16
15
|
end
|
|
17
16
|
context 'read_multi' do
|
|
18
17
|
context 'array' do
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Per-field cache policy' do
|
|
4
|
+
before do
|
|
5
|
+
class FieldCacheDocument
|
|
6
|
+
include Mongoid::Document
|
|
7
|
+
include Mongoid::CachedJson
|
|
8
|
+
|
|
9
|
+
field :stored
|
|
10
|
+
field :live
|
|
11
|
+
|
|
12
|
+
json_fields stored: {}, live: { cache: false }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class FieldCacheChild
|
|
16
|
+
include Mongoid::Document
|
|
17
|
+
include Mongoid::CachedJson
|
|
18
|
+
|
|
19
|
+
field :value
|
|
20
|
+
|
|
21
|
+
json_fields value: {}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class FieldCacheParent
|
|
25
|
+
include Mongoid::Document
|
|
26
|
+
include Mongoid::CachedJson
|
|
27
|
+
|
|
28
|
+
field :name
|
|
29
|
+
attr_accessor :child
|
|
30
|
+
|
|
31
|
+
json_fields name: {}, child: { type: :reference, cache: false }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
after do
|
|
36
|
+
[FieldCacheDocument, FieldCacheChild, FieldCacheParent].each(&:delete_all)
|
|
37
|
+
[:FieldCacheDocument, :FieldCacheChild, :FieldCacheParent].each { |name| Object.send(:remove_const, name) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
[true, false].each do |bulk|
|
|
41
|
+
context "with bulk reads #{bulk}" do
|
|
42
|
+
before do
|
|
43
|
+
@store = ActiveSupport::Cache::MemoryStore.new
|
|
44
|
+
@store.singleton_class.send(:undef_method, :read_multi) unless bulk
|
|
45
|
+
Mongoid::CachedJson.config.cache = @store
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'recomputes an individual callable field without refreshing cached siblings' do
|
|
49
|
+
document = FieldCacheDocument.create!(stored: 'stored', live: 'first')
|
|
50
|
+
expect(document.as_json).to eq(stored: 'stored', live: 'first')
|
|
51
|
+
|
|
52
|
+
document.stored = 'unsaved'
|
|
53
|
+
document.live = 'second'
|
|
54
|
+
expect(document.as_json).to eq(stored: 'stored', live: 'second')
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'bypasses the referenced model cache only for this field' do
|
|
58
|
+
parent = FieldCacheParent.create!(name: 'parent')
|
|
59
|
+
child = FieldCacheChild.create!(value: 'first')
|
|
60
|
+
parent.child = child
|
|
61
|
+
options = { properties: :short, version: :unspecified, is_top_level_json: false }
|
|
62
|
+
key = FieldCacheChild.cached_json_key(options, FieldCacheChild, child.id)
|
|
63
|
+
|
|
64
|
+
expect(parent.as_json).to eq(name: 'parent', child: { value: 'first' })
|
|
65
|
+
expect(@store.read(key)).to be_nil
|
|
66
|
+
|
|
67
|
+
child.value = 'second'
|
|
68
|
+
expect(parent.as_json).to eq(name: 'parent', child: { value: 'second' })
|
|
69
|
+
expect(@store.read(key)).to be_nil
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Per-model cache policy' do
|
|
4
|
+
before do
|
|
5
|
+
class UncachedDocument
|
|
6
|
+
include Mongoid::Document
|
|
7
|
+
include Mongoid::CachedJson
|
|
8
|
+
field :value
|
|
9
|
+
json_fields disable_caching: true, value: {}
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class CachedContainer
|
|
13
|
+
include Mongoid::Document
|
|
14
|
+
include Mongoid::CachedJson
|
|
15
|
+
field :value
|
|
16
|
+
attr_accessor :child
|
|
17
|
+
|
|
18
|
+
json_fields value: {}, child: { type: :reference }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
after do
|
|
23
|
+
[UncachedDocument, CachedContainer].each(&:delete_all)
|
|
24
|
+
[:UncachedDocument, :CachedContainer].each { |name| Object.send(:remove_const, name) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
[true, false].each do |bulk|
|
|
28
|
+
context "with bulk reads #{bulk}" do
|
|
29
|
+
before do
|
|
30
|
+
@store = ActiveSupport::Cache::MemoryStore.new
|
|
31
|
+
@store.singleton_class.send(:undef_method, :read_multi) unless bulk
|
|
32
|
+
Mongoid::CachedJson.config.cache = @store
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'does not read or write shared cache for a disabled model' do
|
|
36
|
+
document = UncachedDocument.create!(value: 'first')
|
|
37
|
+
expect(@store).not_to receive(:read_multi)
|
|
38
|
+
expect(@store).not_to receive(:fetch)
|
|
39
|
+
expect(@store).not_to receive(:write)
|
|
40
|
+
expect(document.as_json).to eq(value: 'first')
|
|
41
|
+
document.value = 'second'
|
|
42
|
+
expect([document].as_json).to eq([{ value: 'second' }])
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'keeps the parent cached while recomputing a disabled child' do
|
|
46
|
+
parent = CachedContainer.create!(value: 'parent')
|
|
47
|
+
child = UncachedDocument.create!(value: 'first')
|
|
48
|
+
parent.child = child
|
|
49
|
+
expect(parent.as_json).to eq(value: 'parent', child: { value: 'first' })
|
|
50
|
+
parent.value = 'not saved'
|
|
51
|
+
child.value = 'second'
|
|
52
|
+
expect(parent.as_json).to eq(value: 'parent', child: { value: 'second' })
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'does not disable caching on other models' do
|
|
56
|
+
expect(CachedContainer.cached_json_disable_caching).to be_falsey
|
|
57
|
+
document = CachedContainer.create!(value: 'first')
|
|
58
|
+
document.as_json
|
|
59
|
+
document.value = 'second'
|
|
60
|
+
expect(document.as_json[:value]).to eq('first')
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -68,7 +68,8 @@ describe Mongoid::Criteria do
|
|
|
68
68
|
)
|
|
69
69
|
# read_multi returned only 2 of 3 things, don't call fetch, just store the third value
|
|
70
70
|
expect(Mongoid::CachedJson.config.cache).not_to receive(:fetch)
|
|
71
|
-
|
|
71
|
+
expected_json = { name: 'screwdriver' }
|
|
72
|
+
expect(Mongoid::CachedJson.config.cache).to receive(:write).with("as_json/unspecified/Tool/#{tool2.id}/all/true", expected_json)
|
|
72
73
|
expect(tool_box.tools.as_json(properties: :all)).to eq([
|
|
73
74
|
{ tool_box: { x: :y }, x: :y },
|
|
74
75
|
{ tool_box: { x: :y }, name: 'screwdriver' }
|