jbuilder 2.0.6 → 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 -12
- data/MIT-LICENSE +1 -1
- data/README.md +171 -45
- 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 +20 -6
- 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 +16 -20
- 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 +1 -1
- data/lib/jbuilder/errors.rb +24 -0
- data/lib/jbuilder/jbuilder.rb +7 -0
- data/lib/jbuilder/jbuilder_template.rb +213 -65
- data/lib/jbuilder/key_formatter.rb +34 -0
- data/lib/jbuilder/railtie.rb +31 -6
- data/lib/jbuilder.rb +148 -114
- data/test/jbuilder_dependency_tracker_test.rb +3 -4
- data/test/jbuilder_generator_test.rb +31 -4
- data/test/jbuilder_template_test.rb +313 -195
- data/test/jbuilder_test.rb +615 -219
- 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 +38 -23
- data/.travis.yml +0 -21
- data/CHANGELOG.md +0 -89
- data/Gemfile.old +0 -14
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
@@ -1,15 +1,7 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
gem
|
6
|
-
gem
|
7
|
-
gem
|
8
|
-
gem 'mocha', require: false
|
9
|
-
|
10
|
-
platforms :rbx do
|
11
|
-
gem 'rubysl', '~> 2.0'
|
12
|
-
gem 'json', '~> 1.8'
|
13
|
-
gem 'rubysl-test-unit'
|
14
|
-
gem 'rubinius-developer_tools'
|
15
|
-
end
|
5
|
+
gem "rake"
|
6
|
+
gem "mocha", require: false
|
7
|
+
gem "appraisal"
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,36 +1,31 @@
|
|
1
1
|
# Jbuilder
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
[travis]: https://travis-ci.org/rails/jbuilder
|
8
|
-
[gem]: https://rubygems.org/gems/jbuilder
|
9
|
-
[codeclimate]: https://codeclimate.com/github/rails/jbuilder
|
10
|
-
|
11
|
-
Jbuilder gives you a simple DSL for declaring JSON structures that beats massaging giant hash structures. This is particularly helpful when the generation process is fraught with conditionals and loops. Here's a simple example:
|
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:
|
12
7
|
|
13
8
|
``` ruby
|
14
|
-
|
15
|
-
json.content format_content(@message.content)
|
16
|
-
json.(@message, :created_at, :updated_at)
|
9
|
+
# app/views/messages/show.json.jbuilder
|
17
10
|
|
18
|
-
|
19
|
-
|
20
|
-
json.email_address @message.creator.email_address_with_name
|
21
|
-
json.url url_for(@message.creator, format: :json)
|
22
|
-
end
|
11
|
+
json.content format_content(@message.content)
|
12
|
+
json.(@message, :created_at, :updated_at)
|
23
13
|
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
27
19
|
|
28
|
-
|
20
|
+
if current_user.admin?
|
21
|
+
json.visitors calculate_visitors(@message)
|
22
|
+
end
|
29
23
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
24
|
+
json.comments @message.comments, :content, :created_at
|
25
|
+
|
26
|
+
json.attachments @message.attachments do |attachment|
|
27
|
+
json.filename attachment.filename
|
28
|
+
json.url url_for(attachment)
|
34
29
|
end
|
35
30
|
```
|
36
31
|
|
@@ -69,30 +64,77 @@ json.set! :author do
|
|
69
64
|
json.set! :name, 'David'
|
70
65
|
end
|
71
66
|
|
72
|
-
# => "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" } }
|
73
81
|
```
|
74
82
|
|
75
83
|
Top level arrays can be handled directly. Useful for index and other collection actions.
|
76
84
|
|
77
85
|
``` ruby
|
78
|
-
# @
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
82
96
|
end
|
83
97
|
|
84
|
-
# => [ { "
|
98
|
+
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
|
85
99
|
```
|
86
100
|
|
87
101
|
You can also extract attributes from array directly.
|
88
102
|
|
89
103
|
``` ruby
|
90
104
|
# @people = People.all
|
105
|
+
|
91
106
|
json.array! @people, :id, :name
|
92
107
|
|
93
108
|
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
|
94
109
|
```
|
95
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
|
+
|
96
138
|
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
|
97
139
|
|
98
140
|
``` ruby
|
@@ -121,7 +163,9 @@ company.to_builder.target!
|
|
121
163
|
# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
|
122
164
|
```
|
123
165
|
|
124
|
-
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):
|
125
169
|
|
126
170
|
``` ruby
|
127
171
|
# Any helpers available to views are available to the builder
|
@@ -139,7 +183,6 @@ if current_user.admin?
|
|
139
183
|
end
|
140
184
|
```
|
141
185
|
|
142
|
-
|
143
186
|
You can use partials as well. The following will render the file
|
144
187
|
`views/comments/_comments.json.jbuilder`, and set a local variable
|
145
188
|
`comments` with all this message's comments, which you can use inside
|
@@ -155,18 +198,48 @@ It's also possible to render collections of partials:
|
|
155
198
|
json.array! @posts, partial: 'posts/post', as: :post
|
156
199
|
|
157
200
|
# or
|
158
|
-
|
159
201
|
json.partial! 'posts/post', collection: @posts, as: :post
|
160
202
|
|
161
203
|
# or
|
162
|
-
|
163
204
|
json.partial! partial: 'posts/post', collection: @posts, as: :post
|
164
205
|
|
165
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:
|
166
224
|
|
167
|
-
|
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
|
168
230
|
```
|
169
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
|
+
|
237
|
+
# or
|
238
|
+
|
239
|
+
json.partial! 'sub_template', user: user
|
240
|
+
```
|
241
|
+
|
242
|
+
|
170
243
|
You can explicitly make Jbuilder object return null if you want:
|
171
244
|
|
172
245
|
``` ruby
|
@@ -181,7 +254,19 @@ json.author do
|
|
181
254
|
end
|
182
255
|
```
|
183
256
|
|
184
|
-
|
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:
|
185
270
|
|
186
271
|
```ruby
|
187
272
|
json.cache! ['v1', @person], expires_in: 10.minutes do
|
@@ -197,7 +282,28 @@ json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
|
|
197
282
|
end
|
198
283
|
```
|
199
284
|
|
200
|
-
|
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:
|
201
307
|
|
202
308
|
``` ruby
|
203
309
|
json.key_format! camelize: :lower
|
@@ -206,18 +312,38 @@ json.first_name 'David'
|
|
206
312
|
# => { "firstName": "David" }
|
207
313
|
```
|
208
314
|
|
209
|
-
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):
|
210
317
|
|
211
318
|
``` ruby
|
212
319
|
Jbuilder.key_format camelize: :lower
|
213
320
|
```
|
214
321
|
|
215
|
-
|
216
|
-
|
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
|
+
```
|
217
333
|
|
218
|
-
|
334
|
+
You can set this globally with the class method `deep_format_keys` (from inside your
|
335
|
+
environment.rb for example):
|
219
336
|
|
220
337
|
``` ruby
|
221
|
-
|
222
|
-
|
223
|
-
|
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).
|
347
|
+
|
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
|