README.md in jbuilder-1.3.0 vs README.md in jbuilder-1.4.0

- old
+ new

@@ -57,11 +57,11 @@ To define attribute and structure names dynamically, use the `set!` method: ``` ruby json.set! :author do - json.set! :name, "David" + json.set! :name, 'David' end # => "author": { "name": "David" } ``` @@ -75,10 +75,20 @@ end # => [ { "name": "David", "age": 32 }, { "name": "Jamie", "age": 31 } ] ``` +You can also extract attributes from array directly. + +``` ruby +# @people = People.all +json.array! @people, :id, :name + +# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] +``` + + Jbuilder objects can be directly nested inside each other. Useful for composing objects. ``` ruby class Person # ... Class Definition ... # @@ -97,11 +107,11 @@ company.president president.to_builder end end end -company = Company.new("Doodle Corp", Person.new("John Stobs", 58)) +company = Company.new('Doodle Corp', Person.new('John Stobs', 58)) company.to_builder.target! # => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}} ``` @@ -124,17 +134,31 @@ # You can use partials as well. The following line will render the file # RAILS_ROOT/app/views/api/comments/_comments, and set a local variable # 'comments' with all this message's comments, which you can use inside # the partial. -json.partial! "api/comments/comments", comments: @message.comments +json.partial! 'api/comments/comments', comments: @message.comments ``` +You can explicitly make Jbuilder object return null if you want: + +``` ruby +json.extract! @post, :id, :title, :content, :published_at +json.author do + if @post.anonymous? + json.null! # or json.nil! + else + json.first_name @post.author_first_name + json.last_name @post.author_last_name + end +end +``` + Keys can be auto formatted using `key_format!`, this can be used to convert keynames from the standard ruby_format to CamelCase: ``` ruby json.key_format! :camelize => :lower -json.first_name "David" +json.first_name 'David' # => { "firstName": "David" } ``` You can set this globaly with the class method `key_format` (from inside your environment.rb for example):