jbuilder 2.9.1 → 2.11.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79b2c794b6341ee3d94c4c2d1ed8236e97357dac75dc9e49618a6a885ab35601
4
- data.tar.gz: 4bb8128da8ec6305ae36c3fa866cf19cd1eb08037128b2c3716002525e97014b
3
+ metadata.gz: 9ce8fbeac8b34ee290535e98a284a1247e2de1b5272c4679c1b89a89ff8149db
4
+ data.tar.gz: cdaeb1302c4755ca43f037328698afe29fe658f2094c99e2c6f81f0000b9368c
5
5
  SHA512:
6
- metadata.gz: c931310549e6384945735110e6c3a2ce36e566d12d691f33d8a5e68f1dca055eeda1ebe606fb093ec85b4c2481c05ebb7dc639f9209a8e4d324f59dd7b4c8693
7
- data.tar.gz: 277b4c68a0fe426e308f6c5fbc59c8b7453d61291aaf3968e4500532f86e902bbc8ac34970ccd2496935c361584773616a4ff71745b7adbabc752b09ba0da9de
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
@@ -1,4 +1,5 @@
1
1
  tmp
2
+ gemfiles/.bundle
2
3
  gemfiles/*.lock
3
4
  Gemfile.lock
4
5
  .ruby-version
data/Appraisals CHANGED
@@ -1,27 +1,25 @@
1
- appraise "rails-4-2" do
2
- gem "rails", "~> 4.2.0"
1
+ appraise "rails-5-0" do
2
+ gem "rails", "~> 5.0.0"
3
3
  end
4
4
 
5
- if RUBY_VERSION >= "2.2.2"
6
- appraise "rails-5-0" do
7
- gem "rails", "~> 5.0.0"
8
- end
9
-
10
- appraise "rails-5-1" do
11
- gem "rails", "~> 5.1.0"
12
- end
5
+ appraise "rails-5-1" do
6
+ gem "rails", "~> 5.1.0"
7
+ end
13
8
 
14
- appraise "rails-5-2" do
15
- gem "rails", "~> 5.2.0"
16
- end
9
+ appraise "rails-5-2" do
10
+ gem "rails", "~> 5.2.0"
17
11
  end
18
12
 
19
13
  if RUBY_VERSION >= "2.5.0"
20
14
  appraise "rails-6-0" do
21
- gem "rails", "~> 6.0.0.rc1"
15
+ gem "rails", "~> 6.0.0"
16
+ end
17
+
18
+ appraise "rails-6-1" do
19
+ gem "rails", "~> 6.1.0"
22
20
  end
23
21
 
24
22
  appraise "rails-head" do
25
- gem "rails", github: "rails/rails"
23
+ gem "rails", github: "rails/rails", branch: "main"
26
24
  end
27
25
  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
@@ -97,7 +95,7 @@ git push origin my-feature-branch -f
97
95
 
98
96
  #### Check on Your Pull Request
99
97
 
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.
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.
101
99
 
102
100
  #### Be Patient
103
101
 
data/README.md CHANGED
@@ -108,6 +108,33 @@ json.array! @people, :id, :name
108
108
  # => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
109
109
  ```
110
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
+
111
138
  Jbuilder objects can be directly nested inside each other. Useful for composing objects.
112
139
 
113
140
  ``` ruby
@@ -137,7 +164,7 @@ company.to_builder.target!
137
164
  ```
138
165
 
139
166
  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
167
+ language. When required in Rails, you can create views à la show.json.jbuilder
141
168
  (the json is already yielded):
142
169
 
143
170
  ``` ruby
@@ -171,19 +198,19 @@ It's also possible to render collections of partials:
171
198
  json.array! @posts, partial: 'posts/post', as: :post
172
199
 
173
200
  # or
174
-
175
201
  json.partial! 'posts/post', collection: @posts, as: :post
176
202
 
177
203
  # or
178
-
179
204
  json.partial! partial: 'posts/post', collection: @posts, as: :post
180
205
 
181
206
  # or
182
-
183
207
  json.comments @post.comments, partial: 'comments/comment', as: :comment
184
208
  ```
185
209
 
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`.
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`.
187
214
 
188
215
  Be sure not to confuse the `as:` option to mean nesting of the partial. For example:
189
216
 
@@ -193,7 +220,7 @@ Be sure not to confuse the `as:` option to mean nesting of the partial. For exam
193
220
  json.partial! @comment, as: :comment
194
221
  ```
195
222
 
196
- is quite different than:
223
+ is quite different from:
197
224
 
198
225
  ```ruby
199
226
  # comment attributes are nested under a "comment" property
@@ -236,6 +263,8 @@ json.bar "bar"
236
263
  # => { "bar": "bar" }
237
264
  ```
238
265
 
266
+ ## Caching
267
+
239
268
  Fragment caching is supported, it uses `Rails.cache` and works like caching in
240
269
  HTML templates:
241
270
 
@@ -253,9 +282,25 @@ json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
253
282
  end
254
283
  ```
255
284
 
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.
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
259
304
 
260
305
  Keys can be auto formatted using `key_format!`, this can be used to convert
261
306
  keynames from the standard ruby_format to camelCase:
@@ -274,6 +319,25 @@ environment.rb for example):
274
319
  Jbuilder.key_format camelize: :lower
275
320
  ```
276
321
 
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
+
277
341
  ## Contributing to Jbuilder
278
342
 
279
343
  Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require "bundler/setup"
2
2
  require "bundler/gem_tasks"
3
3
  require "rake/testtask"
4
4
 
5
- if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
5
+ if !ENV["APPRAISAL_INITIALIZED"] && !ENV["CI"]
6
6
  require "appraisal/task"
7
7
  Appraisal::Task.new
8
8
  task default: :appraisal
@@ -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.rc1"
8
+ gem "rails", "~> 6.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", "~> 4.2.0"
8
+ gem "rails", "~> 6.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", github: "rails/rails"
8
+ gem "rails", github: "rails/rails", branch: "main"
9
9
 
10
10
  gemspec path: "../"
data/jbuilder.gemspec CHANGED
@@ -1,16 +1,31 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '2.9.1'
3
+ s.version = '2.11.5'
4
4
  s.authors = 'David Heinemeier Hansson'
5
5
  s.email = 'david@basecamp.com'
6
6
  s.summary = 'Create JSON structures via a Builder-style DSL'
7
7
  s.homepage = 'https://github.com/rails/jbuilder'
8
8
  s.license = 'MIT'
9
9
 
10
- s.required_ruby_version = '>= 1.9.3'
10
+ s.required_ruby_version = '>= 2.2.2'
11
11
 
12
- s.add_dependency 'activesupport', '>= 4.2.0'
12
+ s.add_dependency 'activesupport', '>= 5.0.0'
13
+ s.add_dependency 'actionview', '>= 5.0.0'
14
+
15
+ if RUBY_ENGINE == 'rbx'
16
+ s.add_development_dependency('racc')
17
+ s.add_development_dependency('json')
18
+ s.add_development_dependency('rubysl')
19
+ end
13
20
 
14
21
  s.files = `git ls-files`.split("\n")
15
22
  s.test_files = `git ls-files -- test/*`.split("\n")
23
+
24
+ s.metadata = {
25
+ "bug_tracker_uri" => "https://github.com/rails/jbuilder/issues",
26
+ "changelog_uri" => "https://github.com/rails/jbuilder/releases/tag/v#{s.version}",
27
+ "mailing_list_uri" => "https://discuss.rubyonrails.org/c/rubyonrails-talk",
28
+ "source_code_uri" => "https://github.com/rails/jbuilder/tree/v#{s.version}",
29
+ "rubygems_mfa_required" => "true",
30
+ }
16
31
  end
@@ -50,6 +50,10 @@ module Rails
50
50
 
51
51
  attributes.map { |a| ":#{a}"} * ', '
52
52
  end
53
+
54
+ def virtual_attributes
55
+ attributes.select {|name| name.respond_to?(:virtual?) && name.virtual? }
56
+ end
53
57
  end
54
58
  end
55
59
  end
@@ -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
@@ -51,7 +51,7 @@ class <%= controller_class_name %>Controller < ApplicationController
51
51
  @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
52
52
  end
53
53
 
54
- # Never trust parameters from the scary internet, only allow the white list through.
54
+ # Only allow a list of trusted parameters through.
55
55
  def <%= "#{singular_table_name}_params" %>
56
56
  <%- if attributes_names.empty? -%>
57
57
  params.fetch(<%= ":#{singular_table_name}" %>, {})
@@ -1,19 +1,17 @@
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, :edit, :update, :destroy]
7
+ before_action :set_<%= singular_table_name %>, only: %i[ show edit update destroy ]
8
8
 
9
- # GET <%= route_url %>
10
- # GET <%= route_url %>.json
9
+ # GET <%= route_url %> or <%= route_url %>.json
11
10
  def index
12
11
  @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
13
12
  end
14
13
 
15
- # GET <%= route_url %>/1
16
- # GET <%= route_url %>/1.json
14
+ # GET <%= route_url %>/1 or <%= route_url %>/1.json
17
15
  def show
18
16
  end
19
17
 
@@ -26,42 +24,40 @@ class <%= controller_class_name %>Controller < ApplicationController
26
24
  def edit
27
25
  end
28
26
 
29
- # POST <%= route_url %>
30
- # POST <%= route_url %>.json
27
+ # POST <%= route_url %> or <%= route_url %>.json
31
28
  def create
32
29
  @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
33
30
 
34
31
  respond_to do |format|
35
32
  if @<%= orm_instance.save %>
36
- format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> }
33
+ format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> }
37
34
  format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> }
38
35
  else
39
- format.html { render :new }
36
+ format.html { render :new, status: :unprocessable_entity }
40
37
  format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
41
38
  end
42
39
  end
43
40
  end
44
41
 
45
- # PATCH/PUT <%= route_url %>/1
46
- # PATCH/PUT <%= route_url %>/1.json
42
+ # PATCH/PUT <%= route_url %>/1 or <%= route_url %>/1.json
47
43
  def update
48
44
  respond_to do |format|
49
45
  if @<%= orm_instance.update("#{singular_table_name}_params") %>
50
- format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
46
+ format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> }
51
47
  format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> }
52
48
  else
53
- format.html { render :edit }
49
+ format.html { render :edit, status: :unprocessable_entity }
54
50
  format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
55
51
  end
56
52
  end
57
53
  end
58
54
 
59
- # DELETE <%= route_url %>/1
60
- # DELETE <%= route_url %>/1.json
55
+ # DELETE <%= route_url %>/1 or <%= route_url %>/1.json
61
56
  def destroy
62
57
  @<%= orm_instance.destroy %>
58
+
63
59
  respond_to do |format|
64
- format.html { redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %> }
60
+ format.html { redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> }
65
61
  format.json { head :no_content }
66
62
  end
67
63
  end
@@ -72,7 +68,7 @@ class <%= controller_class_name %>Controller < ApplicationController
72
68
  @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
73
69
  end
74
70
 
75
- # Never trust parameters from the scary internet, only allow the white list through.
71
+ # Only allow a list of trusted parameters through.
76
72
  def <%= "#{singular_table_name}_params" %>
77
73
  <%- if attributes_names.empty? -%>
78
74
  params.fetch(<%= ":#{singular_table_name}" %>, {})
@@ -1,2 +1,16 @@
1
1
  json.extract! <%= singular_table_name %>, <%= full_attributes_list %>
2
2
  json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
3
+ <%- virtual_attributes.each do |attribute| -%>
4
+ <%- if attribute.type == :rich_text -%>
5
+ json.<%= attribute.name %> <%= singular_table_name %>.<%= attribute.name %>.to_s
6
+ <%- elsif attribute.type == :attachment -%>
7
+ json.<%= attribute.name %> url_for(<%= singular_table_name %>.<%= attribute.name %>)
8
+ <%- elsif attribute.type == :attachments -%>
9
+ json.<%= attribute.name %> do
10
+ json.array!(<%= singular_table_name %>.<%= attribute.name %>) do |<%= attribute.singular_name %>|
11
+ json.id <%= attribute.singular_name %>.id
12
+ json.url url_for(<%= attribute.singular_name %>)
13
+ end
14
+ end
15
+ <%- end -%>
16
+ <%- end -%>
@@ -0,0 +1,109 @@
1
+ require 'delegate'
2
+ require 'active_support/concern'
3
+ require 'action_view'
4
+
5
+ begin
6
+ require 'action_view/renderer/collection_renderer'
7
+ rescue LoadError
8
+ require 'action_view/renderer/partial_renderer'
9
+ end
10
+
11
+ class Jbuilder
12
+ module CollectionRenderable # :nodoc:
13
+ extend ActiveSupport::Concern
14
+
15
+ class_methods do
16
+ def supported?
17
+ superclass.private_method_defined?(:build_rendered_template) && self.superclass.private_method_defined?(:build_rendered_collection)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def build_rendered_template(content, template, layout = nil)
24
+ super(content || json.attributes!, template)
25
+ end
26
+
27
+ def build_rendered_collection(templates, _spacer)
28
+ json.merge!(templates.map(&:body))
29
+ end
30
+
31
+ def json
32
+ @options[:locals].fetch(:json)
33
+ end
34
+
35
+ class ScopedIterator < ::SimpleDelegator # :nodoc:
36
+ include Enumerable
37
+
38
+ def initialize(obj, scope)
39
+ super(obj)
40
+ @scope = scope
41
+ end
42
+
43
+ # Rails 6.0 support:
44
+ def each
45
+ return enum_for(:each) unless block_given?
46
+
47
+ __getobj__.each do |object|
48
+ @scope.call { yield(object) }
49
+ end
50
+ end
51
+
52
+ # Rails 6.1 support:
53
+ def each_with_info
54
+ return enum_for(:each_with_info) unless block_given?
55
+
56
+ __getobj__.each_with_info do |object, info|
57
+ @scope.call { yield(object, info) }
58
+ end
59
+ end
60
+ end
61
+
62
+ private_constant :ScopedIterator
63
+ end
64
+
65
+ if defined?(::ActionView::CollectionRenderer)
66
+ # Rails 6.1 support:
67
+ class CollectionRenderer < ::ActionView::CollectionRenderer # :nodoc:
68
+ include CollectionRenderable
69
+
70
+ def initialize(lookup_context, options, &scope)
71
+ super(lookup_context, options)
72
+ @scope = scope
73
+ end
74
+
75
+ private
76
+ def collection_with_template(view, template, layout, collection)
77
+ super(view, template, layout, ScopedIterator.new(collection, @scope))
78
+ end
79
+ end
80
+ else
81
+ # Rails 6.0 support:
82
+ class CollectionRenderer < ::ActionView::PartialRenderer # :nodoc:
83
+ include CollectionRenderable
84
+
85
+ def initialize(lookup_context, options, &scope)
86
+ super(lookup_context)
87
+ @options = options
88
+ @scope = scope
89
+ end
90
+
91
+ def render_collection_with_partial(collection, partial, context, block)
92
+ render(context, @options.merge(collection: collection, partial: partial), block)
93
+ end
94
+
95
+ private
96
+ def collection_without_template(view)
97
+ @collection = ScopedIterator.new(@collection, @scope)
98
+
99
+ super(view)
100
+ end
101
+
102
+ def collection_with_template(view, template)
103
+ @collection = ScopedIterator.new(@collection, @scope)
104
+
105
+ super(view, template)
106
+ end
107
+ end
108
+ end
109
+ end