diff --git a/lib/jbuilder.rb b/lib/jbuilder.rb index 1759a485..c454ae73 100644 --- a/lib/jbuilder.rb +++ b/lib/jbuilder.rb @@ -126,7 +126,7 @@ def method_missing(method, *args) # json.age 32 # { "age": 32 } - when args.one? + when args.length == 1 set! method, args.first # json.comments { |json| ... } @@ -182,4 +182,4 @@ def _inline_extract(container, record, attributes) end end -require "jbuilder_template" if defined?(ActionView::Template) \ No newline at end of file +require "jbuilder_template" if defined?(ActionView::Template) diff --git a/test/jbuilder_test.rb b/test/jbuilder_test.rb index 7af41743..b1c7be31 100644 --- a/test/jbuilder_test.rb +++ b/test/jbuilder_test.rb @@ -12,6 +12,23 @@ class JbuilderTest < ActiveSupport::TestCase assert_equal "hello", JSON.parse(json)["content"] end + test "single key with false value" do + json = Jbuilder.encode do |json| + json.content false + end + + assert_equal false, JSON.parse(json)["content"] + end + + test "single key with nil value" do + json = Jbuilder.encode do |json| + json.content nil + end + + assert JSON.parse(json).has_key?("content") + assert_equal nil, JSON.parse(json)["content"] + end + test "multiple keys" do json = Jbuilder.encode do |json| json.title "hello" @@ -225,4 +242,4 @@ def initialize(name, age) assert_equal "stuff", JSON.parse(json)["each"] end -end \ No newline at end of file +end