micro-lite-hub 0.0.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 +7 -0
- data/jbuilder-2.15.1/Appraisals +26 -0
- data/jbuilder-2.15.1/CONTRIBUTING.md +100 -0
- data/jbuilder-2.15.1/Gemfile +9 -0
- data/jbuilder-2.15.1/MIT-LICENSE +20 -0
- data/jbuilder-2.15.1/README.md +394 -0
- data/jbuilder-2.15.1/Rakefile +21 -0
- data/jbuilder-2.15.1/bin/release +14 -0
- data/jbuilder-2.15.1/bin/test +6 -0
- data/jbuilder-2.15.1/gemfiles/rails_7_0.gemfile +11 -0
- data/jbuilder-2.15.1/gemfiles/rails_7_1.gemfile +10 -0
- data/jbuilder-2.15.1/gemfiles/rails_7_2.gemfile +10 -0
- data/jbuilder-2.15.1/gemfiles/rails_8_0.gemfile +10 -0
- data/jbuilder-2.15.1/gemfiles/rails_8_1.gemfile +10 -0
- data/jbuilder-2.15.1/gemfiles/rails_head.gemfile +10 -0
- data/jbuilder-2.15.1/jbuilder.gemspec +35 -0
- data/jbuilder-2.15.1/lib/generators/rails/jbuilder_generator.rb +65 -0
- data/jbuilder-2.15.1/lib/generators/rails/scaffold_controller_generator.rb +24 -0
- data/jbuilder-2.15.1/lib/generators/rails/templates/api_controller.rb +69 -0
- data/jbuilder-2.15.1/lib/generators/rails/templates/controller.rb +86 -0
- data/jbuilder-2.15.1/lib/generators/rails/templates/index.json.jbuilder +1 -0
- data/jbuilder-2.15.1/lib/generators/rails/templates/partial.json.jbuilder +16 -0
- data/jbuilder-2.15.1/lib/generators/rails/templates/show.json.jbuilder +1 -0
- data/jbuilder-2.15.1/lib/jbuilder/blank.rb +13 -0
- data/jbuilder-2.15.1/lib/jbuilder/collection_renderer.rb +58 -0
- data/jbuilder-2.15.1/lib/jbuilder/errors.rb +26 -0
- data/jbuilder-2.15.1/lib/jbuilder/jbuilder.rb +3 -0
- data/jbuilder-2.15.1/lib/jbuilder/jbuilder_dependency_tracker.rb +75 -0
- data/jbuilder-2.15.1/lib/jbuilder/jbuilder_template.rb +264 -0
- data/jbuilder-2.15.1/lib/jbuilder/key_formatter.rb +32 -0
- data/jbuilder-2.15.1/lib/jbuilder/railtie.rb +34 -0
- data/jbuilder-2.15.1/lib/jbuilder/version.rb +5 -0
- data/jbuilder-2.15.1/lib/jbuilder.rb +384 -0
- data/jbuilder-2.15.1/test/jbuilder_dependency_tracker_test.rb +71 -0
- data/jbuilder-2.15.1/test/jbuilder_generator_test.rb +68 -0
- data/jbuilder-2.15.1/test/jbuilder_template_test.rb +469 -0
- data/jbuilder-2.15.1/test/jbuilder_test.rb +954 -0
- data/jbuilder-2.15.1/test/scaffold_api_controller_generator_test.rb +83 -0
- data/jbuilder-2.15.1/test/scaffold_controller_generator_test.rb +116 -0
- data/jbuilder-2.15.1/test/test_helper.rb +47 -0
- data/micro-lite-hub.gemspec +11 -0
- metadata +80 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fb165070577b2db82ba5ee7fe90fd37eccfec0e24f52faf06e7c863a0f0b7e37
|
|
4
|
+
data.tar.gz: 530866cebab1176dcd8479a292fa73a72f09529433ad5701aea3bda9b6589d9a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 48ddb18ef05c5772eeedce602081b9512b96e6a52c6447b75a4984808c9dd2160f906cfa2860edf50a0a6455f98d15a8740795daf31be0004ca8aa87f6555da9
|
|
7
|
+
data.tar.gz: b12c4815dcd851fe72c3efa460b8369e2e194e92ce669560e6863bde8c30a9c167b48f86e835a8995efe99fdcad0045bad69583accaf2ec15919b9fb1d010ed7
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
appraise "rails-7-0" do
|
|
4
|
+
gem "rails", "~> 7.0.0"
|
|
5
|
+
gem "concurrent-ruby", "< 1.3.5" # to avoid problem described in https://github.com/rails/rails/pull/54264
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
appraise "rails-7-1" do
|
|
9
|
+
gem "rails", "~> 7.1.0"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
appraise "rails-7-2" do
|
|
13
|
+
gem "rails", "~> 7.2.0"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
appraise "rails-8-0" do
|
|
17
|
+
gem "rails", "~> 8.0.0"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
appraise "rails-8-1" do
|
|
21
|
+
gem "rails", "~> 8.1.0"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
appraise "rails-head" do
|
|
25
|
+
gem "rails", github: "rails/rails", branch: "main"
|
|
26
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Contributing to Jbuilder
|
|
2
|
+
=====================
|
|
3
|
+
|
|
4
|
+
[][test]
|
|
5
|
+
[][gem]
|
|
6
|
+
[][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 using `bin/test`.
|
|
37
|
+
|
|
38
|
+
#### Write Tests
|
|
39
|
+
|
|
40
|
+
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).
|
|
41
|
+
|
|
42
|
+
We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
|
|
43
|
+
|
|
44
|
+
#### Write Code
|
|
45
|
+
|
|
46
|
+
Implement your feature or bug fix.
|
|
47
|
+
|
|
48
|
+
Make sure that `appraisal rake test` completes without errors.
|
|
49
|
+
|
|
50
|
+
#### Write Documentation
|
|
51
|
+
|
|
52
|
+
Document any external behavior in the [README](README.md).
|
|
53
|
+
|
|
54
|
+
#### Commit Changes
|
|
55
|
+
|
|
56
|
+
Make sure git knows your name and email address:
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
git config --global user.name "Your Name"
|
|
60
|
+
git config --global user.email "contributor@example.com"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Writing good commit logs is important. A commit log should describe what changed and why.
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
git add ...
|
|
67
|
+
git commit
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### Push
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
git push origin my-feature-branch
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### Make a Pull Request
|
|
77
|
+
|
|
78
|
+
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.
|
|
79
|
+
|
|
80
|
+
#### Rebase
|
|
81
|
+
|
|
82
|
+
If you've been working on a change for a while, rebase with upstream/master.
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
git fetch upstream
|
|
86
|
+
git rebase upstream/master
|
|
87
|
+
git push origin my-feature-branch -f
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Check on Your Pull Request
|
|
91
|
+
|
|
92
|
+
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.
|
|
93
|
+
|
|
94
|
+
#### Be Patient
|
|
95
|
+
|
|
96
|
+
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!
|
|
97
|
+
|
|
98
|
+
#### Thank You
|
|
99
|
+
|
|
100
|
+
Please do know that we really appreciate and value your time and work. We love you, really.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2011-2018 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.
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
# Jbuilder
|
|
2
|
+
|
|
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:
|
|
7
|
+
|
|
8
|
+
```ruby
|
|
9
|
+
# app/views/messages/show.json.jbuilder
|
|
10
|
+
|
|
11
|
+
json.content format_content(@message.content)
|
|
12
|
+
json.(@message, :created_at, :updated_at)
|
|
13
|
+
|
|
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
|
|
23
|
+
|
|
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)
|
|
29
|
+
end
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
This will build the following structure:
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
{
|
|
36
|
+
"content": "<p>This is <i>serious</i> monkey business</p>",
|
|
37
|
+
"created_at": "2011-10-29T20:45:28-05:00",
|
|
38
|
+
"updated_at": "2011-10-29T20:45:28-05:00",
|
|
39
|
+
|
|
40
|
+
"author": {
|
|
41
|
+
"name": "David H.",
|
|
42
|
+
"email_address": "'David Heinemeier Hansson' <david@heinemeierhansson.com>",
|
|
43
|
+
"url": "http://example.com/users/1-david.json"
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
"visitors": 15,
|
|
47
|
+
|
|
48
|
+
"comments": [
|
|
49
|
+
{ "content": "Hello everyone!", "created_at": "2011-10-29T20:45:28-05:00" },
|
|
50
|
+
{ "content": "To you my good sir!", "created_at": "2011-10-29T20:47:28-05:00" }
|
|
51
|
+
],
|
|
52
|
+
|
|
53
|
+
"attachments": [
|
|
54
|
+
{ "filename": "forecast.xls", "url": "http://example.com/downloads/forecast.xls" },
|
|
55
|
+
{ "filename": "presentation.pdf", "url": "http://example.com/downloads/presentation.pdf" }
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Dynamically Defined Attributes
|
|
61
|
+
|
|
62
|
+
To define attribute and structure names dynamically, use the `set!` method:
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
json.set! :author do
|
|
66
|
+
json.set! :name, 'David'
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# => {"author": { "name": "David" }}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Merging Existing Hash or Array
|
|
73
|
+
|
|
74
|
+
To merge existing hash or array to current context:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
hash = { author: { name: "David" } }
|
|
78
|
+
json.post do
|
|
79
|
+
json.title "Merge HOWTO"
|
|
80
|
+
json.merge! hash
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# => "post": { "title": "Merge HOWTO", "author": { "name": "David" } }
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Top Level Arrays
|
|
87
|
+
|
|
88
|
+
Top level arrays can be handled directly. Useful for index and other collection actions.
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
# @comments = @post.comments
|
|
92
|
+
|
|
93
|
+
json.array! @comments do |comment|
|
|
94
|
+
next if comment.marked_as_spam_by?(current_user)
|
|
95
|
+
|
|
96
|
+
json.body comment.body
|
|
97
|
+
json.author do
|
|
98
|
+
json.first_name comment.author.first_name
|
|
99
|
+
json.last_name comment.author.last_name
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Blow" }} ]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Array Attributes
|
|
107
|
+
|
|
108
|
+
You can also extract attributes from array directly.
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
# @people = People.all
|
|
112
|
+
|
|
113
|
+
json.array! @people, :id, :name
|
|
114
|
+
|
|
115
|
+
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Plain Arrays
|
|
119
|
+
|
|
120
|
+
To make a plain array without keys, construct and pass in a standard Ruby array.
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
my_array = %w(David Jamie)
|
|
124
|
+
|
|
125
|
+
json.people my_array
|
|
126
|
+
|
|
127
|
+
# => "people": [ "David", "Jamie" ]
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Child Objects
|
|
131
|
+
|
|
132
|
+
You don't always have or need a collection when building an array.
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
json.people do
|
|
136
|
+
json.child! do
|
|
137
|
+
json.id 1
|
|
138
|
+
json.name 'David'
|
|
139
|
+
end
|
|
140
|
+
json.child! do
|
|
141
|
+
json.id 2
|
|
142
|
+
json.name 'Jamie'
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# => { "people": [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Nested Jbuilder Objects
|
|
150
|
+
|
|
151
|
+
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
|
|
152
|
+
|
|
153
|
+
```ruby
|
|
154
|
+
class Person
|
|
155
|
+
# ... Class Definition ... #
|
|
156
|
+
def to_builder
|
|
157
|
+
Jbuilder.new do |person|
|
|
158
|
+
person.(self, :name, :age)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
class Company
|
|
164
|
+
# ... Class Definition ... #
|
|
165
|
+
def to_builder
|
|
166
|
+
Jbuilder.new do |company|
|
|
167
|
+
company.name name
|
|
168
|
+
company.president president.to_builder
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
company = Company.new('Doodle Corp', Person.new('John Stobs', 58))
|
|
174
|
+
company.to_builder.target!
|
|
175
|
+
|
|
176
|
+
# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Rails Integration
|
|
180
|
+
|
|
181
|
+
You can either use Jbuilder stand-alone or directly as an ActionView template
|
|
182
|
+
language. When required in Rails, you can create views à la show.json.jbuilder
|
|
183
|
+
(the json is already yielded):
|
|
184
|
+
|
|
185
|
+
```ruby
|
|
186
|
+
# Any helpers available to views are available to the builder
|
|
187
|
+
json.content format_content(@message.content)
|
|
188
|
+
json.(@message, :created_at, :updated_at)
|
|
189
|
+
|
|
190
|
+
json.author do
|
|
191
|
+
json.name @message.creator.name.familiar
|
|
192
|
+
json.email_address @message.creator.email_address_with_name
|
|
193
|
+
json.url url_for(@message.creator, format: :json)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
if current_user.admin?
|
|
197
|
+
json.visitors calculate_visitors(@message)
|
|
198
|
+
end
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Partials
|
|
202
|
+
|
|
203
|
+
You can use partials as well. The following will render the file
|
|
204
|
+
`views/comments/_comments.json.jbuilder`, and set a local variable
|
|
205
|
+
`comments` with all this message's comments, which you can use inside
|
|
206
|
+
the partial.
|
|
207
|
+
|
|
208
|
+
```ruby
|
|
209
|
+
json.partial! 'comments/comments', comments: @message.comments
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
You can also render an object to a partial inline under a key.
|
|
213
|
+
|
|
214
|
+
```ruby
|
|
215
|
+
json.post @post, partial: 'posts/post', as: :post
|
|
216
|
+
# => { "post": { "title": "Hello World!", "author": { "name": "David" } } }
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
It's also possible to render collections of partials:
|
|
220
|
+
|
|
221
|
+
```ruby
|
|
222
|
+
json.array! @posts, partial: 'posts/post', as: :post
|
|
223
|
+
|
|
224
|
+
# or
|
|
225
|
+
json.partial! 'posts/post', collection: @posts, as: :post
|
|
226
|
+
|
|
227
|
+
# or
|
|
228
|
+
json.partial! partial: 'posts/post', collection: @posts, as: :post
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
You can also render to a collection of partials inline under a key.
|
|
232
|
+
|
|
233
|
+
```ruby
|
|
234
|
+
json.comments @post.comments, partial: 'comments/comment', as: :comment
|
|
235
|
+
# => { "comments": [{ "content": "Hello everyone!" }, { "content": "To you my good sir!" }] }
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
You can also provide other locals to the partial you're rendering to.
|
|
239
|
+
|
|
240
|
+
```ruby
|
|
241
|
+
# Provide the `include_body` local to the partial when rendering a single object
|
|
242
|
+
json.post @post, partial: 'posts/post', as: :post, include_body: true
|
|
243
|
+
|
|
244
|
+
# Provide a local to the partial when rendering a collection.
|
|
245
|
+
# Each item in the collection will render with `include_author: true`.
|
|
246
|
+
json.comments @post.comments, partial: 'comments/comment', as: :comment, include_author: true
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the
|
|
250
|
+
partial. If the value is a collection either implicitly or explicitly by using the `collection:` option, then each
|
|
251
|
+
value of the collection is passed to the partial as the variable `some_symbol`. If the value is a singular object,
|
|
252
|
+
then the object is passed to the partial as the variable `some_symbol`.
|
|
253
|
+
|
|
254
|
+
Be sure not to confuse the `as:` option to mean nesting of the partial. For example:
|
|
255
|
+
|
|
256
|
+
```ruby
|
|
257
|
+
# Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
|
|
258
|
+
# Note, `comment` attributes are "inlined".
|
|
259
|
+
json.partial! @comment, as: :comment
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
is quite different from:
|
|
263
|
+
|
|
264
|
+
```ruby
|
|
265
|
+
# comment attributes are nested under a "comment" property
|
|
266
|
+
json.comment do
|
|
267
|
+
json.partial! "/comments/comment.json.jbuilder", comment: @comment
|
|
268
|
+
end
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
You can pass any objects into partial templates with or without `:locals` option.
|
|
272
|
+
|
|
273
|
+
```ruby
|
|
274
|
+
json.partial! 'sub_template', locals: { user: user }
|
|
275
|
+
|
|
276
|
+
# or
|
|
277
|
+
|
|
278
|
+
json.partial! 'sub_template', user: user
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Null Values
|
|
282
|
+
|
|
283
|
+
You can explicitly make Jbuilder object return null if you want:
|
|
284
|
+
|
|
285
|
+
```ruby
|
|
286
|
+
json.extract! @post, :id, :title, :content, :published_at
|
|
287
|
+
json.author do
|
|
288
|
+
if @post.anonymous?
|
|
289
|
+
json.null! # or json.nil!
|
|
290
|
+
else
|
|
291
|
+
json.first_name @post.author_first_name
|
|
292
|
+
json.last_name @post.author_last_name
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
To prevent Jbuilder from including null values in the output, you can use the `ignore_nil!` method:
|
|
298
|
+
|
|
299
|
+
```ruby
|
|
300
|
+
json.ignore_nil!
|
|
301
|
+
json.foo nil
|
|
302
|
+
json.bar "bar"
|
|
303
|
+
# => { "bar": "bar" }
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Caching
|
|
307
|
+
|
|
308
|
+
Fragment caching is supported, it uses `Rails.cache` and works like caching in
|
|
309
|
+
HTML templates:
|
|
310
|
+
|
|
311
|
+
```ruby
|
|
312
|
+
json.cache! ['v1', @person], expires_in: 10.minutes do
|
|
313
|
+
json.extract! @person, :name, :age
|
|
314
|
+
end
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
You can also conditionally cache a block by using `cache_if!` like this:
|
|
318
|
+
|
|
319
|
+
```ruby
|
|
320
|
+
json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
|
|
321
|
+
json.extract! @person, :name, :age
|
|
322
|
+
end
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
Aside from that, the `:cached` options on collection rendering is available on Rails >= 6.0. This will cache the
|
|
326
|
+
rendered results effectively using the multi fetch feature.
|
|
327
|
+
|
|
328
|
+
```ruby
|
|
329
|
+
json.array! @posts, partial: "posts/post", as: :post, cached: true
|
|
330
|
+
|
|
331
|
+
# or:
|
|
332
|
+
json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
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:
|
|
336
|
+
|
|
337
|
+
```ruby
|
|
338
|
+
json.array! @posts, partial: "posts/post", as: :post, cached: -> post { [post, current_user] }
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
This will include both records as part of the cache key and updating either of them will expire the cache.
|
|
342
|
+
|
|
343
|
+
## Formatting Keys
|
|
344
|
+
|
|
345
|
+
Keys can be auto formatted using `key_format!`, this can be used to convert
|
|
346
|
+
key names from the standard ruby_format to camelCase:
|
|
347
|
+
|
|
348
|
+
```ruby
|
|
349
|
+
json.key_format! camelize: :lower
|
|
350
|
+
json.first_name 'David'
|
|
351
|
+
|
|
352
|
+
# => { "firstName": "David" }
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
You can set this globally with the class method `key_format` (from inside your
|
|
356
|
+
environment.rb for example):
|
|
357
|
+
|
|
358
|
+
```ruby
|
|
359
|
+
Jbuilder.key_format camelize: :lower
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
By default, key format is not applied to keys of hashes that are
|
|
363
|
+
passed to methods like `set!`, `array!` or `merge!`. You can opt into
|
|
364
|
+
deeply transforming these as well:
|
|
365
|
+
|
|
366
|
+
```ruby
|
|
367
|
+
json.key_format! camelize: :lower
|
|
368
|
+
json.deep_format_keys!
|
|
369
|
+
json.settings([{some_value: "abc"}])
|
|
370
|
+
|
|
371
|
+
# => { "settings": [{ "someValue": "abc" }]}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
You can set this globally with the class method `deep_format_keys` (from inside your
|
|
375
|
+
environment.rb for example):
|
|
376
|
+
|
|
377
|
+
```ruby
|
|
378
|
+
Jbuilder.deep_format_keys true
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
## Testing JBuilder Response body with RSpec
|
|
382
|
+
|
|
383
|
+
To test the response body of your controller spec, enable `render_views` in your RSpec context. This [configuration](https://rspec.info/features/6-0/rspec-rails/controller-specs/render-views) renders the views in a controller test.
|
|
384
|
+
|
|
385
|
+
## Contributing to Jbuilder
|
|
386
|
+
|
|
387
|
+
Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose
|
|
388
|
+
features and discuss issues.
|
|
389
|
+
|
|
390
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
|
391
|
+
|
|
392
|
+
## License
|
|
393
|
+
|
|
394
|
+
Jbuilder is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "bundler/gem_tasks"
|
|
5
|
+
require "rake/testtask"
|
|
6
|
+
|
|
7
|
+
if !ENV["APPRAISAL_INITIALIZED"] && !ENV["CI"]
|
|
8
|
+
require "appraisal/task"
|
|
9
|
+
Appraisal::Task.new
|
|
10
|
+
task default: :appraisal
|
|
11
|
+
else
|
|
12
|
+
Rake::TestTask.new do |test|
|
|
13
|
+
require "rails/version"
|
|
14
|
+
|
|
15
|
+
test.libs << "test"
|
|
16
|
+
|
|
17
|
+
test.test_files = FileList["test/*_test.rb"]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
task default: :test
|
|
21
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
VERSION=$1
|
|
4
|
+
|
|
5
|
+
printf "class Jbuilder\n VERSION = \"$VERSION\"\nend\n" > ./lib/jbuilder/version.rb
|
|
6
|
+
bundle
|
|
7
|
+
git add lib/jbuilder/version.rb
|
|
8
|
+
git commit -m "Bump version for $VERSION"
|
|
9
|
+
git push
|
|
10
|
+
git tag v$VERSION
|
|
11
|
+
git push --tags
|
|
12
|
+
gem build jbuilder.gemspec
|
|
13
|
+
gem push "jbuilder-$VERSION.gem" --host https://rubygems.org
|
|
14
|
+
rm "jbuilder-$VERSION.gem"
|