jbuilder 2.1.3 → 2.2.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
2
  SHA1:
3
- metadata.gz: 8d5b664cede402d0f7c028a234e22a7bf1114274
4
- data.tar.gz: d4e163e75dea9c02ecb14ce79f7dafda0b5f32f3
3
+ metadata.gz: 141e8a9ebdb989259856973050722aae336d7b50
4
+ data.tar.gz: 265721f5266ccb8e444a291f04193ac562076abf
5
5
  SHA512:
6
- metadata.gz: 88378366476f8fbaf94eecdc4e31be5bf000d605c9797c4c0a9b53ae3be63dc0f5c119e5cd349f42d1fa9459b69de9730d5dad2a2d7fd89e38cefe3808884cf7
7
- data.tar.gz: 13134bb6ea6ff1df39592cfef8a3575f94e185864ebf629af38b8cd19911bdb9f6804eeaf87d5d2a256c1d482cec49cf93aa7b77094f2124a798f7fa959af048
6
+ metadata.gz: dd8f8b1bfdfff3270bd1abaee9e1248aa202779804997237633ac9fc5bbb08e33e4b9a1910e633bfeab12b3dbef0e5c21084dddf7f47d6030a8b1d52c44f7317
7
+ data.tar.gz: 299d5d6f3a75b3bba2f9c666031655ae0813273ea38584d3f410353dae8912dc4d2767e3f0e8092ed34470ff6464a1061e80396a9a4befd79b57ac7cdb308d0b
data/.gitignore CHANGED
@@ -2,3 +2,4 @@ tmp
2
2
  gemfiles/*.lock
3
3
  Gemfile.lock
4
4
  .ruby-version
5
+ pkg
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ 2.2.0
4
+ -----
5
+ * [Allow to skip `array!` iterations by calling `next`](https://github.com/rails/jbuilder/commit/81a63308fb9d5002519dd871f829ccc58067251a)
6
+
3
7
  2.1.2
4
8
  -----
5
9
  * [Cast array-like objects to array before merging](https://github.com/rails/jbuilder/commit/7b8c8a1cb09b7f3dd26e5643ebbd6b2ec67185db)
data/README.md CHANGED
@@ -16,26 +16,26 @@ generation process is fraught with conditionals and loops. Here's a simple
16
16
  example:
17
17
 
18
18
  ``` ruby
19
- Jbuilder.encode do |json|
20
- json.content format_content(@message.content)
21
- json.(@message, :created_at, :updated_at)
19
+ # app/views/message/show.json.jbuilder
22
20
 
23
- json.author do
24
- json.name @message.creator.name.familiar
25
- json.email_address @message.creator.email_address_with_name
26
- json.url url_for(@message.creator, format: :json)
27
- end
21
+ json.content format_content(@message.content)
22
+ json.(@message, :created_at, :updated_at)
28
23
 
29
- if current_user.admin?
30
- json.visitors calculate_visitors(@message)
31
- end
24
+ json.author do
25
+ json.name @message.creator.name.familiar
26
+ json.email_address @message.creator.email_address_with_name
27
+ json.url url_for(@message.creator, format: :json)
28
+ end
32
29
 
33
- json.comments @message.comments, :content, :created_at
30
+ if current_user.admin?
31
+ json.visitors calculate_visitors(@message)
32
+ end
34
33
 
35
- json.attachments @message.attachments do |attachment|
36
- json.filename attachment.filename
37
- json.url url_for(attachment)
38
- end
34
+ json.comments @message.comments, :content, :created_at
35
+
36
+ json.attachments @message.attachments do |attachment|
37
+ json.filename attachment.filename
38
+ json.url url_for(attachment)
39
39
  end
40
40
  ```
41
41
 
@@ -80,19 +80,26 @@ end
80
80
  Top level arrays can be handled directly. Useful for index and other collection actions.
81
81
 
82
82
  ``` ruby
83
- # @people = People.all
84
- json.array! @people do |person|
85
- json.name person.name
86
- json.age calculate_age(person.birthday)
83
+ # @comments = @post.comments
84
+
85
+ json.array! @comments do |comment|
86
+ next if comment.marked_as_spam_by?(current_user)
87
+
88
+ json.body comment.body
89
+ json.author do
90
+ json.first_name comment.author.first_name
91
+ json.last_name comment.author.last_name
92
+ end
87
93
  end
88
94
 
89
- # => [ { "name": "David", "age": 32 }, { "name": "Jamie", "age": 31 } ]
95
+ # => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
90
96
  ```
91
97
 
92
98
  You can also extract attributes from array directly.
93
99
 
94
100
  ``` ruby
95
101
  # @people = People.all
102
+
96
103
  json.array! @people, :id, :name
97
104
 
98
105
  # => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '2.1.3'
3
+ s.version = '2.2.0'
4
4
  s.authors = ['David Heinemeier Hansson', 'Pavel Pravosud']
5
5
  s.email = ['david@37signals.com', 'pavel@pravosud.com']
6
6
  s.summary = 'Create JSON structures via a Builder-style DSL'
@@ -260,10 +260,11 @@ class Jbuilder
260
260
  end
261
261
 
262
262
  def _read(key, default = nil)
263
- @attributes.fetch(_key(key)){ default }
263
+ _blank? ? default : @attributes.fetch(_key(key)){ default }
264
264
  end
265
265
 
266
266
  def _write(key, value)
267
+ @attributes = {} if _blank?
267
268
  @attributes[_key(key)] = value
268
269
  end
269
270
 
@@ -280,12 +281,12 @@ class Jbuilder
280
281
  def _map_collection(collection)
281
282
  collection.map do |element|
282
283
  _scope{ yield element }
283
- end
284
+ end - [BLANK]
284
285
  end
285
286
 
286
287
  def _scope
287
288
  parent_attributes, parent_formatter = @attributes, @key_formatter
288
- @attributes = {}
289
+ @attributes = BLANK
289
290
  yield
290
291
  @attributes
291
292
  ensure
@@ -308,6 +309,10 @@ class Jbuilder
308
309
 
309
310
  attributes
310
311
  end
312
+
313
+ def _blank?
314
+ BLANK == @attributes
315
+ end
311
316
  end
312
317
 
313
318
  require 'jbuilder/jbuilder_template' if defined?(ActionView::Template)
@@ -373,6 +373,20 @@ class JbuilderTest < ActiveSupport::TestCase
373
373
  assert_equal 'world', result.second['content']
374
374
  end
375
375
 
376
+ test 'it allows using next in array block to skip value' do
377
+ comments = [ Comment.new('hello', 1), Comment.new('skip', 2), Comment.new('world', 3) ]
378
+ result = jbuild do |json|
379
+ json.array! comments do |comment|
380
+ next if comment.id == 2
381
+ json.content comment.content
382
+ end
383
+ end
384
+
385
+ assert_equal 2, result.length
386
+ assert_equal 'hello', result.first['content']
387
+ assert_equal 'world', result.second['content']
388
+ end
389
+
376
390
  test 'extract attributes directly from array' do
377
391
  comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
378
392
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-16 00:00:00.000000000 Z
12
+ date: 2014-10-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  requirements: []
107
107
  rubyforge_project:
108
- rubygems_version: 2.2.2
108
+ rubygems_version: 2.4.1
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: Create JSON structures via a Builder-style DSL