jbuilder 2.10.0 → 2.14.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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/devcontainer.json +25 -0
  3. data/.github/workflows/ruby.yml +56 -0
  4. data/.gitignore +2 -0
  5. data/Appraisals +14 -13
  6. data/CONTRIBUTING.md +5 -13
  7. data/Gemfile +2 -0
  8. data/README.md +113 -26
  9. data/Rakefile +3 -1
  10. data/bin/release +14 -0
  11. data/bin/test +6 -0
  12. data/gemfiles/{rails_5_1.gemfile → rails_7_0.gemfile} +2 -1
  13. data/gemfiles/{rails_5_2.gemfile → rails_7_1.gemfile} +1 -1
  14. data/gemfiles/{rails_6_0.gemfile → rails_7_2.gemfile} +1 -1
  15. data/gemfiles/{rails_5_0.gemfile → rails_8_0.gemfile} +1 -1
  16. data/gemfiles/rails_head.gemfile +1 -1
  17. data/jbuilder.gemspec +22 -3
  18. data/lib/generators/rails/jbuilder_generator.rb +10 -0
  19. data/lib/generators/rails/scaffold_controller_generator.rb +2 -0
  20. data/lib/generators/rails/templates/api_controller.rb +8 -2
  21. data/lib/generators/rails/templates/controller.rb +19 -17
  22. data/lib/generators/rails/templates/index.json.jbuilder +1 -1
  23. data/lib/generators/rails/templates/partial.json.jbuilder +14 -0
  24. data/lib/generators/rails/templates/show.json.jbuilder +1 -1
  25. data/lib/jbuilder/blank.rb +2 -0
  26. data/lib/jbuilder/collection_renderer.rb +58 -0
  27. data/lib/jbuilder/errors.rb +3 -1
  28. data/lib/jbuilder/jbuilder.rb +3 -7
  29. data/lib/jbuilder/jbuilder_dependency_tracker.rb +75 -0
  30. data/lib/jbuilder/jbuilder_template.rb +82 -48
  31. data/lib/jbuilder/key_formatter.rb +19 -21
  32. data/lib/jbuilder/railtie.rb +17 -19
  33. data/lib/jbuilder/version.rb +5 -0
  34. data/lib/jbuilder.rb +96 -51
  35. data/test/jbuilder_dependency_tracker_test.rb +2 -3
  36. data/test/jbuilder_generator_test.rb +22 -0
  37. data/test/jbuilder_template_test.rb +133 -8
  38. data/test/jbuilder_test.rb +211 -9
  39. data/test/scaffold_api_controller_generator_test.rb +63 -47
  40. data/test/scaffold_controller_generator_test.rb +47 -11
  41. data/test/test_helper.rb +24 -11
  42. metadata +37 -18
  43. data/.travis.yml +0 -47
  44. data/CHANGELOG.md +0 -283
  45. data/lib/jbuilder/dependency_tracker.rb +0 -61
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7289a23f21f82bfb3eeccd466ec985e8348a9977ed083be43eb8349551e840c
4
- data.tar.gz: 20d2ba506a7a474d2a32030d91cef03d4345f2332a0753bca685ce6360b30995
3
+ metadata.gz: 7980c25163c5711962622d39c236ac262bfd7917160eb5012c6e36a06c797984
4
+ data.tar.gz: 7b6881e873c5e3d6be33cf5e7a122de51c99a33ec61fa34b30baa5c5fa1c2c63
5
5
  SHA512:
6
- metadata.gz: fc04dfeb7c16b4547d0f7949b30ec481eb8517da3318550cf6c9f5e8bbc7bdc107c2d8bccc94e0e506741926d14f6798e783ece3640abb6edb323a39f7a691ae
7
- data.tar.gz: 29a6793c2a3b4318b27b2a0614cc88c2a5e4ca646f39a29cb563d370524bc3c946b9ea8bff114b6a5ae9ea5b4e9d3546e30211f0ada15ad74ff028663933e5d7
6
+ metadata.gz: 6c8477608caf773c5ef0123798df81cdcd8003081fa5dccd79f72a356f58b31c05a06bb1df16725150e01477f611332973e8e870db5239f6077501e9d560daed
7
+ data.tar.gz: 108438d96749fabc48b4432eaa5c9b6dcf47df1f0adcdf0ff3de68bb602f49e3bb0bfe542cd13e041501bcddb80f8a3fd7ff7ffed9882235ff6f6d4c0d1aa464
@@ -0,0 +1,25 @@
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the
2
+ // README at: https://github.com/devcontainers/templates/tree/main/src/ruby
3
+ {
4
+ "name": "jbuilder",
5
+ // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6
+ "image": "ghcr.io/rails/devcontainer/images/ruby:3.4.5",
7
+ "features": {
8
+ "ghcr.io/devcontainers/features/github-cli:1": {}
9
+ }
10
+
11
+ // Features to add to the dev container. More info: https://containers.dev/features.
12
+ // "features": {},
13
+
14
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
15
+ // "forwardPorts": [],
16
+
17
+ // Use 'postCreateCommand' to run commands after the container is created.
18
+ // "postCreateCommand": "ruby --version",
19
+
20
+ // Configure tool-specific properties.
21
+ // "customizations": {},
22
+
23
+ // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
24
+ // "remoteUser": "root"
25
+ }
@@ -0,0 +1,56 @@
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-latest
9
+ continue-on-error: ${{ matrix.gemfile == 'rails_head' }}
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
+ - "3.0"
19
+ - "3.1"
20
+ - "3.2"
21
+ - "3.3"
22
+ - "3.4"
23
+
24
+ gemfile:
25
+ - "rails_7_0"
26
+ - "rails_7_1"
27
+ - "rails_7_2"
28
+ - "rails_8_0"
29
+ - "rails_head"
30
+
31
+ exclude:
32
+ - ruby: '3.0'
33
+ gemfile: rails_7_2
34
+ - ruby: '3.0'
35
+ gemfile: rails_8_0
36
+ - ruby: '3.0'
37
+ gemfile: rails_head
38
+ - ruby: '3.1'
39
+ gemfile: rails_7_2
40
+ - ruby: '3.1'
41
+ gemfile: rails_8_0
42
+ - ruby: '3.1'
43
+ gemfile: rails_head
44
+ - ruby: '3.4'
45
+ gemfile: rails_7_0
46
+
47
+ steps:
48
+ - uses: actions/checkout@v4
49
+
50
+ - uses: ruby/setup-ruby@v1
51
+ with:
52
+ ruby-version: ${{ matrix.ruby }}
53
+ bundler-cache: true
54
+
55
+ - name: Ruby test
56
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -1,4 +1,6 @@
1
1
  tmp
2
+ /log
3
+ gemfiles/.bundle
2
4
  gemfiles/*.lock
3
5
  Gemfile.lock
4
6
  .ruby-version
data/Appraisals CHANGED
@@ -1,21 +1,22 @@
1
- appraise "rails-5-0" do
2
- gem "rails", "~> 5.0.0"
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
3
6
  end
4
7
 
5
- appraise "rails-5-1" do
6
- gem "rails", "~> 5.1.0"
8
+ appraise "rails-7-1" do
9
+ gem "rails", "~> 7.1.0"
7
10
  end
8
11
 
9
- appraise "rails-5-2" do
10
- gem "rails", "~> 5.2.0"
12
+ appraise "rails-7-2" do
13
+ gem "rails", "~> 7.2.0"
11
14
  end
12
15
 
13
- if RUBY_VERSION >= "2.5.0"
14
- appraise "rails-6-0" do
15
- gem "rails", "~> 6.0.0"
16
- end
16
+ appraise "rails-8-0" do
17
+ gem "rails", "~> 8.0.0"
18
+ end
17
19
 
18
- appraise "rails-head" do
19
- gem "rails", github: "rails/rails"
20
- end
20
+ appraise "rails-head" do
21
+ gem "rails", github: "rails/rails", branch: "main"
21
22
  end
data/CONTRIBUTING.md CHANGED
@@ -1,21 +1,19 @@
1
1
  Contributing to Jbuilder
2
2
  =====================
3
3
 
4
- [![Build Status](https://api.travis-ci.org/rails/jbuilder.svg?branch=master)][travis]
4
+ [![Build Status](https://github.com/rails/jbuilder/workflows/Ruby%20test/badge.svg)][test]
5
5
  [![Gem Version](https://badge.fury.io/rb/jbuilder.svg)][gem]
6
6
  [![Code Climate](https://codeclimate.com/github/rails/jbuilder/badges/gpa.svg)][codeclimate]
7
- [![Dependencies Status](https://gemnasium.com/rails/jbuilder.svg)][gemnasium]
8
7
 
9
- [travis]: https://travis-ci.org/rails/jbuilder
8
+ [test]: https://github.com/rails/jbuilder/actions?query=branch%3Amaster
10
9
  [gem]: https://rubygems.org/gems/jbuilder
11
10
  [codeclimate]: https://codeclimate.com/github/rails/jbuilder
12
- [gemnasium]: https://gemnasium.com/rails/jbuilder
13
11
 
14
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).
15
13
 
16
14
  #### Fork the Project
17
15
 
18
- Fork the [project on Github](https://github.com/rails/jbuilder) and check out your copy.
16
+ Fork the [project on GitHub](https://github.com/rails/jbuilder) and check out your copy.
19
17
 
20
18
  ```
21
19
  git clone https://github.com/contributor/jbuilder.git
@@ -35,13 +33,7 @@ git checkout -b my-feature-branch
35
33
 
36
34
  #### Bundle Install and Test
37
35
 
38
- Ensure that you can build the project and run tests.
39
-
40
- ```
41
- bundle install
42
- appraisal install
43
- appraisal rake test
44
- ```
36
+ Ensure that you can build the project and run tests using `bin/test`.
45
37
 
46
38
  #### Write Tests
47
39
 
@@ -97,7 +89,7 @@ git push origin my-feature-branch -f
97
89
 
98
90
  #### Check on Your Pull Request
99
91
 
100
- Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
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.
101
93
 
102
94
  #### Be Patient
103
95
 
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  gemspec
data/README.md CHANGED
@@ -5,7 +5,7 @@ manipulating giant hash structures. This is particularly helpful when the
5
5
  generation process is fraught with conditionals and loops. Here's a simple
6
6
  example:
7
7
 
8
- ``` ruby
8
+ ```ruby
9
9
  # app/views/messages/show.json.jbuilder
10
10
 
11
11
  json.content format_content(@message.content)
@@ -31,7 +31,7 @@ end
31
31
 
32
32
  This will build the following structure:
33
33
 
34
- ``` javascript
34
+ ```javascript
35
35
  {
36
36
  "content": "<p>This is <i>serious</i> monkey business</p>",
37
37
  "created_at": "2011-10-29T20:45:28-05:00",
@@ -57,9 +57,11 @@ This will build the following structure:
57
57
  }
58
58
  ```
59
59
 
60
+ ## Dynamically Defined Attributes
61
+
60
62
  To define attribute and structure names dynamically, use the `set!` method:
61
63
 
62
- ``` ruby
64
+ ```ruby
63
65
  json.set! :author do
64
66
  json.set! :name, 'David'
65
67
  end
@@ -67,10 +69,11 @@ end
67
69
  # => {"author": { "name": "David" }}
68
70
  ```
69
71
 
72
+ ## Merging Existing Hash or Array
70
73
 
71
74
  To merge existing hash or array to current context:
72
75
 
73
- ``` ruby
76
+ ```ruby
74
77
  hash = { author: { name: "David" } }
75
78
  json.post do
76
79
  json.title "Merge HOWTO"
@@ -80,9 +83,11 @@ end
80
83
  # => "post": { "title": "Merge HOWTO", "author": { "name": "David" } }
81
84
  ```
82
85
 
83
- Top level arrays can be handled directly. Useful for index and other collection actions.
86
+ ## Top Level Arrays
84
87
 
85
- ``` ruby
88
+ Top level arrays can be handled directly. Useful for index and other collection actions.
89
+
90
+ ```ruby
86
91
  # @comments = @post.comments
87
92
 
88
93
  json.array! @comments do |comment|
@@ -98,9 +103,11 @@ end
98
103
  # => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
99
104
  ```
100
105
 
106
+ ## Array Attributes
107
+
101
108
  You can also extract attributes from array directly.
102
109
 
103
- ``` ruby
110
+ ```ruby
104
111
  # @people = People.all
105
112
 
106
113
  json.array! @people, :id, :name
@@ -108,9 +115,42 @@ json.array! @people, :id, :name
108
115
  # => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
109
116
  ```
110
117
 
111
- Jbuilder objects can be directly nested inside each other. Useful for composing objects.
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
112
131
 
113
- ``` ruby
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
114
154
  class Person
115
155
  # ... Class Definition ... #
116
156
  def to_builder
@@ -136,11 +176,13 @@ company.to_builder.target!
136
176
  # => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
137
177
  ```
138
178
 
179
+ ## Rails Integration
180
+
139
181
  You can either use Jbuilder stand-alone or directly as an ActionView template
140
- language. When required in Rails, you can create views a la show.json.jbuilder
182
+ language. When required in Rails, you can create views à la show.json.jbuilder
141
183
  (the json is already yielded):
142
184
 
143
- ``` ruby
185
+ ```ruby
144
186
  # Any helpers available to views are available to the builder
145
187
  json.content format_content(@message.content)
146
188
  json.(@message, :created_at, :updated_at)
@@ -156,6 +198,8 @@ if current_user.admin?
156
198
  end
157
199
  ```
158
200
 
201
+ ## Partials
202
+
159
203
  You can use partials as well. The following will render the file
160
204
  `views/comments/_comments.json.jbuilder`, and set a local variable
161
205
  `comments` with all this message's comments, which you can use inside
@@ -171,32 +215,32 @@ It's also possible to render collections of partials:
171
215
  json.array! @posts, partial: 'posts/post', as: :post
172
216
 
173
217
  # or
174
-
175
218
  json.partial! 'posts/post', collection: @posts, as: :post
176
219
 
177
220
  # or
178
-
179
221
  json.partial! partial: 'posts/post', collection: @posts, as: :post
180
222
 
181
223
  # or
182
-
183
224
  json.comments @post.comments, partial: 'comments/comment', as: :comment
184
225
  ```
185
226
 
186
- The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the partial. If the value is a collection (either implicitly or explicitly by using the `collection:` option, then each value of the collection is passed to the partial as the variable `some_symbol`. If the value is a singular object, then the object is passed to the partial as the variable `some_symbol`.
227
+ The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the
228
+ partial. If the value is a collection either implicitly or explicitly by using the `collection:` option, then each
229
+ value of the collection is passed to the partial as the variable `some_symbol`. If the value is a singular object,
230
+ then the object is passed to the partial as the variable `some_symbol`.
187
231
 
188
232
  Be sure not to confuse the `as:` option to mean nesting of the partial. For example:
189
233
 
190
234
  ```ruby
191
- # Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
192
- # Note, `comment` attributes are "inlined".
193
- json.partial! @comment, as: :comment
235
+ # Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
236
+ # Note, `comment` attributes are "inlined".
237
+ json.partial! @comment, as: :comment
194
238
  ```
195
239
 
196
- is quite different than:
240
+ is quite different from:
197
241
 
198
242
  ```ruby
199
- # comment attributes are nested under a "comment" property
243
+ # comment attributes are nested under a "comment" property
200
244
  json.comment do
201
245
  json.partial! "/comments/comment.json.jbuilder", comment: @comment
202
246
  end
@@ -212,10 +256,11 @@ json.partial! 'sub_template', locals: { user: user }
212
256
  json.partial! 'sub_template', user: user
213
257
  ```
214
258
 
259
+ ## Null Values
215
260
 
216
261
  You can explicitly make Jbuilder object return null if you want:
217
262
 
218
- ``` ruby
263
+ ```ruby
219
264
  json.extract! @post, :id, :title, :content, :published_at
220
265
  json.author do
221
266
  if @post.anonymous?
@@ -236,6 +281,8 @@ json.bar "bar"
236
281
  # => { "bar": "bar" }
237
282
  ```
238
283
 
284
+ ## Caching
285
+
239
286
  Fragment caching is supported, it uses `Rails.cache` and works like caching in
240
287
  HTML templates:
241
288
 
@@ -253,14 +300,30 @@ json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
253
300
  end
254
301
  ```
255
302
 
256
- If you are rendering fragments for a collection of objects, have a look at
257
- `jbuilder_cache_multi` gem. It uses fetch_multi (>= Rails 4.1) to fetch
258
- multiple keys at once.
303
+ Aside from that, the `:cached` options on collection rendering is available on Rails >= 6.0. This will cache the
304
+ rendered results effectively using the multi fetch feature.
305
+
306
+ ```ruby
307
+ json.array! @posts, partial: "posts/post", as: :post, cached: true
308
+
309
+ # or:
310
+ json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true
311
+ ```
312
+
313
+ 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:
314
+
315
+ ```ruby
316
+ json.array! @posts, partial: "posts/post", as: :post, cached: -> post { [post, current_user] }
317
+ ```
318
+
319
+ This will include both records as part of the cache key and updating either of them will expire the cache.
320
+
321
+ ## Formatting Keys
259
322
 
260
323
  Keys can be auto formatted using `key_format!`, this can be used to convert
261
324
  keynames from the standard ruby_format to camelCase:
262
325
 
263
- ``` ruby
326
+ ```ruby
264
327
  json.key_format! camelize: :lower
265
328
  json.first_name 'David'
266
329
 
@@ -270,10 +333,33 @@ json.first_name 'David'
270
333
  You can set this globally with the class method `key_format` (from inside your
271
334
  environment.rb for example):
272
335
 
273
- ``` ruby
336
+ ```ruby
274
337
  Jbuilder.key_format camelize: :lower
275
338
  ```
276
339
 
340
+ By default, key format is not applied to keys of hashes that are
341
+ passed to methods like `set!`, `array!` or `merge!`. You can opt into
342
+ deeply transforming these as well:
343
+
344
+ ```ruby
345
+ json.key_format! camelize: :lower
346
+ json.deep_format_keys!
347
+ json.settings([{some_value: "abc"}])
348
+
349
+ # => { "settings": [{ "someValue": "abc" }]}
350
+ ```
351
+
352
+ You can set this globally with the class method `deep_format_keys` (from inside your
353
+ environment.rb for example):
354
+
355
+ ```ruby
356
+ Jbuilder.deep_format_keys true
357
+ ```
358
+
359
+ ## Testing JBuilder Response body with RSpec
360
+
361
+ 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.
362
+
277
363
  ## Contributing to Jbuilder
278
364
 
279
365
  Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose
@@ -282,4 +368,5 @@ features and discuss issues.
282
368
  See [CONTRIBUTING](CONTRIBUTING.md).
283
369
 
284
370
  ## License
371
+
285
372
  Jbuilder is released under the [MIT License](http://www.opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/setup"
2
4
  require "bundler/gem_tasks"
3
5
  require "rake/testtask"
4
6
 
5
- if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
7
+ if !ENV["APPRAISAL_INITIALIZED"] && !ENV["CI"]
6
8
  require "appraisal/task"
7
9
  Appraisal::Task.new
8
10
  task default: :appraisal
data/bin/release ADDED
@@ -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"
data/bin/test ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ bundle install
5
+ appraisal install
6
+ appraisal rake test
@@ -5,6 +5,7 @@ source "https://rubygems.org"
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "rails", "~> 5.1.0"
8
+ gem "rails", "~> 7.0.0"
9
+ gem "concurrent-ruby", "< 1.3.5"
9
10
 
10
11
  gemspec path: "../"
@@ -5,6 +5,6 @@ source "https://rubygems.org"
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "rails", "~> 5.2.0"
8
+ gem "rails", "~> 7.1.0"
9
9
 
10
10
  gemspec path: "../"
@@ -5,6 +5,6 @@ source "https://rubygems.org"
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "rails", "~> 6.0.0"
8
+ gem "rails", "~> 7.2.0"
9
9
 
10
10
  gemspec path: "../"
@@ -5,6 +5,6 @@ source "https://rubygems.org"
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "rails", "~> 5.0.0"
8
+ gem "rails", "~> 8.0.0"
9
9
 
10
10
  gemspec path: "../"
@@ -5,6 +5,6 @@ source "https://rubygems.org"
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "rails", github: "rails/rails"
8
+ gem "rails", github: "rails/rails", branch: "main"
9
9
 
10
10
  gemspec path: "../"
data/jbuilder.gemspec CHANGED
@@ -1,16 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/jbuilder/version"
4
+
1
5
  Gem::Specification.new do |s|
2
6
  s.name = 'jbuilder'
3
- s.version = '2.10.0'
7
+ s.version = Jbuilder::VERSION
4
8
  s.authors = 'David Heinemeier Hansson'
5
9
  s.email = 'david@basecamp.com'
6
10
  s.summary = 'Create JSON structures via a Builder-style DSL'
7
11
  s.homepage = 'https://github.com/rails/jbuilder'
8
12
  s.license = 'MIT'
9
13
 
10
- s.required_ruby_version = '>= 2.2.2'
14
+ s.required_ruby_version = '>= 3.0.0'
11
15
 
12
- s.add_dependency 'activesupport', '>= 5.0.0'
16
+ s.add_dependency 'activesupport', '>= 7.0.0'
17
+ s.add_dependency 'actionview', '>= 7.0.0'
18
+
19
+ if RUBY_ENGINE == 'rbx'
20
+ s.add_development_dependency('racc')
21
+ s.add_development_dependency('json')
22
+ s.add_development_dependency('rubysl')
23
+ end
13
24
 
14
25
  s.files = `git ls-files`.split("\n")
15
26
  s.test_files = `git ls-files -- test/*`.split("\n")
27
+
28
+ s.metadata = {
29
+ "bug_tracker_uri" => "https://github.com/rails/jbuilder/issues",
30
+ "changelog_uri" => "https://github.com/rails/jbuilder/releases/tag/v#{s.version}",
31
+ "mailing_list_uri" => "https://discuss.rubyonrails.org/c/rubyonrails-talk",
32
+ "source_code_uri" => "https://github.com/rails/jbuilder/tree/v#{s.version}",
33
+ "rubygems_mfa_required" => "true",
34
+ }
16
35
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/generators/named_base'
2
4
  require 'rails/generators/resource_helpers'
3
5
 
@@ -50,6 +52,14 @@ module Rails
50
52
 
51
53
  attributes.map { |a| ":#{a}"} * ', '
52
54
  end
55
+
56
+ def virtual_attributes
57
+ attributes.select {|name| name.respond_to?(:virtual?) && name.virtual? }
58
+ end
59
+
60
+ def partial_path_name
61
+ [controller_file_path, singular_table_name].join("/")
62
+ end
53
63
  end
54
64
  end
55
65
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/generators'
2
4
  require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
3
5
 
@@ -1,10 +1,10 @@
1
1
  <% if namespaced? -%>
2
- require_dependency "<%= namespaced_file_path %>/application_controller"
2
+ require_dependency "<%= namespaced_path %>/application_controller"
3
3
 
4
4
  <% end -%>
5
5
  <% module_namespacing do -%>
6
6
  class <%= controller_class_name %>Controller < ApplicationController
7
- before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
7
+ before_action :set_<%= singular_table_name %>, only: %i[ show update destroy ]
8
8
 
9
9
  # GET <%= route_url %>
10
10
  # GET <%= route_url %>.json
@@ -48,13 +48,19 @@ class <%= controller_class_name %>Controller < ApplicationController
48
48
  private
49
49
  # Use callbacks to share common setup or constraints between actions.
50
50
  def set_<%= singular_table_name %>
51
+ <%- if Rails::VERSION::MAJOR >= 8 -%>
52
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params.expect(:id)") %>
53
+ <%- else -%>
51
54
  @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
55
+ <%- end -%>
52
56
  end
53
57
 
54
58
  # Only allow a list of trusted parameters through.
55
59
  def <%= "#{singular_table_name}_params" %>
56
60
  <%- if attributes_names.empty? -%>
57
61
  params.fetch(<%= ":#{singular_table_name}" %>, {})
62
+ <%- elsif Rails::VERSION::MAJOR >= 8 -%>
63
+ params.expect(<%= singular_table_name %>: [ <%= permitted_params %> ])
58
64
  <%- else -%>
59
65
  params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
60
66
  <%- end -%>