jbuilder 1.5.0 → 1.5.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 +8 -8
- data/README.md +15 -3
- data/jbuilder.gemspec +1 -1
- data/lib/jbuilder/jbuilder_template.rb +8 -1
- data/test/jbuilder_template_test.rb +10 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Njk5MjUxMDExMjc4YWZkZGY3ODM0MWFhY2FhYTE0MWUyNTE2YjdjNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YjViMzY4ZmE2ZTVmNGVmZGNiZjM1N2JiOGZlN2NlNjgxYTA3YzIxNg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjRjMDgyNWI1OGU2ZGFjZGZiMWRjZmYyOWZkYzI2N2NhY2MzOWY1MjYwNjM4
|
10
|
+
ZGMzNzBlN2RlYjM0MWZhMjdlZGZhODJhZDIyYTA3MjQ0YjVhYjY2YTY2NjY5
|
11
|
+
MTA3N2RkZDE3NTRlOGQxMzIyZTQ0Mzg1M2NlNDNmYWQwYzY0NDI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YThmNWY1NDU5MmUwMTY0MTgzMDNlYWIyZDI2ODI4MWM2YTJkOWRjZjg1Yzky
|
14
|
+
YWRlYmUxZGVlZmVkMGQxMjJkZGMzNjlkNmIxNmMzYTA0NzVjYzgwNmY2ZDE5
|
15
|
+
NmY2NmIyNjI3ODJlN2EwMGVjNjhjYjM2YTMyMjIzZDhlNWZiMWU=
|
data/README.md
CHANGED
@@ -86,7 +86,6 @@ json.array! @people, :id, :name
|
|
86
86
|
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
|
87
87
|
```
|
88
88
|
|
89
|
-
|
90
89
|
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
|
91
90
|
|
92
91
|
``` ruby
|
@@ -155,6 +154,10 @@ json.partial! 'posts/post', collection: @posts, as: :post
|
|
155
154
|
# or
|
156
155
|
|
157
156
|
json.partial! partial: 'posts/post', collection: @posts, as: :post
|
157
|
+
|
158
|
+
# or
|
159
|
+
|
160
|
+
json.comments @post.comments, partial: 'comment/comment', as: :comment
|
158
161
|
```
|
159
162
|
|
160
163
|
You can explicitly make Jbuilder object return null if you want:
|
@@ -171,10 +174,18 @@ json.author do
|
|
171
174
|
end
|
172
175
|
```
|
173
176
|
|
177
|
+
Fragment caching is supported, it uses `Rails.cache` and works like caching in HTML templates:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
json.cache! ['v1', @person], expires_in: 10.minutes do
|
181
|
+
json.extract! @person, :name, :age
|
182
|
+
end
|
183
|
+
```
|
184
|
+
|
174
185
|
Keys can be auto formatted using `key_format!`, this can be used to convert keynames from the standard ruby_format to CamelCase:
|
175
186
|
|
176
187
|
``` ruby
|
177
|
-
json.key_format! :
|
188
|
+
json.key_format! camelize: :lower
|
178
189
|
json.first_name 'David'
|
179
190
|
|
180
191
|
# => { "firstName": "David" }
|
@@ -183,11 +194,12 @@ json.first_name 'David'
|
|
183
194
|
You can set this globally with the class method `key_format` (from inside your environment.rb for example):
|
184
195
|
|
185
196
|
``` ruby
|
186
|
-
Jbuilder.key_format :
|
197
|
+
Jbuilder.key_format camelize: :lower
|
187
198
|
```
|
188
199
|
|
189
200
|
Libraries similar to this in some form or another include:
|
190
201
|
|
202
|
+
* Active Model Serializers: https://github.com/rails-api/active_model_serializers
|
191
203
|
* RABL: https://github.com/nesquena/rabl
|
192
204
|
* JsonBuilder: https://github.com/nov/jsonbuilder
|
193
205
|
* JSON Builder: https://github.com/dewski/json_builder
|
data/jbuilder.gemspec
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
require 'action_dispatch/http/mime_type'
|
2
2
|
|
3
3
|
class JbuilderTemplate < Jbuilder
|
4
|
+
class << self
|
5
|
+
attr_accessor :template_lookup_options
|
6
|
+
end
|
7
|
+
|
8
|
+
self.template_lookup_options = { :handlers => [:jbuilder] }
|
9
|
+
|
4
10
|
def initialize(context, *args, &block)
|
5
11
|
@context = context
|
6
12
|
super(*args, &block)
|
@@ -54,7 +60,8 @@ class JbuilderTemplate < Jbuilder
|
|
54
60
|
|
55
61
|
protected
|
56
62
|
def _handle_partial_options(options)
|
57
|
-
options.reverse_merge!
|
63
|
+
options.reverse_merge! :locals => {}
|
64
|
+
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
|
58
65
|
collection = options.delete(:collection)
|
59
66
|
as = options[:as]
|
60
67
|
|
@@ -52,8 +52,9 @@ class JbuilderTemplateTest < ActionView::TestCase
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
def assert_collection_rendered(json)
|
55
|
+
def assert_collection_rendered(json, context = nil)
|
56
56
|
result = MultiJson.load(json)
|
57
|
+
result = result.fetch(context) if context
|
57
58
|
|
58
59
|
assert_equal 10, result.length
|
59
60
|
assert_equal Array, result.class
|
@@ -125,6 +126,14 @@ class JbuilderTemplateTest < ActionView::TestCase
|
|
125
126
|
assert_collection_rendered json
|
126
127
|
end
|
127
128
|
|
129
|
+
test 'render array if partials as a value' do
|
130
|
+
json = render_jbuilder <<-JBUILDER
|
131
|
+
json.posts BLOG_POST_COLLECTION, :partial => 'blog_post', :as => :blog_post
|
132
|
+
JBUILDER
|
133
|
+
|
134
|
+
assert_collection_rendered json, 'posts'
|
135
|
+
end
|
136
|
+
|
128
137
|
test 'fragment caching a JSON object' do
|
129
138
|
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
|
130
139
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jbuilder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|