jwtbuilder 0.0.1 → 2.2.12.jwt
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/.gitignore +5 -0
- data/.travis.yml +28 -0
- data/Appraisals +32 -0
- data/CHANGELOG.md +174 -0
- data/Gemfile +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +260 -0
- data/Rakefile +23 -0
- data/gemfiles/rails_3_0.gemfile +12 -0
- data/gemfiles/rails_3_1.gemfile +12 -0
- data/gemfiles/rails_3_2.gemfile +12 -0
- data/gemfiles/rails_4_0.gemfile +11 -0
- data/gemfiles/rails_4_1.gemfile +11 -0
- data/gemfiles/rails_4_2.gemfile +11 -0
- data/jbuilder.gemspec +18 -0
- data/lib/generators/rails/jbuilder_generator.rb +48 -0
- data/lib/generators/rails/scaffold_controller_generator.rb +12 -0
- data/lib/generators/rails/templates/controller.rb +84 -0
- data/lib/generators/rails/templates/index.json.jbuilder +5 -0
- data/lib/generators/rails/templates/show.json.jbuilder +3 -0
- data/lib/jbuilder/blank.rb +11 -0
- data/lib/jbuilder/dependency_tracker.rb +61 -0
- data/lib/jbuilder/errors.rb +17 -0
- data/lib/jbuilder/jbuilder.rb +7 -0
- data/lib/jbuilder/jbuilder_template.rb +145 -0
- data/lib/jbuilder/key_formatter.rb +34 -0
- data/lib/jbuilder/railtie.rb +21 -0
- data/lib/jbuilder.rb +313 -0
- data/test/jbuilder_dependency_tracker_test.rb +72 -0
- data/test/jbuilder_generator_test.rb +32 -0
- data/test/jbuilder_template_test.rb +343 -0
- data/test/jbuilder_test.rb +686 -0
- data/test/scaffold_controller_generator_test.rb +57 -0
- data/test/test_helper.rb +14 -0
- metadata +48 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b011f5c9d1f2f8174ad2195a3e7205d4733961ad
|
4
|
+
data.tar.gz: c24f48349467dd12f4035144e7285d40bd7af304
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 425a59d835961ce6851055803569809f459093cb56a9551f0e52e65dd853840006d313ebb64809f5668a04f06435a4662b43e34c4b5b5d1ebf7da9b48380bf6b
|
7
|
+
data.tar.gz: 2956b8faa3499cbeb0c66a168688047e6676257ce1c3f280704df8eb5112f8d84b30259ed0d9a85a5478c61aea55acebef7eec13580a1b6c74f898a62be2548e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- 1.9
|
5
|
+
- 2.0
|
6
|
+
- 2.1
|
7
|
+
- 2.2
|
8
|
+
- ruby-head
|
9
|
+
- jruby-19mode
|
10
|
+
- rbx
|
11
|
+
|
12
|
+
gemfile:
|
13
|
+
- gemfiles/rails_3_0.gemfile
|
14
|
+
- gemfiles/rails_3_1.gemfile
|
15
|
+
- gemfiles/rails_3_2.gemfile
|
16
|
+
- gemfiles/rails_4_0.gemfile
|
17
|
+
- gemfiles/rails_4_1.gemfile
|
18
|
+
- gemfiles/rails_4_2.gemfile
|
19
|
+
|
20
|
+
matrix:
|
21
|
+
allow_failures:
|
22
|
+
- rvm: jruby-19mode
|
23
|
+
- rvm: rbx
|
24
|
+
- rvm: ruby-head
|
25
|
+
fast_finish: true
|
26
|
+
|
27
|
+
notifications:
|
28
|
+
email: false
|
data/Appraisals
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
appraise "rails-3-0" do
|
2
|
+
gem "test-unit"
|
3
|
+
gem "railties", "~> 3.0.0"
|
4
|
+
gem "actionpack", "~> 3.0.0"
|
5
|
+
end
|
6
|
+
|
7
|
+
appraise "rails-3-1" do
|
8
|
+
gem "test-unit"
|
9
|
+
gem "railties", "~> 3.1.0"
|
10
|
+
gem "actionpack", "~> 3.1.0"
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise "rails-3-2" do
|
14
|
+
gem "test-unit"
|
15
|
+
gem "railties", "~> 3.2.0"
|
16
|
+
gem "actionpack", "~> 3.2.0"
|
17
|
+
end
|
18
|
+
|
19
|
+
appraise "rails-4-0" do
|
20
|
+
gem "railties", "~> 4.0.0"
|
21
|
+
gem "actionpack", "~> 4.0.0"
|
22
|
+
end
|
23
|
+
|
24
|
+
appraise "rails-4-1" do
|
25
|
+
gem "railties", "~> 4.1.0"
|
26
|
+
gem "actionpack", "~> 4.1.0"
|
27
|
+
end
|
28
|
+
|
29
|
+
appraise "rails-4-2" do
|
30
|
+
gem "railties", "~> 4.2.0"
|
31
|
+
gem "actionpack", "~> 4.2.0"
|
32
|
+
end
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
2.2.12
|
4
|
+
------
|
5
|
+
|
6
|
+
* [Replace explici block calls with yield for performance](https://github.com/rails/jbuilder/commit/3184f941276ad03a071cf977133d1a32302afa47)
|
7
|
+
|
8
|
+
2.2.11
|
9
|
+
------
|
10
|
+
|
11
|
+
* Generate the templates for Rails 5+ [#258](https://github.com/rails/jbuilder/pull/258) [@amatsuda](https://github.com/amatsuda)
|
12
|
+
|
13
|
+
2.2.10
|
14
|
+
------
|
15
|
+
|
16
|
+
* Add Jbuilder::Blank#empty? to tell if attributes are empty [#257](https://github.com/rails/jbuilder/pull/257) [@a2ikm](https://github.com/a2ikm)
|
17
|
+
|
18
|
+
2.2.9
|
19
|
+
-----
|
20
|
+
|
21
|
+
* Support `partial!` call with `locals` option in `JbuilderTemplate` [#251](https://github.com/rails/jbuilder/pull/251)
|
22
|
+
|
23
|
+
2.2.8
|
24
|
+
-----
|
25
|
+
* [Raise ArrayError when trying to add key to an array](https://github.com/rails/jbuilder/commit/869e4be1ad165ce986d8fca78311bdd3ed166087)
|
26
|
+
|
27
|
+
2.2.7
|
28
|
+
-----
|
29
|
+
* [Make Blank object serializable with Marshal](https://github.com/rails/jbuilder/commit/7083f28d8b665aa60d0d1b1927ae88bb5c6290ba)
|
30
|
+
|
31
|
+
2.2.6
|
32
|
+
-----
|
33
|
+
* [Make sure dependency tracker loads after template handler](https://github.com/rails/jbuilder/commit/3ba404b1207b557e14771c90b8832bc01ae67a42)
|
34
|
+
|
35
|
+
2.2.5
|
36
|
+
-----
|
37
|
+
* [Refactor merge block behavior to raise error for unexpected values](https://github.com/rails/jbuilder/commit/4503162fb26f53f613fc83ac081fd244748b6fe9)
|
38
|
+
|
39
|
+
2.2.4
|
40
|
+
-----
|
41
|
+
* [Typecast locals hash key during collection render](https://github.com/rails/jbuilder/commit/a6b0c8651a08e01cb53eee38e211c65423f275f7)
|
42
|
+
|
43
|
+
2.2.3
|
44
|
+
-----
|
45
|
+
* [Move template handler registration into railtie](https://github.com/rails/jbuilder/commit/c8acc5cea6da2a79b7b345adc301cb5ff2517647)
|
46
|
+
* [Do not capture the block where it is possible](https://github.com/rails/jbuilder/commit/973b382c3924cb59fc0e4e25266b18e74d41d646)
|
47
|
+
|
48
|
+
2.2.2
|
49
|
+
-----
|
50
|
+
* [Fix `Jbuilder#merge!` inside block](https://github.com/rails/jbuilder/commit/a7b328552eb0d36315f75bff813bea7eecf8c1d7)
|
51
|
+
|
52
|
+
2.2.1
|
53
|
+
-----
|
54
|
+
* [Fix empty block handling](https://github.com/rails/jbuilder/commit/972a11141403269e9b17b45b0c95f8a9788245ee)
|
55
|
+
|
56
|
+
2.2.0
|
57
|
+
-----
|
58
|
+
* [Allow to skip `array!` iterations by calling `next`](https://github.com/rails/jbuilder/commit/81a63308fb9d5002519dd871f829ccc58067251a)
|
59
|
+
|
60
|
+
2.1.2
|
61
|
+
-----
|
62
|
+
* [Cast array-like objects to array before merging](https://github.com/rails/jbuilder/commit/7b8c8a1cb09b7f3dd26e5643ebbd6b2ec67185db)
|
63
|
+
|
64
|
+
2.1.1
|
65
|
+
-----
|
66
|
+
* [Remove unused file](https://github.com/rails/jbuilder/commit/e49e1047976fac93b8242ab212c7b1a463b70809)
|
67
|
+
|
68
|
+
2.1.0
|
69
|
+
-----
|
70
|
+
* [Blocks and their extract! shortcuts are additive by default](https://github.com/rails/jbuilder/commit/a49390736c5f6e2d7a31111df6531bc28dba9fb1)
|
71
|
+
|
72
|
+
2.0.8
|
73
|
+
-----
|
74
|
+
* [Eliminate circular dependencies](https://github.com/rails/jbuilder/commit/0879484dc74e7be93b695f66e3708ba48cdb1be3)
|
75
|
+
* [Support cache key generation for complex objects](https://github.com/rails/jbuilder/commit/ca9622cca30c1112dd4408fcb2e658849abe1dd5)
|
76
|
+
* [Remove JbuilderProxy class](https://github.com/rails/jbuilder/commit/5877482fc7da3224e42d4f72a1386f7a3a08173b)
|
77
|
+
* [Move KeyFormatter into a separate file](https://github.com/rails/jbuilder/commit/13fee8464ff53ce853030114283c03c135c052b6)
|
78
|
+
* [Move NullError into a separate file](https://github.com/rails/jbuilder/commit/13fee8464ff53ce853030114283c03c135c052b6)
|
79
|
+
|
80
|
+
2.0.7
|
81
|
+
-----
|
82
|
+
* [Add destroy notice to scaffold generator](https://github.com/rails/jbuilder/commit/8448e86f8cdfa0f517bd59576947875775a1d43c)
|
83
|
+
|
84
|
+
2.0.6
|
85
|
+
-----
|
86
|
+
* [Use render short form in controller generator](https://github.com/rails/jbuilder/commit/acf37320a7cea7fcc70c791bc94bd5f46b8349ff)
|
87
|
+
|
88
|
+
2.0.5
|
89
|
+
-----
|
90
|
+
* [Fix edgecase when json is defined as a method](https://github.com/rails/jbuilder/commit/ca711a0c0a5760e26258ce2d93c14bef8fff0ead)
|
91
|
+
|
92
|
+
2.0.4
|
93
|
+
-----
|
94
|
+
* [Add cache_if! to conditionally cache JSON fragments](https://github.com/rails/jbuilder/commit/14a5afd8a2c939a6fd710d355a194c114db96eb2)
|
95
|
+
|
96
|
+
2.0.3
|
97
|
+
-----
|
98
|
+
* [Pass options when calling cache_fragment_name](https://github.com/rails/jbuilder/commit/07c2cc7486fe9ef423d7bc821b83f6d485f330e0)
|
99
|
+
|
100
|
+
2.0.2
|
101
|
+
-----
|
102
|
+
* [Fix Dependency Tracking fail to detect single-quoted partial correctly](https://github.com/rails/jbuilder/commit/448679a6d3098eb34d137f782a05f1767711991a)
|
103
|
+
* [Prevent Dependency Tracker constants leaking into global namespace](https://github.com/rails/jbuilder/commit/3544b288b63f504f46fa8aafd1d17ee198d77536)
|
104
|
+
|
105
|
+
2.0.1
|
106
|
+
-----
|
107
|
+
* [Dependency tracking support for Rails 3 with cache_digest gem](https://github.com/rails/jbuilder/commit/6b471d7a38118e8f7645abec21955ef793401daf)
|
108
|
+
|
109
|
+
2.0.0
|
110
|
+
-----
|
111
|
+
* [Respond to PUT/PATCH API request with :ok](https://github.com/rails/jbuilder/commit/9dbce9c12181e89f8f472ac23c764ffe8438040a)
|
112
|
+
* [Remove Ruby 1.8 support](https://github.com/rails/jbuilder/commit/d53fff42d91f33d662eafc2561c4236687ecf6c9)
|
113
|
+
* [Remove deprecated two argument block call](https://github.com/rails/jbuilder/commit/07a35ee7e79ae4b06dba9dbff5c4e07c1e627218)
|
114
|
+
* [Make Jbuilder object initialize with single hash](https://github.com/rails/jbuilder/commit/38bf551db0189327aaa90b9be010c0d1b792c007)
|
115
|
+
* [Track template dependencies](https://github.com/rails/jbuilder/commit/8e73cea39f60da1384afd687cc8e5e399630d8cc)
|
116
|
+
* [Expose merge! method](https://github.com/rails/jbuilder/commit/0e2eb47f6f3c01add06a1a59b37cdda8baf24f29)
|
117
|
+
|
118
|
+
1.5.3
|
119
|
+
-----
|
120
|
+
* [Generators add `:id` column by default](https://github.com/rails/jbuilder/commit/0b52b86773e48ac2ce35d4155c7b70ad8b3e8937)
|
121
|
+
|
122
|
+
1.5.2
|
123
|
+
-----
|
124
|
+
* [Nil-collection should be treated as empty array](https://github.com/rails/jbuilder/commit/2f700bb00ab663c6b7fcb28d2967aeb989bd43c7)
|
125
|
+
|
126
|
+
1.5.1
|
127
|
+
-----
|
128
|
+
* [Expose template lookup options](https://github.com/rails/jbuilder/commit/404c18dee1af96ac6d8052a04062629ef1db2945)
|
129
|
+
|
130
|
+
1.5.0
|
131
|
+
-----
|
132
|
+
* [Do not perform any caching when `controller.perform_caching` is false](https://github.com/rails/jbuilder/commit/94633facde1ac43580f8cd5e13ae9cc83e1da8f4)
|
133
|
+
* [Add partial collection rendering](https://github.com/rails/jbuilder/commit/e8c10fc885e41b18178aaf4dcbc176961c928d76)
|
134
|
+
* [Deprecate extract! calling private methods](https://github.com/rails/jbuilder/commit/b9e19536c2105d7f2e813006bbcb8ca5730d28a3)
|
135
|
+
* [Add array of partials rendering](https://github.com/rails/jbuilder/commit/7d7311071720548047f98f14ad013c560b8d9c3a)
|
136
|
+
|
137
|
+
1.4.2
|
138
|
+
-----
|
139
|
+
* [Require MIME dependency explicitly](https://github.com/rails/jbuilder/commit/b1ed5ac4f08b056f8839b4b19b43562e81e02a59)
|
140
|
+
|
141
|
+
1.4.1
|
142
|
+
-----
|
143
|
+
* [Removed deprecated positioned arguments initializer support](https://github.com/rails/jbuilder/commit/6e03e0452073eeda77e6dfe66aa31e5ec67a3531)
|
144
|
+
* [Deprecate two-arguments block calling](https://github.com/rails/jbuilder/commit/2b10bb058bb12bc782cbcc16f6ec67b489e5ed43)
|
145
|
+
|
146
|
+
1.4.0
|
147
|
+
-----
|
148
|
+
* [Add quick collection attribute extraction](https://github.com/rails/jbuilder/commit/c2b966cf653ea4264fbb008b8cc6ce5359ebe40a)
|
149
|
+
* [Block has priority over attributes extraction](https://github.com/rails/jbuilder/commit/77c24766362c02769d81dac000b1879a9e4d4a00)
|
150
|
+
* [Meaningfull error messages when adding properties to null](https://github.com/rails/jbuilder/commit/e26764602e34b3772e57e730763d512e59489e3b)
|
151
|
+
* [Do not enforce template format, enforce handlers instead](https://github.com/rails/jbuilder/commit/72576755224b15da45e50cbea877679800ab1398)
|
152
|
+
|
153
|
+
1.3.0
|
154
|
+
-----
|
155
|
+
* [Add nil! method for nil JSON](https://github.com/rails/jbuilder/commit/822a906f68664f61a1209336bb681077692c8475)
|
156
|
+
|
157
|
+
1.2.1
|
158
|
+
-----
|
159
|
+
* [Added explicit dependency for MultiJson](https://github.com/rails/jbuilder/commit/4d58eacb6cd613679fb243484ff73a79bbbff2d2
|
160
|
+
|
161
|
+
1.2.0
|
162
|
+
-----
|
163
|
+
* Multiple documentation improvements and internal refactoring
|
164
|
+
* [Fixes fragment caching to work with latest digests](https://github.com/rails/jbuilder/commit/da937d6b8732124074c612abb7ff38868d1d96c0)
|
165
|
+
|
166
|
+
1.0.2
|
167
|
+
-----
|
168
|
+
* [Support non-Enumerable collections](https://github.com/rails/jbuilder/commit/4c20c59bf8131a1e419bb4ebf84f2b6bdcb6b0cf)
|
169
|
+
* [Ensure that the default URL is in json format](https://github.com/rails/jbuilder/commit/0b46782fb7b8c34a3c96afa801fe27a5a97118a4)
|
170
|
+
|
171
|
+
1.0.0
|
172
|
+
-----
|
173
|
+
* Adopt Semantic Versioning
|
174
|
+
* Add rails generators
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011-2014 David Heinemeier Hansson, 37signals
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,260 @@
|
|
1
|
+
# Jbuilder
|
2
|
+
|
3
|
+
[][travis]
|
4
|
+
[][gem]
|
5
|
+
[][codeclimate]
|
6
|
+
[][gemnasium]
|
7
|
+
|
8
|
+
[travis]: https://travis-ci.org/rails/jbuilder
|
9
|
+
[gem]: https://rubygems.org/gems/jbuilder
|
10
|
+
[codeclimate]: https://codeclimate.com/github/rails/jbuilder
|
11
|
+
[gemnasium]: https://gemnasium.com/rails/jbuilder
|
12
|
+
|
13
|
+
Jbuilder gives you a simple DSL for declaring JSON structures that beats
|
14
|
+
massaging giant hash structures. This is particularly helpful when the
|
15
|
+
generation process is fraught with conditionals and loops. Here's a simple
|
16
|
+
example:
|
17
|
+
|
18
|
+
``` ruby
|
19
|
+
# app/views/message/show.json.jbuilder
|
20
|
+
|
21
|
+
json.content format_content(@message.content)
|
22
|
+
json.(@message, :created_at, :updated_at)
|
23
|
+
|
24
|
+
json.author do
|
25
|
+
json.name @message.creator.name.familiar
|
26
|
+
json.email_address @message.creator.email_address_with_name
|
27
|
+
json.url url_for(@message.creator, format: :json)
|
28
|
+
end
|
29
|
+
|
30
|
+
if current_user.admin?
|
31
|
+
json.visitors calculate_visitors(@message)
|
32
|
+
end
|
33
|
+
|
34
|
+
json.comments @message.comments, :content, :created_at
|
35
|
+
|
36
|
+
json.attachments @message.attachments do |attachment|
|
37
|
+
json.filename attachment.filename
|
38
|
+
json.url url_for(attachment)
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
This will build the following structure:
|
43
|
+
|
44
|
+
``` javascript
|
45
|
+
{
|
46
|
+
"content": "<p>This is <i>serious</i> monkey business</p>",
|
47
|
+
"created_at": "2011-10-29T20:45:28-05:00",
|
48
|
+
"updated_at": "2011-10-29T20:45:28-05:00",
|
49
|
+
|
50
|
+
"author": {
|
51
|
+
"name": "David H.",
|
52
|
+
"email_address": "'David Heinemeier Hansson' <david@heinemeierhansson.com>",
|
53
|
+
"url": "http://example.com/users/1-david.json"
|
54
|
+
},
|
55
|
+
|
56
|
+
"visitors": 15,
|
57
|
+
|
58
|
+
"comments": [
|
59
|
+
{ "content": "Hello everyone!", "created_at": "2011-10-29T20:45:28-05:00" },
|
60
|
+
{ "content": "To you my good sir!", "created_at": "2011-10-29T20:47:28-05:00" }
|
61
|
+
],
|
62
|
+
|
63
|
+
"attachments": [
|
64
|
+
{ "filename": "forecast.xls", "url": "http://example.com/downloads/forecast.xls" },
|
65
|
+
{ "filename": "presentation.pdf", "url": "http://example.com/downloads/presentation.pdf" }
|
66
|
+
]
|
67
|
+
}
|
68
|
+
```
|
69
|
+
|
70
|
+
To define attribute and structure names dynamically, use the `set!` method:
|
71
|
+
|
72
|
+
``` ruby
|
73
|
+
json.set! :author do
|
74
|
+
json.set! :name, 'David'
|
75
|
+
end
|
76
|
+
|
77
|
+
# => "author": { "name": "David" }
|
78
|
+
```
|
79
|
+
|
80
|
+
Top level arrays can be handled directly. Useful for index and other collection actions.
|
81
|
+
|
82
|
+
``` ruby
|
83
|
+
# @comments = @post.comments
|
84
|
+
|
85
|
+
json.array! @comments do |comment|
|
86
|
+
next if comment.marked_as_spam_by?(current_user)
|
87
|
+
|
88
|
+
json.body comment.body
|
89
|
+
json.author do
|
90
|
+
json.first_name comment.author.first_name
|
91
|
+
json.last_name comment.author.last_name
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
|
96
|
+
```
|
97
|
+
|
98
|
+
You can also extract attributes from array directly.
|
99
|
+
|
100
|
+
``` ruby
|
101
|
+
# @people = People.all
|
102
|
+
|
103
|
+
json.array! @people, :id, :name
|
104
|
+
|
105
|
+
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
|
106
|
+
```
|
107
|
+
|
108
|
+
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
|
109
|
+
|
110
|
+
``` ruby
|
111
|
+
class Person
|
112
|
+
# ... Class Definition ... #
|
113
|
+
def to_builder
|
114
|
+
Jbuilder.new do |person|
|
115
|
+
person.(self, :name, :age)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class Company
|
121
|
+
# ... Class Definition ... #
|
122
|
+
def to_builder
|
123
|
+
Jbuilder.new do |company|
|
124
|
+
company.name name
|
125
|
+
company.president president.to_builder
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
company = Company.new('Doodle Corp', Person.new('John Stobs', 58))
|
131
|
+
company.to_builder.target!
|
132
|
+
|
133
|
+
# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
|
134
|
+
```
|
135
|
+
|
136
|
+
You can either use Jbuilder stand-alone or directly as an ActionView template
|
137
|
+
language. When required in Rails, you can create views ala show.json.jbuilder
|
138
|
+
(the json is already yielded):
|
139
|
+
|
140
|
+
``` ruby
|
141
|
+
# Any helpers available to views are available to the builder
|
142
|
+
json.content format_content(@message.content)
|
143
|
+
json.(@message, :created_at, :updated_at)
|
144
|
+
|
145
|
+
json.author do
|
146
|
+
json.name @message.creator.name.familiar
|
147
|
+
json.email_address @message.creator.email_address_with_name
|
148
|
+
json.url url_for(@message.creator, format: :json)
|
149
|
+
end
|
150
|
+
|
151
|
+
if current_user.admin?
|
152
|
+
json.visitors calculate_visitors(@message)
|
153
|
+
end
|
154
|
+
```
|
155
|
+
|
156
|
+
|
157
|
+
You can use partials as well. The following will render the file
|
158
|
+
`views/comments/_comments.json.jbuilder`, and set a local variable
|
159
|
+
`comments` with all this message's comments, which you can use inside
|
160
|
+
the partial.
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
json.partial! 'comments/comments', comments: @message.comments
|
164
|
+
```
|
165
|
+
|
166
|
+
It's also possible to render collections of partials:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
json.array! @posts, partial: 'posts/post', as: :post
|
170
|
+
|
171
|
+
# or
|
172
|
+
|
173
|
+
json.partial! 'posts/post', collection: @posts, as: :post
|
174
|
+
|
175
|
+
# or
|
176
|
+
|
177
|
+
json.partial! partial: 'posts/post', collection: @posts, as: :post
|
178
|
+
|
179
|
+
# or
|
180
|
+
|
181
|
+
json.comments @post.comments, partial: 'comment/comment', as: :comment
|
182
|
+
```
|
183
|
+
|
184
|
+
You can pass any objects into partial templates with or without `:locals` option.
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
json.partial! 'sub_template', locals: { user: user }
|
188
|
+
|
189
|
+
# or
|
190
|
+
|
191
|
+
json.partial! 'sub_template', user: user
|
192
|
+
```
|
193
|
+
|
194
|
+
|
195
|
+
You can explicitly make Jbuilder object return null if you want:
|
196
|
+
|
197
|
+
``` ruby
|
198
|
+
json.extract! @post, :id, :title, :content, :published_at
|
199
|
+
json.author do
|
200
|
+
if @post.anonymous?
|
201
|
+
json.null! # or json.nil!
|
202
|
+
else
|
203
|
+
json.first_name @post.author_first_name
|
204
|
+
json.last_name @post.author_last_name
|
205
|
+
end
|
206
|
+
end
|
207
|
+
```
|
208
|
+
|
209
|
+
Fragment caching is supported, it uses `Rails.cache` and works like caching in
|
210
|
+
HTML templates:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
json.cache! ['v1', @person], expires_in: 10.minutes do
|
214
|
+
json.extract! @person, :name, :age
|
215
|
+
end
|
216
|
+
```
|
217
|
+
|
218
|
+
You can also conditionally cache a block by using `cache_if!` like this:
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
|
222
|
+
json.extract! @person, :name, :age
|
223
|
+
end
|
224
|
+
```
|
225
|
+
|
226
|
+
If you are rendering fragments for a collection of objects, have a look at
|
227
|
+
`jbuilder_cache_multi` gem. It uses fetch_multi (>= Rails 4.1) to fetch
|
228
|
+
multiple keys at once.
|
229
|
+
|
230
|
+
Keys can be auto formatted using `key_format!`, this can be used to convert
|
231
|
+
keynames from the standard ruby_format to camelCase:
|
232
|
+
|
233
|
+
``` ruby
|
234
|
+
json.key_format! camelize: :lower
|
235
|
+
json.first_name 'David'
|
236
|
+
|
237
|
+
# => { "firstName": "David" }
|
238
|
+
```
|
239
|
+
|
240
|
+
You can set this globally with the class method `key_format` (from inside your
|
241
|
+
environment.rb for example):
|
242
|
+
|
243
|
+
``` ruby
|
244
|
+
Jbuilder.key_format camelize: :lower
|
245
|
+
```
|
246
|
+
|
247
|
+
Faster JSON backends
|
248
|
+
--------------------
|
249
|
+
|
250
|
+
Jbuilder uses MultiJson, which by default will use the JSON gem. That gem is
|
251
|
+
currently tangled with ActiveSupport's all-Ruby `#to_json` implementation,
|
252
|
+
which is slow (fixed in Rails >= 4.1). For faster Jbuilder rendering, you can
|
253
|
+
specify something like the Yajl JSON generator instead. You'll need to include
|
254
|
+
the `yajl-ruby` gem in your Gemfile and then set the following configuration
|
255
|
+
for MultiJson:
|
256
|
+
|
257
|
+
``` ruby
|
258
|
+
require 'multi_json'
|
259
|
+
MultiJson.use :yajl
|
260
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
|
6
|
+
require "appraisal/task"
|
7
|
+
Appraisal::Task.new
|
8
|
+
task default: :appraisal
|
9
|
+
else
|
10
|
+
Rake::TestTask.new do |test|
|
11
|
+
require "rails/version"
|
12
|
+
|
13
|
+
test.libs << "test"
|
14
|
+
|
15
|
+
if Rails::VERSION::MAJOR == 3
|
16
|
+
test.test_files = %w[test/jbuilder_template_test.rb test/jbuilder_test.rb]
|
17
|
+
else
|
18
|
+
test.test_files = FileList["test/*_test.rb"]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
task default: :test
|
23
|
+
end
|
data/jbuilder.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'jwtbuilder'
|
3
|
+
s.version = '2.2.12.jwt'
|
4
|
+
s.authors = ['Nguyen Ngo Dinh']
|
5
|
+
s.email = ['nguyenngodinh@outlook.com']
|
6
|
+
s.summary = 'Create JSON Web Signature structures via a Builder-style DSL'
|
7
|
+
s.homepage = 'https://github.com/nguyenngodinh'
|
8
|
+
s.license = 'MIT'
|
9
|
+
|
10
|
+
s.required_ruby_version = '>= 1.9.3'
|
11
|
+
|
12
|
+
s.add_dependency 'activesupport', '>= 3.0.0', '< 5'
|
13
|
+
s.add_dependency 'multi_json', '~> 1.2'
|
14
|
+
s.add_dependency 'jwt', '~> 1.4.1'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
18
|
+
end
|