jbuilder 1.0.1 → 1.0.2

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.
Binary file
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '1.0.1'
3
+ s.version = '1.0.2'
4
4
  s.author = 'David Heinemeier Hansson'
5
5
  s.email = 'david@37signals.com'
6
6
  s.summary = 'Create JSON structures via a Builder-style DSL'
@@ -292,7 +292,7 @@ class Jbuilder < JbuilderProxy
292
292
  value
293
293
  end
294
294
  else
295
- if value.kind_of?(::Enumerable)
295
+ if value.respond_to?(:map)
296
296
  # json.comments(@post.comments, :content, :created_at)
297
297
  # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
298
298
  _map_collection(value) do |element|
@@ -342,4 +342,4 @@ require 'jbuilder_template' if defined?(ActionView::Template)
342
342
  begin
343
343
  require 'railtie' if Rails::VERSION::MAJOR == 4
344
344
  rescue NameError
345
- end
345
+ end
@@ -20,13 +20,12 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
20
20
 
21
21
  assert_file 'app/views/posts/index.json.jbuilder' do |content|
22
22
  assert_match /json\.array!\(@posts\) do \|post\|/, content
23
- assert_match /json\.extract! post, :title, :body, :created_at, :updated_at/, content
24
- assert_match /json\.url post_url\(post\)/, content
23
+ assert_match /json\.extract! post, :title, :body/, content
24
+ assert_match /json\.url post_url\(post, format: :json\)/, content
25
25
  end
26
26
 
27
27
  assert_file 'app/views/posts/show.json.jbuilder' do |content|
28
28
  assert_match /json\.extract! @post, :title, :body, :created_at, :updated_at/, content
29
- assert_match /json\.url post_url\(@post\)/, content
30
29
  end
31
30
  end
32
31
  end
@@ -11,6 +11,16 @@ class JbuilderProxy
11
11
  end
12
12
  end if ::RUBY_VERSION < '1.9'
13
13
 
14
+ class NonEnumerable
15
+ def initialize(collection)
16
+ @collection = collection
17
+ end
18
+
19
+ def map(&block)
20
+ @collection.map(&block)
21
+ end
22
+ end
23
+
14
24
  class JbuilderTest < ActiveSupport::TestCase
15
25
  test 'single key' do
16
26
  json = Jbuilder.encode do |json|
@@ -193,6 +203,36 @@ class JbuilderTest < ActiveSupport::TestCase
193
203
  assert_equal [], MultiJson.load(json)['comments']
194
204
  end
195
205
 
206
+ test 'nesting multiple children from a non-Enumerable that responds to #map' do
207
+ comments = NonEnumerable.new([ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ])
208
+
209
+ json = Jbuilder.encode do |json|
210
+ json.comments comments, :content
211
+ end
212
+
213
+ MultiJson.load(json).tap do |parsed|
214
+ assert_equal ['content'], parsed['comments'].first.keys
215
+ assert_equal 'hello', parsed['comments'].first['content']
216
+ assert_equal 'world', parsed['comments'].second['content']
217
+ end
218
+ end
219
+
220
+ test 'nesting multiple chilren from a non-Enumerable that responds to #map with inline loop' do
221
+ comments = NonEnumerable.new([ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ])
222
+
223
+ json = Jbuilder.encode do |json|
224
+ json.comments comments do |comment|
225
+ json.content comment.content
226
+ end
227
+ end
228
+
229
+ MultiJson.load(json).tap do |parsed|
230
+ assert_equal ['content'], parsed['comments'].first.keys
231
+ assert_equal 'hello', parsed['comments'].first['content']
232
+ assert_equal 'world', parsed['comments'].second['content']
233
+ end
234
+ end
235
+
196
236
  test 'nesting multiple children from array with inline loop with old api' do
197
237
  comments = [ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ]
198
238
 
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: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-10 00:00:00.000000000 Z
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -53,6 +53,7 @@ files:
53
53
  - ./Gemfile-1.8
54
54
  - ./jbuilder-0.9.0.gem
55
55
  - ./jbuilder-1.0.0.gem
56
+ - ./jbuilder-1.0.1.gem
56
57
  - ./jbuilder.gemspec
57
58
  - ./lib/generators/rails/jbuilder_generator.rb
58
59
  - ./lib/generators/rails/jbuilder_scaffold_controller_generator.rb