jbuilder 1.0.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f77252f3bcb2ef3d4b51adb825d326df4804bc00
4
+ data.tar.gz: 6d2a626cfe5b9e37a18deb6f544be39e83a1c4d0
5
+ SHA512:
6
+ metadata.gz: 7890f2df618e57810e26afa21c626183b17a45067e476d3f872b3cbe60466d00fc0e32184637e8f87166417b9d573932eb2ac381d6a024e7356a9fcc7d923294
7
+ data.tar.gz: 02e87a25b940a5700ba1bed69ed670003598aeb7c312d03e5d9c16662538fed7b24debae031944b99951739844f1521095b0f3d7f2e7703085a942ae834f996c
@@ -0,0 +1,4 @@
1
+ tmp
2
+ Gemfile.lock
3
+ Gemfile.edge.lock
4
+ .ruby-version
@@ -0,0 +1,32 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ rvm:
5
+ - 1.8.7
6
+ - 1.9.2
7
+ - 1.9.3
8
+ - ruby-head
9
+ - ree
10
+ - jruby-18mode
11
+ - jruby-19mode
12
+ - rbx-18mode
13
+ - rbx-19mode
14
+ gemfile:
15
+ - Gemfile
16
+ - Gemfile.edge
17
+ matrix:
18
+ exclude:
19
+ # Edge Rails is only compatible with 1.9.3
20
+ - gemfile: Gemfile.edge
21
+ rvm: 1.8.7
22
+ - gemfile: Gemfile.edge
23
+ rvm: 1.9.2
24
+ - gemfile: Gemfile.edge
25
+ rvm: ree
26
+ - gemfile: Gemfile.edge
27
+ rvm: jruby-18mode
28
+ - gemfile: Gemfile.edge
29
+ rvm: rbx-18mode
30
+
31
+ notifications:
32
+ email: false
data/Gemfile CHANGED
@@ -1,8 +1,6 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- git 'git://github.com/rails/rails.git' do
6
- gem 'railties'
7
- gem 'actionpack'
8
- end
5
+ gem 'mocha', :require => false
6
+ gem 'actionpack', '~> 3.0'
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'mocha', require: false
6
+
7
+ git 'git://github.com/rails/rails.git' do
8
+ gem 'railties', require: 'rails/railtie'
9
+ gem 'actionpack'
10
+ end
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Jbuilder [![Build Status](https://travis-ci.org/rails/jbuilder.png)](https://travis-ci.org/rails/jbuilder)
1
+ Jbuilder [![Build Status](https://travis-ci.org/rails/jbuilder.png)](https://travis-ci.org/rails/jbuilder) [![Gem Version](https://badge.fury.io/rb/jbuilder.png)](https://rubygems.org/gems/jbuilder) [![Code Climate](https://codeclimate.com/github/rails/jbuilder.png)](https://codeclimate.com/github/rails/jbuilder)
2
2
  ========
3
3
 
4
4
  Jbuilder gives you a simple DSL for declaring JSON structures that beats massaging giant hash structures. This is particularly helpful when the generation process is fraught with conditionals and loops. Here's a simple example:
@@ -31,7 +31,7 @@ This will build the following structure:
31
31
 
32
32
  ``` javascript
33
33
  {
34
- "content": "<p>This is <i>serious</i> monkey business",
34
+ "content": "<p>This is <i>serious</i> monkey business</p>",
35
35
  "created_at": "2011-10-29T20:45:28-05:00",
36
36
  "updated_at": "2011-10-29T20:45:28-05:00",
37
37
 
@@ -55,15 +55,26 @@ This will build the following structure:
55
55
  }
56
56
  ```
57
57
 
58
+ To define attribute and structure names dynamically, use the `set!` method:
59
+
60
+ ``` ruby
61
+ json.set! :author do
62
+ json.set! :name, "David"
63
+ end
64
+
65
+ # => "author": { "name": "David" }
66
+ ```
67
+
58
68
  Top level arrays can be handled directly. Useful for index and other collection actions.
59
69
 
60
70
  ``` ruby
61
71
  # @people = People.all
62
- json.array!(@people) do |person|
72
+ json.array! @people do |person|
63
73
  json.name person.name
64
74
  json.age calculate_age(person.birthday)
65
75
  end
66
- # => [ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ]
76
+
77
+ # => [ { "name": "David", "age": 32 }, { "name": "Jamie", "age": 31 } ]
67
78
  ```
68
79
 
69
80
  Jbuilder objects can be directly nested inside each other. Useful for composing objects.
@@ -124,10 +135,10 @@ Keys can be auto formatted using `key_format!`, this can be used to convert keyn
124
135
  json.key_format! :camelize => :lower
125
136
  json.first_name "David"
126
137
 
127
- # { "firstName": "David" }
138
+ # => { "firstName": "David" }
128
139
  ```
129
140
 
130
- You can set this globaly with the class method `key_format` (from inside your enviorment.rb for example):
141
+ You can set this globaly with the class method `key_format` (from inside your environment.rb for example):
131
142
 
132
143
  ``` ruby
133
144
  Jbuilder.key_format :camelize => :lower
data/Rakefile CHANGED
@@ -4,10 +4,10 @@ require 'rake/testtask'
4
4
  Bundler.require
5
5
 
6
6
  Rake::TestTask.new do |test|
7
- if ::RUBY_VERSION < '1.9'
8
- test.test_files = %w(test/jbuilder_template_test.rb test/jbuilder_test.rb)
9
- else
7
+ if defined?(::Rails::Railtie)
10
8
  test.test_files = FileList['test/*_test.rb']
9
+ else
10
+ test.test_files = %w(test/jbuilder_template_test.rb test/jbuilder_test.rb)
11
11
  end
12
12
  end
13
13
 
@@ -1,12 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '1.0.2'
3
+ s.version = '1.2.0'
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'
7
+ s.license = 'MIT'
7
8
 
8
9
  s.add_dependency 'activesupport', '>= 3.0.0'
9
10
  s.add_development_dependency 'rake', '~> 10.0.3'
10
11
 
11
- s.files = Dir["#{File.dirname(__FILE__)}/**/*"]
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- test/*`.split("\n")
12
14
  end
@@ -11,7 +11,8 @@ module Rails
11
11
  argument :attributes, type: :array, default: [], banner: 'field:type field:type'
12
12
 
13
13
  def create_root_folder
14
- empty_directory File.join('app/views', controller_file_path)
14
+ path = File.join('app/views', controller_file_path)
15
+ empty_directory path unless File.directory?(path)
15
16
  end
16
17
 
17
18
  def copy_view_files
@@ -30,8 +31,12 @@ module Rails
30
31
  def attributes_list_with_timestamps
31
32
  attributes_list(attributes_names + %w(created_at updated_at))
32
33
  end
33
-
34
+
34
35
  def attributes_list(attributes = attributes_names)
36
+ if self.attributes.any? {|attr| attr.name == 'password' && attr.type == :digest}
37
+ attributes = attributes.reject {|name| %w(password password_confirmation).include? name}
38
+ end
39
+
35
40
  attributes.map { |a| ":#{a}"} * ', '
36
41
  end
37
42
  end
@@ -1,11 +1,12 @@
1
+ require 'rails/generators'
1
2
  require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
2
3
 
3
4
  module Rails
4
5
  module Generators
5
- class JbuilderScaffoldControllerGenerator < ScaffoldControllerGenerator
6
+ class ScaffoldControllerGenerator
6
7
  source_root File.expand_path('../templates', __FILE__)
7
8
 
8
- hook_for :json_template_engine, as: :scaffold
9
+ hook_for :jbuilder, default: true
9
10
  end
10
11
  end
11
12
  end
@@ -1,4 +1,4 @@
1
1
  json.array!(@<%= plural_table_name %>) do |<%= singular_table_name %>|
2
2
  json.extract! <%= singular_table_name %>, <%= attributes_list %>
3
3
  json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
4
- end
4
+ end
@@ -35,10 +35,10 @@ class Jbuilder < JbuilderProxy
35
35
  def format(key)
36
36
  @cache[key] ||= @format.inject(key.to_s) do |result, args|
37
37
  func, args = args
38
- if func.is_a? Proc
39
- func.call(result, *args)
38
+ if func.is_a?(::Proc)
39
+ func.call result, *args
40
40
  else
41
- result.send(func, *args)
41
+ result.send func, *args
42
42
  end
43
43
  end
44
44
  end
@@ -70,30 +70,47 @@ class Jbuilder < JbuilderProxy
70
70
  yield self if ::Kernel.block_given?
71
71
  end
72
72
 
73
- # Dynamically set a key value pair.
74
- #
75
- # Example:
76
- #
77
- # json.set!(:each, "stuff")
78
- #
79
- # { "each": "stuff" }
80
- #
81
- # You can also pass a block for nested attributes
82
- #
83
- # json.set!(:author) do
84
- # json.name "David"
85
- # json.age 32
86
- # end
87
- #
88
- # { "author": { "name": "David", "age": 32 } }
89
- def set!(key, value = nil)
90
- if ::Kernel::block_given?
91
- _set_value(key, _scope { yield self })
73
+ BLANK = ::Object.new
74
+
75
+ def set!(key, value = BLANK, *args, &block)
76
+ result = if ::Kernel.block_given?
77
+ if BLANK != value
78
+ # json.comments @post.comments { |comment| ... }
79
+ # { "comments": [ { ... }, { ... } ] }
80
+ _scope{ array! value, &block }
81
+ else
82
+ # json.comments { ... }
83
+ # { "comments": ... }
84
+ _scope { yield self }
85
+ end
86
+ elsif args.empty?
87
+ if ::Jbuilder === value
88
+ # json.age 32
89
+ # json.person another_jbuilder
90
+ # { "age": 32, "person": { ... }
91
+ value.attributes!
92
+ else
93
+ # json.age 32
94
+ # { "age": 32 }
95
+ value
96
+ end
97
+ elsif value.respond_to?(:map)
98
+ # json.comments @post.comments, :content, :created_at
99
+ # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
100
+ _map_collection(value){ |element| extract! element, *args }
92
101
  else
93
- _set_value(key, value)
102
+ # json.author @post.creator, :name, :email_address
103
+ # { "author": { "name": "David", "email_address": "david@loudthinking.com" } }
104
+ _scope { extract! value, *args }
94
105
  end
106
+
107
+ _set_value key, result
95
108
  end
96
109
 
110
+ alias_method :method_missing, :set!
111
+ private :method_missing
112
+
113
+
97
114
  # Specifies formatting to be applied to the key. Passing in a name of a function
98
115
  # will cause that function to be called on the key. So :upcase will upper case
99
116
  # the key. You can also pass in lambdas for more complex transformations.
@@ -136,12 +153,12 @@ class Jbuilder < JbuilderProxy
136
153
  # not to receive keys which have null values.
137
154
  #
138
155
  # Example:
139
- # json.ignore_nil!
156
+ # json.ignore_nil! false
140
157
  # json.id User.new.id
141
158
  #
142
159
  # { "id": null }
143
160
  #
144
- # json.ignore_nil
161
+ # json.ignore_nil!
145
162
  # json.id User.new.id
146
163
  #
147
164
  # {}
@@ -172,7 +189,7 @@ class Jbuilder < JbuilderProxy
172
189
  # json.content comment.formatted_content
173
190
  # end
174
191
  def child!
175
- @attributes = [] unless @attributes.is_a? ::Array
192
+ @attributes = [] unless @attributes.is_a?(::Array)
176
193
  @attributes << _scope { yield self }
177
194
  end
178
195
 
@@ -206,9 +223,9 @@ class Jbuilder < JbuilderProxy
206
223
  # json.array! [1, 2, 3]
207
224
  #
208
225
  # [1,2,3]
209
- def array!(collection)
226
+ def array!(collection, &block)
210
227
  @attributes = if ::Kernel::block_given?
211
- _map_collection(collection) { |element| if ::Proc.new.arity == 2 then yield self, element else yield element end }
228
+ _map_collection(collection) { |element| block.arity == 2 ? block[self, element] : block[element] }
212
229
  else
213
230
  collection
214
231
  end
@@ -233,17 +250,17 @@ class Jbuilder < JbuilderProxy
233
250
  # json.(@person, :name, :age)
234
251
  def extract!(object, *attributes)
235
252
  if object.is_a?(::Hash)
236
- attributes.each {|attribute| _set_value attribute, object.send(:fetch, attribute)}
253
+ attributes.each { |attribute| _set_value attribute, object.fetch(attribute) }
237
254
  else
238
- attributes.each {|attribute| _set_value attribute, object.send(attribute)}
255
+ attributes.each { |attribute| _set_value attribute, object.send(attribute) }
239
256
  end
240
257
  end
241
258
 
242
259
  def call(object = nil, *attributes)
243
260
  if attributes.empty?
244
- array!(object, &::Proc.new)
261
+ array! object, &::Proc.new
245
262
  else
246
- extract!(object, *attributes)
263
+ extract! object, *attributes
247
264
  end
248
265
  end
249
266
 
@@ -254,62 +271,17 @@ class Jbuilder < JbuilderProxy
254
271
 
255
272
  # Encodes the current builder as JSON.
256
273
  def target!
257
- ::MultiJson.encode @attributes
274
+ ::MultiJson.dump @attributes
258
275
  end
259
276
 
260
- protected
277
+ private
278
+
261
279
  def _set_value(key, value)
262
280
  unless @ignore_nil && value.nil?
263
281
  @attributes[@key_formatter.format(key)] = value
264
282
  end
265
283
  end
266
284
 
267
- private
268
-
269
- BLANK = ::Object.new
270
-
271
- def method_missing(method, value = BLANK, *args)
272
- result = if ::Kernel.block_given?
273
- if BLANK != value
274
- # json.comments @post.comments { |comment| ... }
275
- # { "comments": [ { ... }, { ... } ] }
276
- _map_collection(value) { |element| if ::Proc.new.arity == 2 then yield self, element else yield element end }
277
- else
278
- # json.comments { ... }
279
- # { "comments": ... }
280
- _scope { yield self }
281
- end
282
- else
283
- if args.empty?
284
- if ::Jbuilder === value
285
- # json.age 32
286
- # json.person another_jbuilder
287
- # { "age": 32, "person": { ... }
288
- value.attributes!
289
- else
290
- # json.age 32
291
- # { "age": 32 }
292
- value
293
- end
294
- else
295
- if value.respond_to?(:map)
296
- # json.comments(@post.comments, :content, :created_at)
297
- # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
298
- _map_collection(value) do |element|
299
- args.each do |attribute|
300
- _set_value attribute, element.send(attribute)
301
- end
302
- end
303
- else
304
- # json.author @post.creator, :name, :email_address
305
- # { "author": { "name": "David", "email_address": "david@loudthinking.com" } }
306
- _scope { extract! value, *args }
307
- end
308
- end
309
- end
310
- _set_value method, result
311
- end
312
-
313
285
  def _map_collection(collection)
314
286
  return [] if collection.nil?
315
287
 
@@ -329,17 +301,13 @@ class Jbuilder < JbuilderProxy
329
301
 
330
302
  def _merge(hash_or_array)
331
303
  if hash_or_array.is_a?(::Array)
332
- @attributes = [] unless @attributes.is_a? ::Array
333
- @attributes.concat(hash_or_array)
304
+ @attributes = [] unless @attributes.is_a?(::Array)
305
+ @attributes.concat hash_or_array
334
306
  else
335
- @attributes.update(hash_or_array)
307
+ @attributes.update hash_or_array
336
308
  end
337
309
  end
338
310
  end
339
311
 
340
- require 'jbuilder_template' if defined?(ActionView::Template)
341
-
342
- begin
343
- require 'railtie' if Rails::VERSION::MAJOR == 4
344
- rescue NameError
345
- end
312
+ require 'jbuilder/jbuilder_template' if defined?(ActionView::Template)
313
+ require 'jbuilder/railtie' if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR == 4
@@ -34,7 +34,12 @@ class JbuilderTemplate < Jbuilder
34
34
 
35
35
  protected
36
36
  def _cache_key(key)
37
- if @context.respond_to?(:fragment_name_with_digest)
37
+ if @context.respond_to?(:cache_fragment_name)
38
+ # Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
39
+ # should be used instead.
40
+ @context.cache_fragment_name(key)
41
+ elsif @context.respond_to?(:fragment_name_with_digest)
42
+ # Backwards compatibility for period of time when fragment_name_with_digest was made public.
38
43
  @context.fragment_name_with_digest(key)
39
44
  else
40
45
  ::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split('://').last : key, :jbuilder)
@@ -0,0 +1,11 @@
1
+ require 'rails/railtie'
2
+
3
+ class Jbuilder
4
+ class Railtie < ::Rails::Railtie
5
+ generators do |app|
6
+ Rails::Generators.configure! app.config.generators
7
+ Rails::Generators.hidden_namespaces.uniq!
8
+ require 'generators/rails/scaffold_controller_generator'
9
+ end
10
+ end
11
+ end
@@ -3,8 +3,8 @@ require 'generators/rails/jbuilder_generator'
3
3
 
4
4
  class JbuilderGeneratorTest < Rails::Generators::TestCase
5
5
  tests Rails::Generators::JbuilderGenerator
6
- arguments %w(Post title body:text)
7
- destination File.expand_path('../tmp', File.dirname(__FILE__))
6
+ arguments %w(Post title body:text password:digest)
7
+ destination File.expand_path('../tmp', __FILE__)
8
8
  setup :prepare_destination
9
9
 
10
10
  test 'views are generated' do
@@ -28,4 +28,4 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
28
28
  assert_match /json\.extract! @post, :title, :body, :created_at, :updated_at/, content
29
29
  end
30
30
  end
31
- end
31
+ end
@@ -1,121 +1,151 @@
1
1
  require 'test/unit'
2
+ require 'mocha/setup'
2
3
  require 'action_view'
3
4
  require 'action_view/testing/resolvers'
4
5
  require 'active_support/cache'
5
6
  require 'jbuilder'
6
7
 
7
8
  module Rails
8
- class Cache
9
- def initialize
10
- clear
11
- end
12
-
13
- def clear; @cache = {}; end
14
-
15
- def write(k, v, opt={})
16
- @cache[k] = v
17
- end
18
-
19
- def read(k, opt={})
20
- @cache[k]
21
- end
22
-
23
- def fetch(k, opt={}, &block)
24
- @cache[k] || @cache[k] = block.call
25
- end
9
+ def self.cache
10
+ @cache ||= ActiveSupport::Cache::MemoryStore.new
26
11
  end
27
-
28
- def self.cache; @cache ||= Cache.new; end
29
12
  end
30
13
 
31
14
  class JbuilderTemplateTest < ActionView::TestCase
15
+ setup do
16
+ @context = self
17
+ Rails.cache.clear
18
+ end
19
+
32
20
  def partials
33
- { "_partial.json.jbuilder" => 'json.content "hello"' }
21
+ { '_partial.json.jbuilder' => 'json.content "hello"' }
34
22
  end
35
23
 
36
24
  def render_jbuilder(source)
37
25
  @rendered = []
38
- lookup_context.view_paths = [ActionView::FixtureResolver.new(partials.merge("test.json.jbuilder" => source))]
39
- ActionView::Template.new(source, "test", JbuilderHandler, :virtual_path => "test").render(self, {}).strip
26
+ lookup_context.view_paths = [ActionView::FixtureResolver.new(partials.merge('test.json.jbuilder' => source))]
27
+ ActionView::Template.new(source, 'test', JbuilderHandler, :virtual_path => 'test').render(self, {}).strip
28
+ end
29
+
30
+ def undef_context_methods(*names)
31
+ self.class_eval do
32
+ names.each do |name|
33
+ undef_method name.to_sym if self.method_defined?(name.to_sym)
34
+ end
35
+ end
40
36
  end
41
37
 
42
- test "rendering" do
38
+ test 'rendering' do
43
39
  json = render_jbuilder <<-JBUILDER
44
- json.content "hello"
40
+ json.content 'hello'
45
41
  JBUILDER
46
42
 
47
- assert_equal "hello", MultiJson.load(json)["content"]
43
+ assert_equal 'hello', MultiJson.load(json)['content']
48
44
  end
49
45
 
50
- test "key_format! with parameter" do
46
+ test 'key_format! with parameter' do
51
47
  json = render_jbuilder <<-JBUILDER
52
48
  json.key_format! :camelize => [:lower]
53
- json.camel_style "for JS"
49
+ json.camel_style 'for JS'
54
50
  JBUILDER
55
51
 
56
52
  assert_equal ['camelStyle'], MultiJson.load(json).keys
57
53
  end
58
54
 
59
- test "key_format! propagates to child elements" do
55
+ test 'key_format! propagates to child elements' do
60
56
  json = render_jbuilder <<-JBUILDER
61
57
  json.key_format! :upcase
62
- json.level1 "one"
58
+ json.level1 'one'
63
59
  json.level2 do
64
- json.value "two"
60
+ json.value 'two'
65
61
  end
66
62
  JBUILDER
67
63
 
68
64
  result = MultiJson.load(json)
69
- assert_equal "one", result["LEVEL1"]
70
- assert_equal "two", result["LEVEL2"]["VALUE"]
65
+ assert_equal 'one', result['LEVEL1']
66
+ assert_equal 'two', result['LEVEL2']['VALUE']
71
67
  end
72
68
 
73
- test "partial! renders partial" do
69
+ test 'partial! renders partial' do
74
70
  json = render_jbuilder <<-JBUILDER
75
71
  json.partial! 'partial'
76
72
  JBUILDER
77
73
 
78
- assert_equal "hello", MultiJson.load(json)["content"]
74
+ assert_equal 'hello', MultiJson.load(json)['content']
79
75
  end
80
76
 
81
- test "fragment caching a JSON object" do
82
- self.controller.perform_caching = true
83
- Rails.cache.clear
77
+ test 'fragment caching a JSON object' do
78
+ undef_context_methods :fragment_name_with_digest, :cache_fragment_name
79
+
84
80
  render_jbuilder <<-JBUILDER
85
- json.cache!("cachekey") do
86
- json.name "Cache"
81
+ json.cache! 'cachekey' do
82
+ json.name 'Cache'
87
83
  end
88
84
  JBUILDER
89
85
 
90
86
  json = render_jbuilder <<-JBUILDER
91
- json.cache!("cachekey") do
92
- json.name "Miss"
87
+ json.cache! 'cachekey' do
88
+ json.name 'Miss'
93
89
  end
94
90
  JBUILDER
95
91
 
96
- MultiJson.load(json).tap do |parsed|
97
- assert_equal "Cache", parsed['name']
98
- end
92
+ parsed = MultiJson.load(json)
93
+ assert_equal 'Cache', parsed['name']
99
94
  end
100
95
 
101
- test "fragment caching deserializes an array" do
102
- Rails.cache.clear
103
- self.controller.perform_caching = true
96
+ test 'fragment caching deserializes an array' do
97
+ undef_context_methods :fragment_name_with_digest, :cache_fragment_name
98
+
104
99
  render_jbuilder <<-JBUILDER
105
- json.cache!("cachekey") do
106
- json.array! ['a', 'b', 'c']
100
+ json.cache! 'cachekey' do
101
+ json.array! %w(a b c)
107
102
  end
108
103
  JBUILDER
109
104
 
110
105
  json = render_jbuilder <<-JBUILDER
111
- json.cache!("cachekey") do
112
- json.array! ['1', '2', '3']
106
+ json.cache! 'cachekey' do
107
+ json.array! %w(1 2 3)
113
108
  end
114
109
  JBUILDER
115
110
 
116
- MultiJson.load(json).tap do |parsed|
117
- assert_equal ["a", "b", "c"], parsed
118
- end
111
+ parsed = MultiJson.load(json)
112
+ assert_equal %w(a b c), parsed
119
113
  end
120
114
 
121
- end
115
+ test 'fragment caching works with previous version of cache digests' do
116
+ undef_context_methods :cache_fragment_name
117
+
118
+ @context.expects :fragment_name_with_digest
119
+
120
+ render_jbuilder <<-JBUILDER
121
+ json.cache! 'cachekey' do
122
+ json.name 'Cache'
123
+ end
124
+ JBUILDER
125
+ end
126
+
127
+ test 'fragment caching works with current cache digests' do
128
+ undef_context_methods :fragment_name_with_digest
129
+
130
+ @context.expects :cache_fragment_name
131
+
132
+ render_jbuilder <<-JBUILDER
133
+ json.cache! 'cachekey' do
134
+ json.name 'Cache'
135
+ end
136
+ JBUILDER
137
+ end
138
+
139
+ test 'fragment caching falls back on ActiveSupport::Cache.expand_cache_key' do
140
+ undef_context_methods :fragment_name_with_digest, :cache_fragment_name
141
+
142
+ ActiveSupport::Cache.expects :expand_cache_key
143
+
144
+ render_jbuilder <<-JBUILDER
145
+ json.cache! 'cachekey' do
146
+ json.name 'Cache'
147
+ end
148
+ JBUILDER
149
+ end
150
+
151
+ end
@@ -1,9 +1,10 @@
1
1
  require 'test/unit'
2
2
  require 'active_support/test_case'
3
3
  require 'active_support/inflector'
4
-
5
4
  require 'jbuilder'
6
5
 
6
+ Comment = Struct.new(:content, :id)
7
+
7
8
  class JbuilderProxy
8
9
  # Faking Object#instance_eval for 1.8
9
10
  def instance_eval(code)
@@ -53,10 +54,9 @@ class JbuilderTest < ActiveSupport::TestCase
53
54
  json.content 'world'
54
55
  end
55
56
 
56
- MultiJson.load(json).tap do |parsed|
57
- assert_equal 'hello', parsed['title']
58
- assert_equal 'world', parsed['content']
59
- end
57
+ parsed = MultiJson.load(json)
58
+ assert_equal 'hello', parsed['title']
59
+ assert_equal 'world', parsed['content']
60
60
  end
61
61
 
62
62
  test 'extracting from object' do
@@ -66,10 +66,9 @@ class JbuilderTest < ActiveSupport::TestCase
66
66
  json.extract! person, :name, :age
67
67
  end
68
68
 
69
- MultiJson.load(json).tap do |parsed|
70
- assert_equal 'David', parsed['name']
71
- assert_equal 32, parsed['age']
72
- end
69
+ parsed = MultiJson.load(json)
70
+ assert_equal 'David', parsed['name']
71
+ assert_equal 32, parsed['age']
73
72
  end
74
73
 
75
74
  test 'extracting from object using call style for 1.9' do
@@ -83,10 +82,9 @@ class JbuilderTest < ActiveSupport::TestCase
83
82
  end
84
83
  end
85
84
 
86
- MultiJson.load(json).tap do |parsed|
87
- assert_equal 'David', parsed['name']
88
- assert_equal 32, parsed['age']
89
- end
85
+ parsed = MultiJson.load(json)
86
+ assert_equal 'David', parsed['name']
87
+ assert_equal 32, parsed['age']
90
88
  end
91
89
 
92
90
  test 'extracting from hash' do
@@ -96,10 +94,9 @@ class JbuilderTest < ActiveSupport::TestCase
96
94
  json.extract! person, :name, :age
97
95
  end
98
96
 
99
- MultiJson.load(json).tap do |parsed|
100
- assert_equal 'Jim', parsed['name']
101
- assert_equal 34, parsed['age']
102
- end
97
+ parsed = MultiJson.load(json)
98
+ assert_equal 'Jim', parsed['name']
99
+ assert_equal 34, parsed['age']
103
100
  end
104
101
 
105
102
  test 'nesting single child with block' do
@@ -110,10 +107,9 @@ class JbuilderTest < ActiveSupport::TestCase
110
107
  end
111
108
  end
112
109
 
113
- MultiJson.load(json).tap do |parsed|
114
- assert_equal 'David', parsed['author']['name']
115
- assert_equal 32, parsed['author']['age']
116
- end
110
+ parsed = MultiJson.load(json)
111
+ assert_equal 'David', parsed['author']['name']
112
+ assert_equal 32, parsed['author']['age']
117
113
  end
118
114
 
119
115
  test 'nesting multiple children with block' do
@@ -124,10 +120,9 @@ class JbuilderTest < ActiveSupport::TestCase
124
120
  end
125
121
  end
126
122
 
127
- MultiJson.load(json).tap do |parsed|
128
- assert_equal 'hello', parsed['comments'].first['content']
129
- assert_equal 'world', parsed['comments'].second['content']
130
- end
123
+ parsed = MultiJson.load(json)
124
+ assert_equal 'hello', parsed['comments'].first['content']
125
+ assert_equal 'world', parsed['comments'].second['content']
131
126
  end
132
127
 
133
128
  test 'nesting single child with inline extract' do
@@ -143,24 +138,22 @@ class JbuilderTest < ActiveSupport::TestCase
143
138
  json.author person, :name, :age
144
139
  end
145
140
 
146
- MultiJson.load(json).tap do |parsed|
147
- assert_equal 'David', parsed['author']['name']
148
- assert_equal 32, parsed['author']['age']
149
- end
141
+ parsed = MultiJson.load(json)
142
+ assert_equal 'David', parsed['author']['name']
143
+ assert_equal 32, parsed['author']['age']
150
144
  end
151
145
 
152
146
  test 'nesting multiple children from array' do
153
- comments = [ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ]
147
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
154
148
 
155
149
  json = Jbuilder.encode do |json|
156
150
  json.comments comments, :content
157
151
  end
158
152
 
159
- MultiJson.load(json).tap do |parsed|
160
- assert_equal ['content'], parsed['comments'].first.keys
161
- assert_equal 'hello', parsed['comments'].first['content']
162
- assert_equal 'world', parsed['comments'].second['content']
163
- end
153
+ parsed = MultiJson.load(json)
154
+ assert_equal ['content'], parsed['comments'].first.keys
155
+ assert_equal 'hello', parsed['comments'].first['content']
156
+ assert_equal 'world', parsed['comments'].second['content']
164
157
  end
165
158
 
166
159
  test 'nesting multiple children from array when child array is empty' do
@@ -171,14 +164,13 @@ class JbuilderTest < ActiveSupport::TestCase
171
164
  json.comments comments, :content
172
165
  end
173
166
 
174
- MultiJson.load(json).tap do |parsed|
175
- assert_equal 'Parent', parsed['name']
176
- assert_equal [], parsed['comments']
177
- end
167
+ parsed = MultiJson.load(json)
168
+ assert_equal 'Parent', parsed['name']
169
+ assert_equal [], parsed['comments']
178
170
  end
179
171
 
180
172
  test 'nesting multiple children from array with inline loop' do
181
- comments = [ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ]
173
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
182
174
 
183
175
  json = Jbuilder.encode do |json|
184
176
  json.comments comments do |comment|
@@ -186,11 +178,10 @@ class JbuilderTest < ActiveSupport::TestCase
186
178
  end
187
179
  end
188
180
 
189
- MultiJson.load(json).tap do |parsed|
190
- assert_equal ['content'], parsed['comments'].first.keys
191
- assert_equal 'hello', parsed['comments'].first['content']
192
- assert_equal 'world', parsed['comments'].second['content']
193
- end
181
+ parsed = MultiJson.load(json)
182
+ assert_equal ['content'], parsed['comments'].first.keys
183
+ assert_equal 'hello', parsed['comments'].first['content']
184
+ assert_equal 'world', parsed['comments'].second['content']
194
185
  end
195
186
 
196
187
  test 'handles nil-collections as empty arrays' do
@@ -204,21 +195,20 @@ class JbuilderTest < ActiveSupport::TestCase
204
195
  end
205
196
 
206
197
  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) ])
198
+ comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
208
199
 
209
200
  json = Jbuilder.encode do |json|
210
201
  json.comments comments, :content
211
202
  end
212
203
 
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
204
+ parsed = MultiJson.load(json)
205
+ assert_equal ['content'], parsed['comments'].first.keys
206
+ assert_equal 'hello', parsed['comments'].first['content']
207
+ assert_equal 'world', parsed['comments'].second['content']
218
208
  end
219
209
 
220
210
  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) ])
211
+ comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
222
212
 
223
213
  json = Jbuilder.encode do |json|
224
214
  json.comments comments do |comment|
@@ -226,15 +216,14 @@ class JbuilderTest < ActiveSupport::TestCase
226
216
  end
227
217
  end
228
218
 
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
219
+ parsed = MultiJson.load(json)
220
+ assert_equal ['content'], parsed['comments'].first.keys
221
+ assert_equal 'hello', parsed['comments'].first['content']
222
+ assert_equal 'world', parsed['comments'].second['content']
234
223
  end
235
224
 
236
225
  test 'nesting multiple children from array with inline loop with old api' do
237
- comments = [ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ]
226
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
238
227
 
239
228
  json = Jbuilder.encode do |json|
240
229
  json.comments comments do |json, comment|
@@ -242,15 +231,14 @@ class JbuilderTest < ActiveSupport::TestCase
242
231
  end
243
232
  end
244
233
 
245
- MultiJson.load(json).tap do |parsed|
246
- assert_equal ['content'], parsed['comments'].first.keys
247
- assert_equal 'hello', parsed['comments'].first['content']
248
- assert_equal 'world', parsed['comments'].second['content']
249
- end
234
+ parsed = MultiJson.load(json)
235
+ assert_equal ['content'], parsed['comments'].first.keys
236
+ assert_equal 'hello', parsed['comments'].first['content']
237
+ assert_equal 'world', parsed['comments'].second['content']
250
238
  end
251
239
 
252
240
  test 'nesting multiple children from array with inline loop on root' do
253
- comments = [ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ]
241
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
254
242
 
255
243
  json = Jbuilder.encode do |json|
256
244
  json.call(comments) do |comment|
@@ -258,14 +246,13 @@ class JbuilderTest < ActiveSupport::TestCase
258
246
  end
259
247
  end
260
248
 
261
- MultiJson.load(json).tap do |parsed|
262
- assert_equal 'hello', parsed.first['content']
263
- assert_equal 'world', parsed.second['content']
264
- end
249
+ parsed = MultiJson.load(json)
250
+ assert_equal 'hello', parsed.first['content']
251
+ assert_equal 'world', parsed.second['content']
265
252
  end
266
253
 
267
254
  test 'nesting multiple children from array with inline loop on root with old api' do
268
- comments = [ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ]
255
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
269
256
 
270
257
  json = Jbuilder.encode do |json|
271
258
  json.call(comments) do |json, comment|
@@ -273,10 +260,9 @@ class JbuilderTest < ActiveSupport::TestCase
273
260
  end
274
261
  end
275
262
 
276
- MultiJson.load(json).tap do |parsed|
277
- assert_equal 'hello', parsed.first['content']
278
- assert_equal 'world', parsed.second['content']
279
- end
263
+ parsed = MultiJson.load(json)
264
+ assert_equal 'hello', parsed.first['content']
265
+ assert_equal 'world', parsed.second['content']
280
266
  end
281
267
 
282
268
  test 'array nested inside nested hash' do
@@ -292,10 +278,9 @@ class JbuilderTest < ActiveSupport::TestCase
292
278
  end
293
279
  end
294
280
 
295
- MultiJson.load(json).tap do |parsed|
296
- assert_equal 'hello', parsed['author']['comments'].first['content']
297
- assert_equal 'world', parsed['author']['comments'].second['content']
298
- end
281
+ parsed = MultiJson.load(json)
282
+ assert_equal 'hello', parsed['author']['comments'].first['content']
283
+ assert_equal 'world', parsed['author']['comments'].second['content']
299
284
  end
300
285
 
301
286
  test 'array nested inside array' do
@@ -351,13 +336,25 @@ class JbuilderTest < ActiveSupport::TestCase
351
336
  json.value 'Test'
352
337
  json.nested to_nest
353
338
  end
354
- parsed = MultiJson.load(json)
355
- assert_equal 'Test', parsed['value']
356
- assert_equal 'Nested Test', parsed['nested']['nested_value']
339
+
340
+ result = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
341
+ assert_equal result, MultiJson.load(json)
342
+ end
343
+
344
+ test 'nested jbuilder object via set!' do
345
+ to_nest = Jbuilder.new
346
+ to_nest.nested_value 'Nested Test'
347
+ json = Jbuilder.encode do |json|
348
+ json.value 'Test'
349
+ json.set! :nested, to_nest
350
+ end
351
+
352
+ result = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
353
+ assert_equal result, MultiJson.load(json)
357
354
  end
358
355
 
359
356
  test 'top-level array' do
360
- comments = [ Struct.new(:content, :id).new('hello', 1), Struct.new(:content, :id).new('world', 2) ]
357
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
361
358
 
362
359
  json = Jbuilder.encode do |json|
363
360
  json.array!(comments) do |comment|
@@ -365,10 +362,9 @@ class JbuilderTest < ActiveSupport::TestCase
365
362
  end
366
363
  end
367
364
 
368
- MultiJson.load(json).tap do |parsed|
369
- assert_equal 'hello', parsed.first['content']
370
- assert_equal 'world', parsed.second['content']
371
- end
365
+ parsed = MultiJson.load(json)
366
+ assert_equal 'hello', parsed.first['content']
367
+ assert_equal 'world', parsed.second['content']
372
368
  end
373
369
 
374
370
  test 'empty top-level array' do
@@ -385,7 +381,7 @@ class JbuilderTest < ActiveSupport::TestCase
385
381
 
386
382
  test 'dynamically set a key/value' do
387
383
  json = Jbuilder.encode do |json|
388
- json.set!(:each, 'stuff')
384
+ json.set! :each, 'stuff'
389
385
  end
390
386
 
391
387
  assert_equal 'stuff', MultiJson.load(json)['each']
@@ -399,10 +395,22 @@ class JbuilderTest < ActiveSupport::TestCase
399
395
  end
400
396
  end
401
397
 
402
- MultiJson.load(json).tap do |parsed|
403
- assert_equal 'David', parsed['author']['name']
404
- assert_equal 32, parsed['author']['age']
398
+ parsed = MultiJson.load(json)
399
+ assert_equal 'David', parsed['author']['name']
400
+ assert_equal 32, parsed['author']['age']
401
+ end
402
+
403
+ test 'dynamically sets a collection' do
404
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
405
+
406
+ json = Jbuilder.encode do |json|
407
+ json.set! :comments, comments, :content
405
408
  end
409
+
410
+ parsed = MultiJson.load(json)
411
+ assert_equal ['content'], parsed['comments'].first.keys
412
+ assert_equal 'hello', parsed['comments'].first['content']
413
+ assert_equal 'world', parsed['comments'].second['content']
406
414
  end
407
415
 
408
416
  test 'query like object' do
@@ -1,10 +1,10 @@
1
1
  require 'rails/generators/test_case'
2
- require 'generators/rails/jbuilder_scaffold_controller_generator'
2
+ require 'generators/rails/scaffold_controller_generator'
3
3
 
4
- class JbuilderScaffoldControllerGeneratorTest < Rails::Generators::TestCase
5
- tests Rails::Generators::JbuilderScaffoldControllerGenerator
4
+ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
5
+ tests Rails::Generators::ScaffoldControllerGenerator
6
6
  arguments %w(Post title body:text)
7
- destination File.expand_path('../tmp', File.dirname(__FILE__))
7
+ destination File.expand_path('../tmp', __FILE__)
8
8
  setup :prepare_destination
9
9
 
10
10
  test 'controller content' do
metadata CHANGED
@@ -1,36 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
5
- prerelease:
4
+ version: 1.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - David Heinemeier Hansson
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-11 00:00:00.000000000 Z
11
+ date: 2013-03-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.0.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -49,49 +44,52 @@ executables: []
49
44
  extensions: []
50
45
  extra_rdoc_files: []
51
46
  files:
52
- - ./Gemfile
53
- - ./Gemfile-1.8
54
- - ./jbuilder-0.9.0.gem
55
- - ./jbuilder-1.0.0.gem
56
- - ./jbuilder-1.0.1.gem
57
- - ./jbuilder.gemspec
58
- - ./lib/generators/rails/jbuilder_generator.rb
59
- - ./lib/generators/rails/jbuilder_scaffold_controller_generator.rb
60
- - ./lib/generators/rails/templates/controller.rb
61
- - ./lib/generators/rails/templates/index.json.jbuilder
62
- - ./lib/generators/rails/templates/show.json.jbuilder
63
- - ./lib/jbuilder.rb
64
- - ./lib/jbuilder_template.rb
65
- - ./lib/railtie.rb
66
- - ./MIT-LICENSE
67
- - ./Rakefile
68
- - ./README.md
69
- - ./test/jbuilder_generator_test.rb
70
- - ./test/jbuilder_template_test.rb
71
- - ./test/jbuilder_test.rb
72
- - ./test/scaffold_controller_generator_test.rb
47
+ - .gitignore
48
+ - .travis.yml
49
+ - Gemfile
50
+ - Gemfile.edge
51
+ - MIT-LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - jbuilder.gemspec
55
+ - lib/generators/rails/jbuilder_generator.rb
56
+ - lib/generators/rails/scaffold_controller_generator.rb
57
+ - lib/generators/rails/templates/controller.rb
58
+ - lib/generators/rails/templates/index.json.jbuilder
59
+ - lib/generators/rails/templates/show.json.jbuilder
60
+ - lib/jbuilder.rb
61
+ - lib/jbuilder/jbuilder_template.rb
62
+ - lib/jbuilder/railtie.rb
63
+ - test/jbuilder_generator_test.rb
64
+ - test/jbuilder_template_test.rb
65
+ - test/jbuilder_test.rb
66
+ - test/scaffold_controller_generator_test.rb
73
67
  homepage:
74
- licenses: []
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
75
71
  post_install_message:
76
72
  rdoc_options: []
77
73
  require_paths:
78
74
  - lib
79
75
  required_ruby_version: !ruby/object:Gem::Requirement
80
- none: false
81
76
  requirements:
82
- - - ! '>='
77
+ - - '>='
83
78
  - !ruby/object:Gem::Version
84
79
  version: '0'
85
80
  required_rubygems_version: !ruby/object:Gem::Requirement
86
- none: false
87
81
  requirements:
88
- - - ! '>='
82
+ - - '>='
89
83
  - !ruby/object:Gem::Version
90
84
  version: '0'
91
85
  requirements: []
92
86
  rubyforge_project:
93
- rubygems_version: 1.8.23
87
+ rubygems_version: 2.0.0
94
88
  signing_key:
95
- specification_version: 3
89
+ specification_version: 4
96
90
  summary: Create JSON structures via a Builder-style DSL
97
- test_files: []
91
+ test_files:
92
+ - test/jbuilder_generator_test.rb
93
+ - test/jbuilder_template_test.rb
94
+ - test/jbuilder_test.rb
95
+ - test/scaffold_controller_generator_test.rb
@@ -1,5 +0,0 @@
1
- source :rubygems
2
-
3
- gemspec
4
-
5
- gem 'actionpack', '~> 3.0'
Binary file
Binary file
Binary file
@@ -1,8 +0,0 @@
1
- require 'rails/engine'
2
-
3
- class Jbuilder
4
- class Engine < ::Rails::Engine
5
- config.app_generators.scaffold_controller = :jbuilder_scaffold_controller
6
- config.app_generators.json_template_engine = :jbuilder
7
- end
8
- end