jbuilder 1.5.3 → 2.11.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +108 -0
- data/.gitignore +4 -1
- data/Appraisals +25 -0
- data/CONTRIBUTING.md +106 -0
- data/Gemfile +4 -5
- data/MIT-LICENSE +2 -2
- data/README.md +182 -40
- data/Rakefile +15 -10
- data/gemfiles/rails_5_0.gemfile +10 -0
- data/gemfiles/rails_5_1.gemfile +10 -0
- data/gemfiles/rails_5_2.gemfile +10 -0
- data/gemfiles/rails_6_0.gemfile +10 -0
- data/gemfiles/rails_6_1.gemfile +10 -0
- data/gemfiles/rails_head.gemfile +10 -0
- data/jbuilder.gemspec +25 -8
- data/lib/generators/rails/jbuilder_generator.rb +13 -2
- data/lib/generators/rails/scaffold_controller_generator.rb +9 -3
- data/lib/generators/rails/templates/api_controller.rb +63 -0
- data/lib/generators/rails/templates/controller.rb +18 -22
- data/lib/generators/rails/templates/index.json.jbuilder +1 -4
- data/lib/generators/rails/templates/partial.json.jbuilder +16 -0
- data/lib/generators/rails/templates/show.json.jbuilder +1 -1
- data/lib/jbuilder/blank.rb +11 -0
- data/lib/jbuilder/collection_renderer.rb +109 -0
- data/lib/jbuilder/dependency_tracker.rb +61 -0
- data/lib/jbuilder/errors.rb +24 -0
- data/lib/jbuilder/jbuilder.rb +7 -0
- data/lib/jbuilder/jbuilder_template.rb +228 -67
- data/lib/jbuilder/key_formatter.rb +34 -0
- data/lib/jbuilder/railtie.rb +31 -6
- data/lib/jbuilder.rb +144 -137
- data/test/jbuilder_dependency_tracker_test.rb +72 -0
- data/test/jbuilder_generator_test.rb +31 -4
- data/test/jbuilder_template_test.rb +319 -163
- data/test/jbuilder_test.rb +613 -298
- data/test/scaffold_api_controller_generator_test.rb +70 -0
- data/test/scaffold_controller_generator_test.rb +62 -19
- data/test/test_helper.rb +36 -0
- metadata +47 -22
- data/.travis.yml +0 -38
- data/Gemfile.old +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9ce8fbeac8b34ee290535e98a284a1247e2de1b5272c4679c1b89a89ff8149db
|
4
|
+
data.tar.gz: cdaeb1302c4755ca43f037328698afe29fe658f2094c99e2c6f81f0000b9368c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6903b1aa13786fd5501d8e667eb7a913ebd469d24ce183a24cdf949a62788b1e598c2e0678c5ed5e369d4232c7720e284850f735c5be6f4691e4ecf42256d21a
|
7
|
+
data.tar.gz: 2a40e415a25a4d8680ac00ba81b9984188ef35440869a357db4fd7184d3f0b36d0ce10545a0d5253e6d12374b2f1d51f5a96f1ded1370ec7eddecaee469afc61
|
@@ -0,0 +1,108 @@
|
|
1
|
+
name: Ruby test
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
name: Ruby ${{ matrix.ruby }} (${{ matrix.gemfile }})
|
8
|
+
runs-on: ubuntu-20.04
|
9
|
+
continue-on-error: ${{ matrix.experimental }}
|
10
|
+
env:
|
11
|
+
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
|
12
|
+
BUNDLE_JOBS: 4
|
13
|
+
BUNDLE_RETRY: 3
|
14
|
+
strategy:
|
15
|
+
fail-fast: false
|
16
|
+
matrix:
|
17
|
+
ruby:
|
18
|
+
- "2.2"
|
19
|
+
- "2.3"
|
20
|
+
- "2.4"
|
21
|
+
- "2.5"
|
22
|
+
- "2.6"
|
23
|
+
- "2.7"
|
24
|
+
- "3.0"
|
25
|
+
|
26
|
+
gemfile:
|
27
|
+
- "rails_5_0"
|
28
|
+
- "rails_5_1"
|
29
|
+
- "rails_5_2"
|
30
|
+
- "rails_6_0"
|
31
|
+
- "rails_6_1"
|
32
|
+
- "rails_head"
|
33
|
+
|
34
|
+
experimental: [false]
|
35
|
+
exclude:
|
36
|
+
- ruby: 2.7
|
37
|
+
gemfile: rails_5_0
|
38
|
+
- ruby: '3.0'
|
39
|
+
gemfile: rails_5_0
|
40
|
+
- ruby: head
|
41
|
+
gemfile: rails_5_0
|
42
|
+
- ruby: 2.7
|
43
|
+
gemfile: rails_5_1
|
44
|
+
- ruby: '3.0'
|
45
|
+
gemfile: rails_5_1
|
46
|
+
- ruby: head
|
47
|
+
gemfile: rails_5_1
|
48
|
+
- ruby: 2.2
|
49
|
+
gemfile: rails_5_2
|
50
|
+
- ruby: 2.7
|
51
|
+
gemfile: rails_5_2
|
52
|
+
- ruby: '3.0'
|
53
|
+
gemfile: rails_5_2
|
54
|
+
- ruby: head
|
55
|
+
gemfile: rails_5_2
|
56
|
+
- ruby: 2.2
|
57
|
+
gemfile: rails_6_0
|
58
|
+
- ruby: 2.3
|
59
|
+
gemfile: rails_6_0
|
60
|
+
- ruby: 2.4
|
61
|
+
gemfile: rails_6_0
|
62
|
+
- ruby: '3.0'
|
63
|
+
gemfile: rails_6_0
|
64
|
+
- ruby: head
|
65
|
+
gemfile: rails_6_0
|
66
|
+
- ruby: 2.2
|
67
|
+
gemfile: rails_6_1
|
68
|
+
- ruby: 2.3
|
69
|
+
gemfile: rails_6_1
|
70
|
+
- ruby: 2.4
|
71
|
+
gemfile: rails_6_1
|
72
|
+
- ruby: 2.2
|
73
|
+
gemfile: rails_head
|
74
|
+
- ruby: 2.3
|
75
|
+
gemfile: rails_head
|
76
|
+
- ruby: 2.4
|
77
|
+
gemfile: rails_head
|
78
|
+
- ruby: 2.5
|
79
|
+
gemfile: rails_head
|
80
|
+
- ruby: 2.6
|
81
|
+
gemfile: rails_head
|
82
|
+
- ruby: 2.7
|
83
|
+
gemfile: rails_head
|
84
|
+
experimental: false
|
85
|
+
- ruby: '3.0'
|
86
|
+
gemfile: rails_head
|
87
|
+
experimental: false
|
88
|
+
include:
|
89
|
+
- ruby: 2.7
|
90
|
+
gemfile: rails_head
|
91
|
+
experimental: true
|
92
|
+
- ruby: '3.0'
|
93
|
+
gemfile: rails_head
|
94
|
+
experimental: true
|
95
|
+
- ruby: head
|
96
|
+
gemfile: rails_head
|
97
|
+
experimental: true
|
98
|
+
|
99
|
+
steps:
|
100
|
+
- uses: actions/checkout@v2
|
101
|
+
|
102
|
+
- uses: ruby/setup-ruby@v1
|
103
|
+
with:
|
104
|
+
ruby-version: ${{ matrix.ruby }}
|
105
|
+
bundler-cache: true
|
106
|
+
|
107
|
+
- name: Ruby test
|
108
|
+
run: bundle exec rake
|
data/.gitignore
CHANGED
data/Appraisals
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
appraise "rails-5-0" do
|
2
|
+
gem "rails", "~> 5.0.0"
|
3
|
+
end
|
4
|
+
|
5
|
+
appraise "rails-5-1" do
|
6
|
+
gem "rails", "~> 5.1.0"
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise "rails-5-2" do
|
10
|
+
gem "rails", "~> 5.2.0"
|
11
|
+
end
|
12
|
+
|
13
|
+
if RUBY_VERSION >= "2.5.0"
|
14
|
+
appraise "rails-6-0" do
|
15
|
+
gem "rails", "~> 6.0.0"
|
16
|
+
end
|
17
|
+
|
18
|
+
appraise "rails-6-1" do
|
19
|
+
gem "rails", "~> 6.1.0"
|
20
|
+
end
|
21
|
+
|
22
|
+
appraise "rails-head" do
|
23
|
+
gem "rails", github: "rails/rails", branch: "main"
|
24
|
+
end
|
25
|
+
end
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
Contributing to Jbuilder
|
2
|
+
=====================
|
3
|
+
|
4
|
+
[![Build Status](https://github.com/rails/jbuilder/workflows/Ruby%20test/badge.svg)][test]
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/jbuilder.svg)][gem]
|
6
|
+
[![Code Climate](https://codeclimate.com/github/rails/jbuilder/badges/gpa.svg)][codeclimate]
|
7
|
+
|
8
|
+
[test]: https://github.com/rails/jbuilder/actions?query=branch%3Amaster
|
9
|
+
[gem]: https://rubygems.org/gems/jbuilder
|
10
|
+
[codeclimate]: https://codeclimate.com/github/rails/jbuilder
|
11
|
+
|
12
|
+
Jbuilder is work of [many contributors](https://github.com/rails/jbuilder/graphs/contributors). You're encouraged to submit [pull requests](https://github.com/rails/jbuilder/pulls), [propose features and discuss issues](https://github.com/rails/jbuilder/issues).
|
13
|
+
|
14
|
+
#### Fork the Project
|
15
|
+
|
16
|
+
Fork the [project on GitHub](https://github.com/rails/jbuilder) and check out your copy.
|
17
|
+
|
18
|
+
```
|
19
|
+
git clone https://github.com/contributor/jbuilder.git
|
20
|
+
cd jbuilder
|
21
|
+
git remote add upstream https://github.com/rails/jbuilder.git
|
22
|
+
```
|
23
|
+
|
24
|
+
#### Create a Topic Branch
|
25
|
+
|
26
|
+
Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
|
27
|
+
|
28
|
+
```
|
29
|
+
git checkout master
|
30
|
+
git pull upstream master
|
31
|
+
git checkout -b my-feature-branch
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Bundle Install and Test
|
35
|
+
|
36
|
+
Ensure that you can build the project and run tests.
|
37
|
+
|
38
|
+
```
|
39
|
+
bundle install
|
40
|
+
appraisal install
|
41
|
+
appraisal rake test
|
42
|
+
```
|
43
|
+
|
44
|
+
#### Write Tests
|
45
|
+
|
46
|
+
Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add to [test](test).
|
47
|
+
|
48
|
+
We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
|
49
|
+
|
50
|
+
#### Write Code
|
51
|
+
|
52
|
+
Implement your feature or bug fix.
|
53
|
+
|
54
|
+
Make sure that `appraisal rake test` completes without errors.
|
55
|
+
|
56
|
+
#### Write Documentation
|
57
|
+
|
58
|
+
Document any external behavior in the [README](README.md).
|
59
|
+
|
60
|
+
#### Commit Changes
|
61
|
+
|
62
|
+
Make sure git knows your name and email address:
|
63
|
+
|
64
|
+
```
|
65
|
+
git config --global user.name "Your Name"
|
66
|
+
git config --global user.email "contributor@example.com"
|
67
|
+
```
|
68
|
+
|
69
|
+
Writing good commit logs is important. A commit log should describe what changed and why.
|
70
|
+
|
71
|
+
```
|
72
|
+
git add ...
|
73
|
+
git commit
|
74
|
+
```
|
75
|
+
|
76
|
+
#### Push
|
77
|
+
|
78
|
+
```
|
79
|
+
git push origin my-feature-branch
|
80
|
+
```
|
81
|
+
|
82
|
+
#### Make a Pull Request
|
83
|
+
|
84
|
+
Visit your forked repo and click the 'New pull request' button. Select your feature branch, fill out the form, and click the 'Create pull request' button. Pull requests are usually reviewed within a few days.
|
85
|
+
|
86
|
+
#### Rebase
|
87
|
+
|
88
|
+
If you've been working on a change for a while, rebase with upstream/master.
|
89
|
+
|
90
|
+
```
|
91
|
+
git fetch upstream
|
92
|
+
git rebase upstream/master
|
93
|
+
git push origin my-feature-branch -f
|
94
|
+
```
|
95
|
+
|
96
|
+
#### Check on Your Pull Request
|
97
|
+
|
98
|
+
Go back to your pull request after a few minutes and see whether it passed muster with GitHub Actions. Everything should look green, otherwise fix issues and amend your commit as described above.
|
99
|
+
|
100
|
+
#### Be Patient
|
101
|
+
|
102
|
+
It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang in there!
|
103
|
+
|
104
|
+
#### Thank You
|
105
|
+
|
106
|
+
Please do know that we really appreciate and value your time and work. We love you, really.
|
data/Gemfile
CHANGED
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2011-
|
1
|
+
Copyright (c) 2011-2018 David Heinemeier Hansson, 37signals
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
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.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
Jbuilder
|
2
|
-
========
|
1
|
+
# Jbuilder
|
3
2
|
|
4
|
-
Jbuilder gives you a simple DSL for declaring JSON structures that beats
|
3
|
+
Jbuilder gives you a simple DSL for declaring JSON structures that beats
|
4
|
+
manipulating giant hash structures. This is particularly helpful when the
|
5
|
+
generation process is fraught with conditionals and loops. Here's a simple
|
6
|
+
example:
|
5
7
|
|
6
8
|
``` ruby
|
7
|
-
|
8
|
-
json.content format_content(@message.content)
|
9
|
-
json.(@message, :created_at, :updated_at)
|
9
|
+
# app/views/messages/show.json.jbuilder
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
json.email_address @message.creator.email_address_with_name
|
14
|
-
json.url url_for(@message.creator, format: :json)
|
15
|
-
end
|
11
|
+
json.content format_content(@message.content)
|
12
|
+
json.(@message, :created_at, :updated_at)
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
json.author do
|
15
|
+
json.name @message.creator.name.familiar
|
16
|
+
json.email_address @message.creator.email_address_with_name
|
17
|
+
json.url url_for(@message.creator, format: :json)
|
18
|
+
end
|
19
|
+
|
20
|
+
if current_user.admin?
|
21
|
+
json.visitors calculate_visitors(@message)
|
22
|
+
end
|
20
23
|
|
21
|
-
|
24
|
+
json.comments @message.comments, :content, :created_at
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
26
|
+
json.attachments @message.attachments do |attachment|
|
27
|
+
json.filename attachment.filename
|
28
|
+
json.url url_for(attachment)
|
27
29
|
end
|
28
30
|
```
|
29
31
|
|
@@ -62,30 +64,77 @@ json.set! :author do
|
|
62
64
|
json.set! :name, 'David'
|
63
65
|
end
|
64
66
|
|
65
|
-
# => "author": { "name": "David" }
|
67
|
+
# => {"author": { "name": "David" }}
|
68
|
+
```
|
69
|
+
|
70
|
+
|
71
|
+
To merge existing hash or array to current context:
|
72
|
+
|
73
|
+
``` ruby
|
74
|
+
hash = { author: { name: "David" } }
|
75
|
+
json.post do
|
76
|
+
json.title "Merge HOWTO"
|
77
|
+
json.merge! hash
|
78
|
+
end
|
79
|
+
|
80
|
+
# => "post": { "title": "Merge HOWTO", "author": { "name": "David" } }
|
66
81
|
```
|
67
82
|
|
68
83
|
Top level arrays can be handled directly. Useful for index and other collection actions.
|
69
84
|
|
70
85
|
``` ruby
|
71
|
-
# @
|
72
|
-
|
73
|
-
|
74
|
-
|
86
|
+
# @comments = @post.comments
|
87
|
+
|
88
|
+
json.array! @comments do |comment|
|
89
|
+
next if comment.marked_as_spam_by?(current_user)
|
90
|
+
|
91
|
+
json.body comment.body
|
92
|
+
json.author do
|
93
|
+
json.first_name comment.author.first_name
|
94
|
+
json.last_name comment.author.last_name
|
95
|
+
end
|
75
96
|
end
|
76
97
|
|
77
|
-
# => [ { "
|
98
|
+
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
|
78
99
|
```
|
79
100
|
|
80
101
|
You can also extract attributes from array directly.
|
81
102
|
|
82
103
|
``` ruby
|
83
104
|
# @people = People.all
|
105
|
+
|
84
106
|
json.array! @people, :id, :name
|
85
107
|
|
86
108
|
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
|
87
109
|
```
|
88
110
|
|
111
|
+
To make a plain array without keys, construct and pass in a standard Ruby array.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
my_array = %w(David Jamie)
|
115
|
+
|
116
|
+
json.people my_array
|
117
|
+
|
118
|
+
# => "people": [ "David", "Jamie" ]
|
119
|
+
```
|
120
|
+
|
121
|
+
You don't always have or need a collection when building an array.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
json.people do
|
125
|
+
json.child! do
|
126
|
+
json.id 1
|
127
|
+
json.name 'David'
|
128
|
+
end
|
129
|
+
json.child! do
|
130
|
+
json.id 2
|
131
|
+
json.name 'Jamie'
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# => { "people": [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] }
|
136
|
+
```
|
137
|
+
|
89
138
|
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
|
90
139
|
|
91
140
|
``` ruby
|
@@ -114,7 +163,9 @@ company.to_builder.target!
|
|
114
163
|
# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
|
115
164
|
```
|
116
165
|
|
117
|
-
You can either use Jbuilder stand-alone or directly as an ActionView template
|
166
|
+
You can either use Jbuilder stand-alone or directly as an ActionView template
|
167
|
+
language. When required in Rails, you can create views à la show.json.jbuilder
|
168
|
+
(the json is already yielded):
|
118
169
|
|
119
170
|
``` ruby
|
120
171
|
# Any helpers available to views are available to the builder
|
@@ -132,7 +183,6 @@ if current_user.admin?
|
|
132
183
|
end
|
133
184
|
```
|
134
185
|
|
135
|
-
|
136
186
|
You can use partials as well. The following will render the file
|
137
187
|
`views/comments/_comments.json.jbuilder`, and set a local variable
|
138
188
|
`comments` with all this message's comments, which you can use inside
|
@@ -148,18 +198,48 @@ It's also possible to render collections of partials:
|
|
148
198
|
json.array! @posts, partial: 'posts/post', as: :post
|
149
199
|
|
150
200
|
# or
|
151
|
-
|
152
201
|
json.partial! 'posts/post', collection: @posts, as: :post
|
153
202
|
|
154
203
|
# or
|
155
|
-
|
156
204
|
json.partial! partial: 'posts/post', collection: @posts, as: :post
|
157
205
|
|
206
|
+
# or
|
207
|
+
json.comments @post.comments, partial: 'comments/comment', as: :comment
|
208
|
+
```
|
209
|
+
|
210
|
+
The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the
|
211
|
+
partial. If the value is a collection either implicitly or explicitly by using the `collection:` option, then each
|
212
|
+
value of the collection is passed to the partial as the variable `some_symbol`. If the value is a singular object,
|
213
|
+
then the object is passed to the partial as the variable `some_symbol`.
|
214
|
+
|
215
|
+
Be sure not to confuse the `as:` option to mean nesting of the partial. For example:
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
# Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
|
219
|
+
# Note, `comment` attributes are "inlined".
|
220
|
+
json.partial! @comment, as: :comment
|
221
|
+
```
|
222
|
+
|
223
|
+
is quite different from:
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
# comment attributes are nested under a "comment" property
|
227
|
+
json.comment do
|
228
|
+
json.partial! "/comments/comment.json.jbuilder", comment: @comment
|
229
|
+
end
|
230
|
+
```
|
231
|
+
|
232
|
+
You can pass any objects into partial templates with or without `:locals` option.
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
json.partial! 'sub_template', locals: { user: user }
|
236
|
+
|
158
237
|
# or
|
159
238
|
|
160
|
-
json.
|
239
|
+
json.partial! 'sub_template', user: user
|
161
240
|
```
|
162
241
|
|
242
|
+
|
163
243
|
You can explicitly make Jbuilder object return null if you want:
|
164
244
|
|
165
245
|
``` ruby
|
@@ -174,7 +254,19 @@ json.author do
|
|
174
254
|
end
|
175
255
|
```
|
176
256
|
|
177
|
-
|
257
|
+
To prevent Jbuilder from including null values in the output, you can use the `ignore_nil!` method:
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
json.ignore_nil!
|
261
|
+
json.foo nil
|
262
|
+
json.bar "bar"
|
263
|
+
# => { "bar": "bar" }
|
264
|
+
```
|
265
|
+
|
266
|
+
## Caching
|
267
|
+
|
268
|
+
Fragment caching is supported, it uses `Rails.cache` and works like caching in
|
269
|
+
HTML templates:
|
178
270
|
|
179
271
|
```ruby
|
180
272
|
json.cache! ['v1', @person], expires_in: 10.minutes do
|
@@ -182,7 +274,36 @@ json.cache! ['v1', @person], expires_in: 10.minutes do
|
|
182
274
|
end
|
183
275
|
```
|
184
276
|
|
185
|
-
|
277
|
+
You can also conditionally cache a block by using `cache_if!` like this:
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
|
281
|
+
json.extract! @person, :name, :age
|
282
|
+
end
|
283
|
+
```
|
284
|
+
|
285
|
+
Aside from that, the `:cached` options on collection rendering is available on Rails >= 6.0. This will cache the
|
286
|
+
rendered results effectively using the multi fetch feature.
|
287
|
+
|
288
|
+
```ruby
|
289
|
+
json.array! @posts, partial: "posts/post", as: :post, cached: true
|
290
|
+
|
291
|
+
# or:
|
292
|
+
json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true
|
293
|
+
```
|
294
|
+
|
295
|
+
If your collection cache depends on multiple sources (try to avoid this to keep things simple), you can name all these dependencies as part of a block that returns an array:
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
json.array! @posts, partial: "posts/post", as: :post, cached: -> post { [post, current_user] }
|
299
|
+
```
|
300
|
+
|
301
|
+
This will include both records as part of the cache key and updating either of them will expire the cache.
|
302
|
+
|
303
|
+
## Formatting Keys
|
304
|
+
|
305
|
+
Keys can be auto formatted using `key_format!`, this can be used to convert
|
306
|
+
keynames from the standard ruby_format to camelCase:
|
186
307
|
|
187
308
|
``` ruby
|
188
309
|
json.key_format! camelize: :lower
|
@@ -191,17 +312,38 @@ json.first_name 'David'
|
|
191
312
|
# => { "firstName": "David" }
|
192
313
|
```
|
193
314
|
|
194
|
-
You can set this globally with the class method `key_format` (from inside your
|
315
|
+
You can set this globally with the class method `key_format` (from inside your
|
316
|
+
environment.rb for example):
|
195
317
|
|
196
318
|
``` ruby
|
197
319
|
Jbuilder.key_format camelize: :lower
|
198
320
|
```
|
199
321
|
|
200
|
-
|
322
|
+
By default, key format is not applied to keys of hashes that are
|
323
|
+
passed to methods like `set!`, `array!` or `merge!`. You can opt into
|
324
|
+
deeply transforming these as well:
|
325
|
+
|
326
|
+
``` ruby
|
327
|
+
json.key_format! camelize: :lower
|
328
|
+
json.deep_format_keys!
|
329
|
+
json.settings([{some_value: "abc"}])
|
330
|
+
|
331
|
+
# => { "settings": [{ "someValue": "abc" }]}
|
332
|
+
```
|
333
|
+
|
334
|
+
You can set this globally with the class method `deep_format_keys` (from inside your
|
335
|
+
environment.rb for example):
|
336
|
+
|
337
|
+
``` ruby
|
338
|
+
Jbuilder.deep_format_keys true
|
339
|
+
```
|
340
|
+
|
341
|
+
## Contributing to Jbuilder
|
342
|
+
|
343
|
+
Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose
|
344
|
+
features and discuss issues.
|
345
|
+
|
346
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
201
347
|
|
202
|
-
|
203
|
-
|
204
|
-
* JsonBuilder: https://github.com/nov/jsonbuilder
|
205
|
-
* JSON Builder: https://github.com/dewski/json_builder
|
206
|
-
* Jsonify: https://github.com/bsiggelkow/jsonify
|
207
|
-
* RepresentationView: https://github.com/mdub/representative_view
|
348
|
+
## License
|
349
|
+
Jbuilder is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -1,14 +1,19 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "bundler/setup"
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rake/testtask"
|
3
4
|
|
4
|
-
|
5
|
+
if !ENV["APPRAISAL_INITIALIZED"] && !ENV["CI"]
|
6
|
+
require "appraisal/task"
|
7
|
+
Appraisal::Task.new
|
8
|
+
task default: :appraisal
|
9
|
+
else
|
10
|
+
Rake::TestTask.new do |test|
|
11
|
+
require "rails/version"
|
5
12
|
|
6
|
-
|
7
|
-
|
8
|
-
test.test_files =
|
9
|
-
else
|
10
|
-
test.test_files = FileList['test/*_test.rb']
|
13
|
+
test.libs << "test"
|
14
|
+
|
15
|
+
test.test_files = FileList["test/*_test.rb"]
|
11
16
|
end
|
12
|
-
end
|
13
17
|
|
14
|
-
task :
|
18
|
+
task default: :test
|
19
|
+
end
|