jbuilder 2.7.0 → 2.9.0

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
- SHA1:
3
- metadata.gz: 45c3afb337be47562a40f26a3ca88c30c866a6bf
4
- data.tar.gz: 735bc2cec1ed339e9ed9ab106d039fb6c877f486
2
+ SHA256:
3
+ metadata.gz: 780a2d0395698145f0e1c9e96a7a2079b70def4be27846b74537ec90786b9495
4
+ data.tar.gz: da6e2704a1060b023c7a78d4539ad39da447dba6e3d7c657834a81badcd47afa
5
5
  SHA512:
6
- metadata.gz: e2ba8f0402fb0f931a85a7a3d64af40908076ed3a0acdc47af4c250ed77b19606ac0203bc30640b832bf7a73ae5ee69448f2a837eea4347838f6beb23c7ea40c
7
- data.tar.gz: c8c798a98892a6b131944a1d3eba848cee0e7ec8a3a4a81bac765dec866c03f5d3c6e70c736692d81c2680d46f7d6d74eb4ac56a270250c2bcb9148685cb278e
6
+ metadata.gz: c088b1a55e41c650bccd49901ab964ac9b4b715bdab5d51d68a5b34b6a3bb18adcb37ade32548fa1f69ecd4d2cb29c8f34f747dd4af80daa4c8115e99bd80489
7
+ data.tar.gz: 23e2b31830276ba589c941a484ac6d9b80dc0e3a0f4c4767f8d534f1bdfae5d9e76d1e75a31127103a045afba127c6eb2a7e87f35327860729ef7f08aafdd766
data/.travis.yml CHANGED
@@ -1,66 +1,60 @@
1
1
  language: ruby
2
2
 
3
- sudo: false
4
3
  cache: bundler
5
4
 
5
+ before_install:
6
+ - "gem update --system 2.7.9"
7
+ - "gem install bundler -v '<2'"
8
+
6
9
  rvm:
7
- - 1.9
8
- - 2.0
9
- - 2.1
10
- - 2.2.5
11
- - 2.3.1
12
- - 2.4.0
10
+ - 2.2.10
11
+ - 2.3.8
12
+ - 2.4.6
13
+ - 2.5.5
14
+ - 2.6.2
13
15
  - ruby-head
14
- - jruby-19mode
15
- - rbx
16
+ - rbx-3.107
16
17
 
17
18
  gemfile:
18
- - gemfiles/rails_3_0.gemfile
19
- - gemfiles/rails_3_1.gemfile
20
- - gemfiles/rails_3_2.gemfile
21
- - gemfiles/rails_4_0.gemfile
22
- - gemfiles/rails_4_1.gemfile
23
19
  - gemfiles/rails_4_2.gemfile
24
20
  - gemfiles/rails_5_0.gemfile
25
21
  - gemfiles/rails_5_1.gemfile
22
+ - gemfiles/rails_5_2.gemfile
23
+ - gemfiles/rails_head.gemfile
26
24
 
27
25
  matrix:
28
- allow_failures:
29
- - rvm: jruby-19mode
30
- - rvm: rbx
31
- - rvm: ruby-head
32
- fast_finish: true
33
- exclude:
34
- - rvm: 1.9
35
- gemfile: gemfiles/rails_5_0.gemfile
26
+ include:
36
27
  - rvm: 1.9
37
- gemfile: gemfiles/rails_5_1.gemfile
28
+ gemfile: gemfiles/rails_4_2.gemfile
38
29
  - rvm: 2.0
39
- gemfile: gemfiles/rails_5_0.gemfile
40
- - rvm: 2.0
41
- gemfile: gemfiles/rails_5_1.gemfile
42
- - rvm: 2.1
43
- gemfile: gemfiles/rails_5_0.gemfile
30
+ gemfile: gemfiles/rails_4_2.gemfile
44
31
  - rvm: 2.1
45
- gemfile: gemfiles/rails_5_1.gemfile
32
+ gemfile: gemfiles/rails_4_2.gemfile
33
+ - rvm: 2.3
34
+ gemfile: gemfiles/rails_4_2.gemfile
46
35
  - rvm: jruby-19mode
47
- gemfile: gemfiles/rails_5_0.gemfile
36
+ gemfile: gemfiles/rails_4_2.gemfile
37
+ - rvm: rbx-3.107
38
+ gemfile: gemfiles/rails_4_2.gemfile
39
+ exclude:
40
+ - rvm: 2.4.6
41
+ gemfile: gemfiles/rails_4_2.gemfile
42
+ - rvm: 2.5.5
43
+ gemfile: gemfiles/rails_4_2.gemfile
44
+ - rvm: 2.6.2
45
+ gemfile: gemfiles/rails_4_2.gemfile
46
+ - rvm: 2.2.10
47
+ gemfile: gemfiles/rails_head.gemfile
48
+ - rvm: 2.3.8
49
+ gemfile: gemfiles/rails_head.gemfile
50
+ - rvm: 2.4.6
51
+ gemfile: gemfiles/rails_head.gemfile
52
+ allow_failures:
48
53
  - rvm: jruby-19mode
49
- gemfile: gemfiles/rails_5_1.gemfile
50
- - rvm: rbx
51
- gemfile: gemfiles/rails_5_0.gemfile
52
- - rvm: rbx
53
- gemfile: gemfiles/rails_5_1.gemfile
54
- - rvm: 2.4.0
55
- gemfile: gemfiles/rails_3_0.gemfile
56
- - rvm: 2.4.0
57
- gemfile: gemfiles/rails_3_1.gemfile
58
- - rvm: 2.4.0
59
- gemfile: gemfiles/rails_3_2.gemfile
60
- - rvm: 2.4.0
61
- gemfile: gemfiles/rails_4_0.gemfile
62
- - rvm: 2.4.0
63
- gemfile: gemfiles/rails_4_1.gemfile
54
+ - rvm: rbx-3.107
55
+ - rvm: ruby-head
56
+ - gemfile: gemfiles/rails_head.gemfile
57
+ fast_finish: true
64
58
 
65
59
  notifications:
66
60
  email: false
data/Appraisals CHANGED
@@ -1,17 +1,23 @@
1
1
  appraise "rails-4-2" do
2
- gem "railties", "~> 4.2.0"
3
- gem "actionpack", "~> 4.2.0"
4
- gem "activemodel", "~> 4.2.0"
2
+ gem "rails", "~> 4.2.0"
5
3
  end
6
4
 
7
- appraise "rails-5-0" do
8
- gem "railties", "~> 5.0.0"
9
- gem "actionpack", "~> 5.0.0"
10
- gem "activemodel", "~> 5.0.0"
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
13
+
14
+ appraise "rails-5-2" do
15
+ gem "rails", "~> 5.2.0"
16
+ end
11
17
  end
12
18
 
13
- appraise "rails-5-1" do
14
- gem "railties", ">= 5.1.0", "< 5.2"
15
- gem "actionpack", ">= 5.1.0", "< 5.2"
16
- gem "activemodel", ">= 5.1.0", "< 5.2"
19
+ if RUBY_VERSION >= "2.5.0"
20
+ appraise "rails-head" do
21
+ gem "rails", github: "rails/rails"
22
+ end
17
23
  end
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ 2.8.0
4
+ -----
5
+
6
+ * [Replace deprecated fragment_cache_key for Rails 5.2 support](https://github.com/rails/jbuilder/pull/430)
7
+
8
+ 2.7.0
9
+ -----
10
+
11
+ * [Requires Rails 4+](https://github.com/rails/jbuilder/commit/5207ff394533177fffdd768bfaa6413a0cd16dc8)
12
+ * [Fix implicitly rendering a JSON partial with the same name as an
13
+ HTML partial](https://github.com/rails/jbuilder/pull/400)
14
+
3
15
  2.6.4
4
16
  -----
5
17
 
data/Gemfile CHANGED
@@ -5,4 +5,3 @@ gemspec
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "pry"
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011-2017 David Heinemeier Hansson, 37signals
1
+ Copyright (c) 2011-2018 David Heinemeier Hansson, 37signals
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -67,6 +67,19 @@ end
67
67
  # => {"author": { "name": "David" }}
68
68
  ```
69
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" } }
81
+ ```
82
+
70
83
  Top level arrays can be handled directly. Useful for index and other collection actions.
71
84
 
72
85
  ``` ruby
@@ -143,7 +156,6 @@ if current_user.admin?
143
156
  end
144
157
  ```
145
158
 
146
-
147
159
  You can use partials as well. The following will render the file
148
160
  `views/comments/_comments.json.jbuilder`, and set a local variable
149
161
  `comments` with all this message's comments, which you can use inside
@@ -171,6 +183,25 @@ json.partial! partial: 'posts/post', collection: @posts, as: :post
171
183
  json.comments @post.comments, partial: 'comments/comment', as: :comment
172
184
  ```
173
185
 
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`.
187
+
188
+ Be sure not to confuse the `as:` option to mean nesting of the partial. For example:
189
+
190
+ ```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
194
+ ```
195
+
196
+ is quite different than:
197
+
198
+ ```ruby
199
+ # comment attributes are nested under a "comment" property
200
+ json.comment do
201
+ json.partial! "/comments/comment.json.jbuilder", comment: @comment
202
+ end
203
+ ```
204
+
174
205
  You can pass any objects into partial templates with or without `:locals` option.
175
206
 
176
207
  ```ruby
@@ -243,21 +274,6 @@ environment.rb for example):
243
274
  Jbuilder.key_format camelize: :lower
244
275
  ```
245
276
 
246
- Faster JSON backends
247
- --------------------
248
-
249
- Jbuilder uses MultiJson, which by default will use the JSON gem. That gem is
250
- currently tangled with ActiveSupport's all-Ruby `#to_json` implementation,
251
- which is slow (fixed in Rails >= 4.1). For faster Jbuilder rendering, you can
252
- specify something like the Yajl JSON generator instead. You'll need to include
253
- the `yajl-ruby` gem in your Gemfile and then set the following configuration
254
- for MultiJson:
255
-
256
- ``` ruby
257
- require 'multi_json'
258
- MultiJson.use :yajl
259
- ```
260
-
261
277
  ## Contributing to Jbuilder
262
278
 
263
279
  Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose
@@ -5,9 +5,6 @@ source "https://rubygems.org"
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "pry"
9
- gem "railties", "~> 4.2.0"
10
- gem "actionpack", "~> 4.2.0"
11
- gem "activemodel", "~> 4.2.0"
8
+ gem "rails", "~> 4.2.0"
12
9
 
13
10
  gemspec path: "../"
@@ -5,9 +5,6 @@ source "https://rubygems.org"
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "pry"
9
- gem "railties", "~> 5.0.0"
10
- gem "actionpack", "~> 5.0.0"
11
- gem "activemodel", "~> 5.0.0"
8
+ gem "rails", "~> 5.0.0"
12
9
 
13
10
  gemspec path: "../"
@@ -5,9 +5,6 @@ source "https://rubygems.org"
5
5
  gem "rake"
6
6
  gem "mocha", require: false
7
7
  gem "appraisal"
8
- gem "pry"
9
- gem "railties", ">= 5.1.0", "< 5.2"
10
- gem "actionpack", ">= 5.1.0", "< 5.2"
11
- gem "activemodel", ">= 5.1.0", "< 5.2"
8
+ gem "rails", "~> 5.1.0"
12
9
 
13
10
  gemspec path: "../"
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "mocha", require: false
7
+ gem "appraisal"
8
+ gem "rails", "~> 5.2.0"
9
+
10
+ gemspec path: "../"
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "mocha", require: false
7
+ gem "appraisal"
8
+ gem "rails", github: "rails/rails"
9
+
10
+ gemspec path: "../"
data/jbuilder.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '2.7.0'
3
+ s.version = '2.9.0'
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'
@@ -10,7 +10,6 @@ Gem::Specification.new do |s|
10
10
  s.required_ruby_version = '>= 1.9.3'
11
11
 
12
12
  s.add_dependency 'activesupport', '>= 4.2.0'
13
- s.add_dependency 'multi_json', '>= 1.2'
14
13
 
15
14
  s.files = `git ls-files`.split("\n")
16
15
  s.test_files = `git ls-files -- test/*`.split("\n")
@@ -10,6 +10,8 @@ module Rails
10
10
 
11
11
  argument :attributes, type: :array, default: [], banner: 'field:type field:type'
12
12
 
13
+ class_option :timestamps, type: :boolean, default: true
14
+
13
15
  def create_root_folder
14
16
  path = File.join('app/views', controller_file_path)
15
17
  empty_directory path unless File.directory?(path)
@@ -33,8 +35,12 @@ module Rails
33
35
  [name, :json, :jbuilder] * '.'
34
36
  end
35
37
 
36
- def attributes_list_with_timestamps
37
- attributes_list(attributes_names + %w(created_at updated_at))
38
+ def full_attributes_list
39
+ if options[:timestamps]
40
+ attributes_list(attributes_names + %w(created_at updated_at))
41
+ else
42
+ attributes_list(attributes_names)
43
+ end
38
44
  end
39
45
 
40
46
  def attributes_list(attributes = attributes_names)
@@ -7,6 +7,12 @@ module Rails
7
7
  source_paths << File.expand_path('../templates', __FILE__)
8
8
 
9
9
  hook_for :jbuilder, type: :boolean, default: true
10
+
11
+ private
12
+
13
+ def permitted_params
14
+ attributes_names.map { |name| ":#{name}" }.join(', ')
15
+ end unless private_method_defined? :permitted_params
10
16
  end
11
17
  end
12
18
  end
@@ -56,7 +56,7 @@ class <%= controller_class_name %>Controller < ApplicationController
56
56
  <%- if attributes_names.empty? -%>
57
57
  params.fetch(<%= ":#{singular_table_name}" %>, {})
58
58
  <%- else -%>
59
- params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
59
+ params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
60
60
  <%- end -%>
61
61
  end
62
62
  end
@@ -77,7 +77,7 @@ class <%= controller_class_name %>Controller < ApplicationController
77
77
  <%- if attributes_names.empty? -%>
78
78
  params.fetch(<%= ":#{singular_table_name}" %>, {})
79
79
  <%- else -%>
80
- params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
80
+ params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
81
81
  <%- end -%>
82
82
  end
83
83
  end
@@ -1 +1 @@
1
- json.array! @<%= plural_table_name %>, partial: '<%= plural_table_name %>/<%= singular_table_name %>', as: :<%= singular_table_name %>
1
+ json.array! @<%= plural_table_name %>, partial: "<%= plural_table_name %>/<%= singular_table_name %>", as: :<%= singular_table_name %>
@@ -1,2 +1,2 @@
1
- json.extract! <%= singular_table_name %>, <%= attributes_list_with_timestamps %>
1
+ json.extract! <%= singular_table_name %>, <%= full_attributes_list %>
2
2
  json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
@@ -104,7 +104,7 @@ class JbuilderTemplate < Jbuilder
104
104
  private
105
105
 
106
106
  def _render_partial_with_options(options)
107
- options.reverse_merge! locals: {}
107
+ options.reverse_merge! locals: options.except(:partial, :as, :collection)
108
108
  options.reverse_merge! ::JbuilderTemplate.template_lookup_options
109
109
  as = options[:as]
110
110
 
@@ -151,8 +151,8 @@ class JbuilderTemplate < Jbuilder
151
151
  name_options = options.slice(:skip_digest, :virtual_path)
152
152
  key = _fragment_name_with_digest(key, name_options)
153
153
 
154
- if @context.respond_to?(:fragment_cache_key)
155
- key = @context.fragment_cache_key(key)
154
+ if @context.respond_to?(:combined_fragment_cache_key)
155
+ key = @context.combined_fragment_cache_key(key)
156
156
  else
157
157
  key = url_for(key).split('://', 2).last if ::Hash === key
158
158
  end
@@ -222,11 +222,12 @@ end
222
222
 
223
223
  class JbuilderHandler
224
224
  cattr_accessor :default_format
225
- self.default_format = Mime[:json]
225
+ self.default_format = :json
226
226
 
227
- def self.call(template)
227
+ def self.call(template, source = nil)
228
+ source ||= template.source
228
229
  # this juggling is required to keep line numbers right in the error
229
- %{__already_defined = defined?(json); json||=JbuilderTemplate.new(self); #{template.source}
230
+ %{__already_defined = defined?(json); json||=JbuilderTemplate.new(self); #{source}
230
231
  json.target! unless (__already_defined && __already_defined != "method")}
231
232
  end
232
233
  end
data/lib/jbuilder.rb CHANGED
@@ -2,7 +2,7 @@ require 'jbuilder/jbuilder'
2
2
  require 'jbuilder/blank'
3
3
  require 'jbuilder/key_formatter'
4
4
  require 'jbuilder/errors'
5
- require 'multi_json'
5
+ require 'json'
6
6
  require 'ostruct'
7
7
 
8
8
  class Jbuilder
@@ -247,7 +247,7 @@ class Jbuilder
247
247
 
248
248
  # Encodes the current builder as JSON.
249
249
  def target!
250
- ::MultiJson.dump(@attributes)
250
+ ::JSON.dump(@attributes)
251
251
  end
252
252
 
253
253
  private
@@ -53,7 +53,7 @@ class JbuilderDependencyTrackerTest < ActiveSupport::TestCase
53
53
  assert_equal %w[path/to/partial], dependencies
54
54
  end
55
55
 
56
- test 'detects partial in indirect collecton calls' do
56
+ test 'detects partial in indirect collection calls' do
57
57
  dependencies = track_dependencies <<-RUBY
58
58
  json.comments @post.comments, partial: 'comments/comment', as: :comment
59
59
  RUBY
@@ -21,18 +21,26 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
21
21
  run_generator
22
22
 
23
23
  assert_file 'app/views/posts/index.json.jbuilder' do |content|
24
- assert_match %r{json.array! @posts, partial: 'posts/post', as: :post}, content
24
+ assert_match %r{json\.array! @posts, partial: "posts/post", as: :post}, content
25
25
  end
26
26
 
27
27
  assert_file 'app/views/posts/show.json.jbuilder' do |content|
28
- assert_match %r{json.partial! \"posts/post\", post: @post}, content
28
+ assert_match %r{json\.partial! "posts/post", post: @post}, content
29
29
  end
30
-
31
- assert_file 'app/views/posts/_post.json.jbuilder' do |content|
30
+
31
+ assert_file 'app/views/posts/_post.json.jbuilder' do |content|
32
32
  assert_match %r{json\.extract! post, :id, :title, :body}, content
33
+ assert_match %r{:created_at, :updated_at}, content
33
34
  assert_match %r{json\.url post_url\(post, format: :json\)}, content
34
35
  end
35
-
36
+ end
36
37
 
38
+ test 'timestamps are not generated in partial with --no-timestamps' do
39
+ run_generator %w(Post title body:text --no-timestamps)
40
+
41
+ assert_file 'app/views/posts/_post.json.jbuilder' do |content|
42
+ assert_match %r{json\.extract! post, :id, :title, :body$}, content
43
+ assert_no_match %r{:created_at, :updated_at}, content
44
+ end
37
45
  end
38
46
  end