jbuilder 0.7.0 → 0.8.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/Gemfile.lock +1 -1
- data/README.md +4 -4
- data/jbuilder-0.7.0.gem +0 -0
- data/jbuilder.gemspec +1 -1
- data/lib/jbuilder.rb +30 -47
- data/lib/jbuilder_template.rb +16 -0
- data/test/jbuilder_template_test.rb +63 -1
- data/test/jbuilder_test.rb +78 -77
- metadata +5 -4
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Jbuilder.encode do |json|
|
|
8
8
|
json.content format_content(@message.content)
|
9
9
|
json.(@message, :created_at, :updated_at)
|
10
10
|
|
11
|
-
json.author do
|
11
|
+
json.author do
|
12
12
|
json.name @message.creator.name.familiar
|
13
13
|
json.email_address @message.creator.email_address_with_name
|
14
14
|
json.url url_for(@message.creator, format: :json)
|
@@ -20,7 +20,7 @@ Jbuilder.encode do |json|
|
|
20
20
|
|
21
21
|
json.comments @message.comments, :content, :created_at
|
22
22
|
|
23
|
-
json.attachments @message.attachments do |
|
23
|
+
json.attachments @message.attachments do |attachment|
|
24
24
|
json.filename attachment.filename
|
25
25
|
json.url url_for(attachment)
|
26
26
|
end
|
@@ -59,7 +59,7 @@ Top level arrays can be handled directly. Useful for index and other collection
|
|
59
59
|
|
60
60
|
``` ruby
|
61
61
|
# @people = People.all
|
62
|
-
json.array!(@people) do |
|
62
|
+
json.array!(@people) do |person|
|
63
63
|
json.name person.name
|
64
64
|
json.age calculate_age(person.birthday)
|
65
65
|
end
|
@@ -101,7 +101,7 @@ You can either use Jbuilder stand-alone or directly as an ActionView template la
|
|
101
101
|
json.content format_content(@message.content)
|
102
102
|
json.(@message, :created_at, :updated_at)
|
103
103
|
|
104
|
-
json.author do
|
104
|
+
json.author do
|
105
105
|
json.name @message.creator.name.familiar
|
106
106
|
json.email_address @message.creator.email_address_with_name
|
107
107
|
json.url url_for(@message.creator, format: :json)
|
data/jbuilder-0.7.0.gem
ADDED
Binary file
|
data/jbuilder.gemspec
CHANGED
data/lib/jbuilder.rb
CHANGED
@@ -62,7 +62,7 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
62
62
|
#
|
63
63
|
# You can also pass a block for nested attributes
|
64
64
|
#
|
65
|
-
# json.set!(:author) do
|
65
|
+
# json.set!(:author) do
|
66
66
|
# json.name "David"
|
67
67
|
# json.age 32
|
68
68
|
# end
|
@@ -70,7 +70,7 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
70
70
|
# { "author": { "name": "David", "age": 32 } }
|
71
71
|
def set!(key, value = nil)
|
72
72
|
if ::Kernel::block_given?
|
73
|
-
_set_value(key,
|
73
|
+
_set_value(key, _scope { yield self })
|
74
74
|
else
|
75
75
|
_set_value(key, value)
|
76
76
|
end
|
@@ -83,7 +83,7 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
83
83
|
# Example:
|
84
84
|
#
|
85
85
|
# json.key_format! :upcase
|
86
|
-
# json.author do
|
86
|
+
# json.author do
|
87
87
|
# json.name "David"
|
88
88
|
# json.age 32
|
89
89
|
# end
|
@@ -117,21 +117,21 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
117
117
|
#
|
118
118
|
# Example:
|
119
119
|
#
|
120
|
-
# json.comments do
|
121
|
-
# json.child! {
|
122
|
-
# json.child! {
|
120
|
+
# json.comments do
|
121
|
+
# json.child! { json.content "hello" }
|
122
|
+
# json.child! { json.content "world" }
|
123
123
|
# end
|
124
124
|
#
|
125
125
|
# { "comments": [ { "content": "hello" }, { "content": "world" } ]}
|
126
126
|
#
|
127
127
|
# More commonly, you'd use the combined iterator, though:
|
128
128
|
#
|
129
|
-
# json.comments(@post.comments) do |
|
129
|
+
# json.comments(@post.comments) do |comment|
|
130
130
|
# json.content comment.formatted_content
|
131
131
|
# end
|
132
132
|
def child!
|
133
133
|
@attributes = [] unless @attributes.is_a? ::Array
|
134
|
-
@attributes <<
|
134
|
+
@attributes << _scope { yield self }
|
135
135
|
end
|
136
136
|
|
137
137
|
# Turns the current element into an array and iterates over the passed collection, adding each iteration as
|
@@ -139,7 +139,7 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
139
139
|
#
|
140
140
|
# Example:
|
141
141
|
#
|
142
|
-
# json.array!(@people) do |
|
142
|
+
# json.array!(@people) do |person|
|
143
143
|
# json.name person.name
|
144
144
|
# json.age calculate_age(person.birthday)
|
145
145
|
# end
|
@@ -148,11 +148,11 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
148
148
|
#
|
149
149
|
# If you are using Ruby 1.9+, you can use the call syntax instead of an explicit extract! call:
|
150
150
|
#
|
151
|
-
# json.(@people) { |
|
151
|
+
# json.(@people) { |person| ... }
|
152
152
|
#
|
153
153
|
# It's generally only needed to use this method for top-level arrays. If you have named arrays, you can do:
|
154
154
|
#
|
155
|
-
# json.people(@people) do |
|
155
|
+
# json.people(@people) do |person|
|
156
156
|
# json.name person.name
|
157
157
|
# json.age calculate_age(person.birthday)
|
158
158
|
# end
|
@@ -165,8 +165,8 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
165
165
|
#
|
166
166
|
# [1,2,3]
|
167
167
|
def array!(collection)
|
168
|
-
@attributes = if block_given?
|
169
|
-
_map_collection(collection) { |element| yield self, element }
|
168
|
+
@attributes = if ::Kernel::block_given?
|
169
|
+
_map_collection(collection) { |element| if ::Proc.new.arity == 2 then yield self, element else yield element end }
|
170
170
|
else
|
171
171
|
collection
|
172
172
|
end
|
@@ -199,7 +199,7 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
199
199
|
|
200
200
|
def call(object = nil, *attributes)
|
201
201
|
if attributes.empty?
|
202
|
-
array!(object
|
202
|
+
array!(object, &::Proc.new)
|
203
203
|
else
|
204
204
|
extract!(object, *attributes)
|
205
205
|
end
|
@@ -215,48 +215,22 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
215
215
|
::MultiJson.encode @attributes
|
216
216
|
end
|
217
217
|
|
218
|
-
# Caches the json constructed within the block passed. Has the same signature as the `cache` helper
|
219
|
-
# method in `ActionView::Helpers::CacheHelper` and so can be used in the same way.
|
220
|
-
#
|
221
|
-
# Example:
|
222
|
-
#
|
223
|
-
# json.cache! ['v1', @person], :expires_in => 10.minutes do |json|
|
224
|
-
# json.extract! @person, :name, :age
|
225
|
-
# end
|
226
|
-
def cache!(key=nil, options={}, &block)
|
227
|
-
cache_key = ::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split("://").last : key, :jbuilder)
|
228
|
-
value = ::Rails.cache.fetch(cache_key, options) do
|
229
|
-
jb = ::Jbuilder.new
|
230
|
-
yield jb
|
231
|
-
jb.attributes!
|
232
|
-
end
|
233
|
-
|
234
|
-
if value.is_a?(::Array)
|
235
|
-
array! value
|
236
|
-
else
|
237
|
-
value.each do |k, v|
|
238
|
-
set! k, v
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
218
|
protected
|
244
219
|
def _set_value(key, value)
|
245
220
|
@attributes[@key_formatter.format(key)] = value
|
246
221
|
end
|
247
222
|
|
248
|
-
|
249
223
|
private
|
250
224
|
def method_missing(method, value = nil, *args)
|
251
225
|
result = if ::Kernel.block_given?
|
252
226
|
if value
|
253
|
-
# json.comments @post.comments { |
|
227
|
+
# json.comments @post.comments { |comment| ... }
|
254
228
|
# { "comments": [ { ... }, { ... } ] }
|
255
|
-
_map_collection(value) { |element| yield self, element }
|
229
|
+
_map_collection(value) { |element| if ::Proc.new.arity == 2 then yield self, element else yield element end }
|
256
230
|
else
|
257
|
-
# json.comments {
|
231
|
+
# json.comments { ... }
|
258
232
|
# { "comments": ... }
|
259
|
-
|
233
|
+
_scope { yield self }
|
260
234
|
end
|
261
235
|
else
|
262
236
|
if args.empty?
|
@@ -282,7 +256,7 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
282
256
|
else
|
283
257
|
# json.author @post.creator, :name, :email_address
|
284
258
|
# { "author": { "name": "David", "email_address": "david@loudthinking.com" } }
|
285
|
-
|
259
|
+
_scope { extract! value, *args }
|
286
260
|
end
|
287
261
|
end
|
288
262
|
end
|
@@ -291,11 +265,11 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
291
265
|
|
292
266
|
def _map_collection(collection)
|
293
267
|
collection.each.map do |element|
|
294
|
-
|
268
|
+
_scope { yield element }
|
295
269
|
end
|
296
270
|
end
|
297
271
|
|
298
|
-
def
|
272
|
+
def _scope
|
299
273
|
parent_attributes, parent_formatter = @attributes, @key_formatter
|
300
274
|
@attributes = ::ActiveSupport::OrderedHash.new
|
301
275
|
yield
|
@@ -303,6 +277,15 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
303
277
|
ensure
|
304
278
|
@attributes, @key_formatter = parent_attributes, parent_formatter
|
305
279
|
end
|
280
|
+
|
281
|
+
def _merge(hash_or_array)
|
282
|
+
if hash_or_array.is_a?(::Array)
|
283
|
+
@attributes = [] unless @attributes.is_a? ::Array
|
284
|
+
@attributes.concat(hash_or_array)
|
285
|
+
else
|
286
|
+
@attributes.update(hash_or_array)
|
287
|
+
end
|
288
|
+
end
|
306
289
|
end
|
307
290
|
|
308
291
|
require "jbuilder_template" if defined?(ActionView::Template)
|
data/lib/jbuilder_template.rb
CHANGED
@@ -14,6 +14,22 @@ class JbuilderTemplate < Jbuilder
|
|
14
14
|
@context.render(options, locals.merge(:json => self))
|
15
15
|
end
|
16
16
|
end
|
17
|
+
|
18
|
+
# Caches the json constructed within the block passed. Has the same signature as the `cache` helper
|
19
|
+
# method in `ActionView::Helpers::CacheHelper` and so can be used in the same way.
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
#
|
23
|
+
# json.cache! ['v1', @person], :expires_in => 10.minutes do
|
24
|
+
# json.extract! @person, :name, :age
|
25
|
+
# end
|
26
|
+
def cache!(key=nil, options={}, &block)
|
27
|
+
cache_key = ::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split("://").last : key, :jbuilder)
|
28
|
+
value = ::Rails.cache.fetch(cache_key, options) do
|
29
|
+
_scope { yield self }
|
30
|
+
end
|
31
|
+
_merge(value)
|
32
|
+
end
|
17
33
|
end
|
18
34
|
|
19
35
|
class JbuilderHandler
|
@@ -1,9 +1,32 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'action_view'
|
3
3
|
require 'action_view/testing/resolvers'
|
4
|
+
require 'active_support/cache'
|
4
5
|
|
5
6
|
require 'jbuilder'
|
6
7
|
|
8
|
+
module Rails
|
9
|
+
class Cache
|
10
|
+
def initialize
|
11
|
+
@cache = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def write(k, v, opt={})
|
15
|
+
@cache[k] = v
|
16
|
+
end
|
17
|
+
|
18
|
+
def read(k, opt={})
|
19
|
+
@cache[k]
|
20
|
+
end
|
21
|
+
|
22
|
+
def fetch(k, opt={}, &block)
|
23
|
+
@cache[k] || @cache[k] = block.call
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.cache; @cache ||= Cache.new; end
|
28
|
+
end
|
29
|
+
|
7
30
|
class JbuilderTemplateTest < ActionView::TestCase
|
8
31
|
def partials
|
9
32
|
{ "_partial.json.jbuilder" => 'json.content "hello"' }
|
@@ -36,7 +59,7 @@ class JbuilderTemplateTest < ActionView::TestCase
|
|
36
59
|
json = render_jbuilder <<-JBUILDER
|
37
60
|
json.key_format! :upcase
|
38
61
|
json.level1 "one"
|
39
|
-
json.level2 do
|
62
|
+
json.level2 do
|
40
63
|
json.value "two"
|
41
64
|
end
|
42
65
|
JBUILDER
|
@@ -53,4 +76,43 @@ class JbuilderTemplateTest < ActionView::TestCase
|
|
53
76
|
|
54
77
|
assert_equal "hello", MultiJson.load(json)["content"]
|
55
78
|
end
|
79
|
+
|
80
|
+
test "fragment caching a JSON object" do
|
81
|
+
json = render_jbuilder <<-JBUILDER
|
82
|
+
json.cache!("cachekey") do
|
83
|
+
json.name "Cache"
|
84
|
+
end
|
85
|
+
JBUILDER
|
86
|
+
|
87
|
+
Rails.cache.read("jbuilder/cachekey").tap do |parsed|
|
88
|
+
assert_equal "Cache", parsed['name']
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
test "fragment caching deserializes a JSON object" do
|
93
|
+
Rails.cache.write("jbuilder/cachekey", {'name' => "Something"})
|
94
|
+
json = render_jbuilder <<-JBUILDER
|
95
|
+
json.cache!("cachekey") do
|
96
|
+
json.name "Cache"
|
97
|
+
end
|
98
|
+
JBUILDER
|
99
|
+
|
100
|
+
MultiJson.load(json).tap do |parsed|
|
101
|
+
assert_equal "Something", parsed['name']
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
test "fragment caching deserializes an array" do
|
106
|
+
Rails.cache.write("jbuilder/cachekey", ["a", "b", "c"])
|
107
|
+
json = render_jbuilder <<-JBUILDER
|
108
|
+
json.cache!("cachekey") do
|
109
|
+
json.array! ['1', '2', '3']
|
110
|
+
end
|
111
|
+
JBUILDER
|
112
|
+
|
113
|
+
MultiJson.load(json).tap do |parsed|
|
114
|
+
assert_equal ["a", "b", "c"], parsed
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
56
118
|
end
|
data/test/jbuilder_test.rb
CHANGED
@@ -1,32 +1,9 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'active_support/test_case'
|
3
|
-
require 'active_support/cache'
|
4
3
|
require 'active_support/inflector'
|
5
4
|
|
6
5
|
require 'jbuilder'
|
7
6
|
|
8
|
-
module Rails
|
9
|
-
class Cache
|
10
|
-
def initialize
|
11
|
-
@cache = {}
|
12
|
-
end
|
13
|
-
|
14
|
-
def write(k, v, opt={})
|
15
|
-
@cache[k] = v
|
16
|
-
end
|
17
|
-
|
18
|
-
def read(k, opt={})
|
19
|
-
@cache[k]
|
20
|
-
end
|
21
|
-
|
22
|
-
def fetch(k, opt={}, &block)
|
23
|
-
@cache[k] || @cache[k] = block.call
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.cache; @cache ||= Cache.new; end
|
28
|
-
end
|
29
|
-
|
30
7
|
class JbuilderTest < ActiveSupport::TestCase
|
31
8
|
test "single key" do
|
32
9
|
json = Jbuilder.encode do |json|
|
@@ -110,7 +87,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
110
87
|
|
111
88
|
test "nesting single child with block" do
|
112
89
|
json = Jbuilder.encode do |json|
|
113
|
-
json.author do
|
90
|
+
json.author do
|
114
91
|
json.name "David"
|
115
92
|
json.age 32
|
116
93
|
end
|
@@ -124,9 +101,9 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
124
101
|
|
125
102
|
test "nesting multiple children with block" do
|
126
103
|
json = Jbuilder.encode do |json|
|
127
|
-
json.comments do
|
128
|
-
json.child! {
|
129
|
-
json.child! {
|
104
|
+
json.comments do
|
105
|
+
json.child! { json.content "hello" }
|
106
|
+
json.child! { json.content "world" }
|
130
107
|
end
|
131
108
|
end
|
132
109
|
|
@@ -186,6 +163,22 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
186
163
|
test "nesting multiple children from array with inline loop" do
|
187
164
|
comments = [ Struct.new(:content, :id).new("hello", 1), Struct.new(:content, :id).new("world", 2) ]
|
188
165
|
|
166
|
+
json = Jbuilder.encode do |json|
|
167
|
+
json.comments comments do |comment|
|
168
|
+
json.content comment.content
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
MultiJson.load(json).tap do |parsed|
|
173
|
+
assert_equal ["content"], parsed["comments"].first.keys
|
174
|
+
assert_equal "hello", parsed["comments"].first["content"]
|
175
|
+
assert_equal "world", parsed["comments"].second["content"]
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
test "nesting multiple children from array with inline loop with old api" do
|
180
|
+
comments = [ Struct.new(:content, :id).new("hello", 1), Struct.new(:content, :id).new("world", 2) ]
|
181
|
+
|
189
182
|
json = Jbuilder.encode do |json|
|
190
183
|
json.comments comments do |json, comment|
|
191
184
|
json.content comment.content
|
@@ -202,6 +195,21 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
202
195
|
test "nesting multiple children from array with inline loop on root" do
|
203
196
|
comments = [ Struct.new(:content, :id).new("hello", 1), Struct.new(:content, :id).new("world", 2) ]
|
204
197
|
|
198
|
+
json = Jbuilder.encode do |json|
|
199
|
+
json.call(comments) do |comment|
|
200
|
+
json.content comment.content
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
MultiJson.load(json).tap do |parsed|
|
205
|
+
assert_equal "hello", parsed.first["content"]
|
206
|
+
assert_equal "world", parsed.second["content"]
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
test "nesting multiple children from array with inline loop on root with old api" do
|
211
|
+
comments = [ Struct.new(:content, :id).new("hello", 1), Struct.new(:content, :id).new("world", 2) ]
|
212
|
+
|
205
213
|
json = Jbuilder.encode do |json|
|
206
214
|
json.call(comments) do |json, comment|
|
207
215
|
json.content comment.content
|
@@ -216,13 +224,13 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
216
224
|
|
217
225
|
test "array nested inside nested hash" do
|
218
226
|
json = Jbuilder.encode do |json|
|
219
|
-
json.author do
|
227
|
+
json.author do
|
220
228
|
json.name "David"
|
221
229
|
json.age 32
|
222
230
|
|
223
|
-
json.comments do
|
224
|
-
json.child! {
|
225
|
-
json.child! {
|
231
|
+
json.comments do
|
232
|
+
json.child! { json.content "hello" }
|
233
|
+
json.child! { json.content "world" }
|
226
234
|
end
|
227
235
|
end
|
228
236
|
end
|
@@ -235,10 +243,10 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
235
243
|
|
236
244
|
test "array nested inside array" do
|
237
245
|
json = Jbuilder.encode do |json|
|
238
|
-
json.comments do
|
239
|
-
json.child! do
|
240
|
-
json.authors do
|
241
|
-
json.child! do
|
246
|
+
json.comments do
|
247
|
+
json.child! do
|
248
|
+
json.authors do
|
249
|
+
json.child! do
|
242
250
|
json.name "david"
|
243
251
|
end
|
244
252
|
end
|
@@ -249,6 +257,36 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
249
257
|
assert_equal "david", MultiJson.load(json)["comments"].first["authors"].first["name"]
|
250
258
|
end
|
251
259
|
|
260
|
+
test "directly set an array nested in another array" do
|
261
|
+
data = [ { :department => 'QA', :not_in_json => 'hello', :names => ['John', 'David'] } ]
|
262
|
+
json = Jbuilder.encode do |json|
|
263
|
+
json.array! data do |object|
|
264
|
+
json.department object[:department]
|
265
|
+
json.names do
|
266
|
+
json.array! object[:names]
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
assert_equal 'David', MultiJson.load(json)[0]['names'].last
|
272
|
+
assert_not_equal 'hello', MultiJson.load(json)[0]['not_in_json']
|
273
|
+
end
|
274
|
+
|
275
|
+
test "directly set an array nested in another array with old api" do
|
276
|
+
data = [ { :department => 'QA', :not_in_json => 'hello', :names => ['John', 'David'] } ]
|
277
|
+
json = Jbuilder.encode do |json|
|
278
|
+
json.array! data do |json, object|
|
279
|
+
json.department object[:department]
|
280
|
+
json.names do
|
281
|
+
json.array! object[:names]
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
assert_equal 'David', MultiJson.load(json)[0]['names'].last
|
287
|
+
assert_not_equal 'hello', MultiJson.load(json)[0]['not_in_json']
|
288
|
+
end
|
289
|
+
|
252
290
|
test "nested jbuilder objects" do
|
253
291
|
to_nest = Jbuilder.new
|
254
292
|
to_nest.nested_value "Nested Test"
|
@@ -265,7 +303,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
265
303
|
comments = [ Struct.new(:content, :id).new("hello", 1), Struct.new(:content, :id).new("world", 2) ]
|
266
304
|
|
267
305
|
json = Jbuilder.encode do |json|
|
268
|
-
json.array!(comments) do |
|
306
|
+
json.array!(comments) do |comment|
|
269
307
|
json.content comment.content
|
270
308
|
end
|
271
309
|
end
|
@@ -280,7 +318,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
280
318
|
comments = []
|
281
319
|
|
282
320
|
json = Jbuilder.encode do |json|
|
283
|
-
json.array!(comments) do |
|
321
|
+
json.array!(comments) do |comment|
|
284
322
|
json.content comment.content
|
285
323
|
end
|
286
324
|
end
|
@@ -298,7 +336,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
298
336
|
|
299
337
|
test "dynamically set a key/nested child with block" do
|
300
338
|
json = Jbuilder.encode do |json|
|
301
|
-
json.set!(:author) do
|
339
|
+
json.set!(:author) do
|
302
340
|
json.name "David"
|
303
341
|
json.age 32
|
304
342
|
end
|
@@ -357,7 +395,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
357
395
|
json = Jbuilder.new
|
358
396
|
json.key_format! :upcase
|
359
397
|
json.level1 "one"
|
360
|
-
json.level2 do
|
398
|
+
json.level2 do
|
361
399
|
json.value "two"
|
362
400
|
end
|
363
401
|
|
@@ -368,7 +406,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
368
406
|
|
369
407
|
test "key_format! resets after child element" do
|
370
408
|
json = Jbuilder.new
|
371
|
-
json.level2 do
|
409
|
+
json.level2 do
|
372
410
|
json.key_format! :upcase
|
373
411
|
json.value "two"
|
374
412
|
end
|
@@ -419,41 +457,4 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
419
457
|
assert_equal [], Jbuilder.send(:class_variable_get, "@@key_formatter").instance_variable_get("@cache").keys
|
420
458
|
end
|
421
459
|
|
422
|
-
test "fragment caching a JSON object" do
|
423
|
-
json = Jbuilder.encode do |json|
|
424
|
-
json.cache!("cachekey") do |json|
|
425
|
-
json.name "Cache"
|
426
|
-
end
|
427
|
-
end
|
428
|
-
|
429
|
-
Rails.cache.read("jbuilder/cachekey").tap do |parsed|
|
430
|
-
assert_equal "Cache", parsed['name']
|
431
|
-
end
|
432
|
-
end
|
433
|
-
|
434
|
-
test "fragment caching deserializes a JSON object" do
|
435
|
-
Rails.cache.write("jbuilder/cachekey", {'name' => "Something"})
|
436
|
-
json = Jbuilder.encode do |json|
|
437
|
-
json.cache!("cachekey") do |json|
|
438
|
-
json.name "Cache"
|
439
|
-
end
|
440
|
-
end
|
441
|
-
|
442
|
-
JSON.parse(json).tap do |parsed|
|
443
|
-
assert_equal "Something", parsed['name']
|
444
|
-
end
|
445
|
-
end
|
446
|
-
|
447
|
-
test "fragment caching deserializes an array" do
|
448
|
-
Rails.cache.write("jbuilder/cachekey", ["a", "b", "c"])
|
449
|
-
json = Jbuilder.encode do |json|
|
450
|
-
json.cache!("cachekey") do |json|
|
451
|
-
json.array! ['1', '2', '3']
|
452
|
-
end
|
453
|
-
end
|
454
|
-
|
455
|
-
JSON.parse(json).tap do |parsed|
|
456
|
-
assert_equal ["a", "b", "c"], parsed
|
457
|
-
end
|
458
|
-
end
|
459
460
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jbuilder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152108040 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152108040
|
25
25
|
description:
|
26
26
|
email: david@37signals.com
|
27
27
|
executables: []
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- ./Gemfile
|
32
32
|
- ./Gemfile.lock
|
33
33
|
- ./jbuilder-0.6.0.gem
|
34
|
+
- ./jbuilder-0.7.0.gem
|
34
35
|
- ./jbuilder.gemspec
|
35
36
|
- ./lib/jbuilder.rb
|
36
37
|
- ./lib/jbuilder_template.rb
|