mongoid-cached-json 1.4.3 → 1.5.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.
- data/README.md +32 -0
- data/lib/mongoid-cached-json/cached_json.rb +5 -5
- data/lib/mongoid-cached-json/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -101,6 +101,38 @@ Defining Fields
|
|
101
101
|
* `:properties` can be one of `:short`, `:public`, `:all`, in this order
|
102
102
|
* `:version` can be a single version for this field to appear in
|
103
103
|
* `:versions` can be an array of versions for this field to appear in
|
104
|
+
* `:reference_properties` can be one of `:short`, `:public`, `:all`, default will select the reference properties format dynamically (see below)
|
105
|
+
|
106
|
+
Reference Properties
|
107
|
+
--------------------
|
108
|
+
|
109
|
+
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:
|
110
|
+
|
111
|
+
* Use the value of the `:reference_properties` option, if specified.
|
112
|
+
* For `:short` JSON, use `:short`.
|
113
|
+
* For `:public` JSON, use `:short`.
|
114
|
+
* For `:all` JSON, use `:all`.
|
115
|
+
|
116
|
+
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.
|
117
|
+
|
118
|
+
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.
|
119
|
+
|
120
|
+
``` ruby
|
121
|
+
class Person
|
122
|
+
include Mongoid::Document
|
123
|
+
include Mongoid::CachedJson
|
124
|
+
|
125
|
+
field :name
|
126
|
+
field :ssn
|
127
|
+
has_and_belongs_to_many :friends, :class_name => "Person"
|
128
|
+
|
129
|
+
json_fields \
|
130
|
+
:name => {},
|
131
|
+
:ssn => { :properties => :all }
|
132
|
+
friends: { :properties => :public, :reference_properties => :short }
|
133
|
+
|
134
|
+
end
|
135
|
+
```
|
104
136
|
|
105
137
|
Versioning
|
106
138
|
----------
|
@@ -20,7 +20,7 @@ module Mongoid
|
|
20
20
|
# @since 1.0
|
21
21
|
def json_fields(defs)
|
22
22
|
self.hide_as_child_json_when = defs.delete(:hide_as_child_json_when) || lambda { |a| false }
|
23
|
-
self.all_json_properties = [:short, :public, :all]
|
23
|
+
self.all_json_properties = [ :short, :public, :all ]
|
24
24
|
cached_json_defs = Hash[defs.map { |k,v| [k, { :type => :callable, :properties => :short, :definition => k }.merge(v)] }]
|
25
25
|
self.cached_json_field_defs = {}
|
26
26
|
self.cached_json_reference_defs = {}
|
@@ -30,10 +30,10 @@ module Mongoid
|
|
30
30
|
end.flatten.compact.uniq
|
31
31
|
self.all_json_properties.each_with_index do |property, i|
|
32
32
|
self.cached_json_field_defs[property] = Hash[cached_json_defs.find_all do |field, definition|
|
33
|
-
self.all_json_properties.find_index(definition[:properties]) <= i
|
33
|
+
self.all_json_properties.find_index(definition[:properties]) <= i && definition[:type] == :callable
|
34
34
|
end]
|
35
35
|
self.cached_json_reference_defs[property] = Hash[cached_json_defs.find_all do |field, definition|
|
36
|
-
self.all_json_properties.find_index(definition[:properties]) <= i
|
36
|
+
self.all_json_properties.find_index(definition[:properties]) <= i && definition[:type] == :reference
|
37
37
|
end]
|
38
38
|
# If the field is a reference and is just specified as a symbol, reflect on it to get metadata
|
39
39
|
self.cached_json_reference_defs[property].to_a.each do |field, definition|
|
@@ -89,9 +89,9 @@ module Mongoid
|
|
89
89
|
reference_defs = clazz.cached_json_reference_defs[options[:properties]]
|
90
90
|
if !reference_defs.empty?
|
91
91
|
object_reference = clazz.where({ :_id => id }).first if !object_reference
|
92
|
-
if object_reference
|
92
|
+
if object_reference && (is_top_level_json || options[:properties] == :all || !clazz.hide_as_child_json_when.call(object_reference))
|
93
93
|
json.merge!(Hash[reference_defs.map do |field, definition|
|
94
|
-
json_properties_type = (options[:properties] == :all) ? :all : :short
|
94
|
+
json_properties_type = definition[:reference_properties] || ((options[:properties] == :all) ? :all : :short)
|
95
95
|
reference_keys, reference = clazz.resolve_json_reference(options.merge({ :properties => json_properties_type, :is_top_level_json => false}), object_reference, field, definition)
|
96
96
|
if (reference.is_a?(Hash) && ref = reference[:_ref])
|
97
97
|
ref[:_parent] = json
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-cached-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-03-13 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -174,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
174
174
|
version: '0'
|
175
175
|
segments:
|
176
176
|
- 0
|
177
|
-
hash:
|
177
|
+
hash: 4082728450150957168
|
178
178
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
179
|
none: false
|
180
180
|
requirements:
|
@@ -183,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
183
183
|
version: '0'
|
184
184
|
requirements: []
|
185
185
|
rubyforge_project:
|
186
|
-
rubygems_version: 1.8.
|
186
|
+
rubygems_version: 1.8.25
|
187
187
|
signing_key:
|
188
188
|
specification_version: 3
|
189
189
|
summary: Effective model-level JSON caching for the Mongoid ODM.
|