From 9ea8c97253e341524e79461692f62dcefdfaa6a9 Mon Sep 17 00:00:00 2001 From: Jason Webb Date: Wed, 8 Feb 2012 16:35:03 -0700 Subject: [PATCH] Correctly handle false single values. The use of the #one? method was causing a boolean value of false to be skipped. This is unexpected and problematic in most situations. --- lib/jbuilder.rb | 4 ++-- test/jbuilder_test.rb | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) 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..e9a7aab3 100644 --- a/test/jbuilder_test.rb +++ b/test/jbuilder_test.rb @@ -12,6 +12,14 @@ 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 "multiple keys" do json = Jbuilder.encode do |json| json.title "hello" @@ -225,4 +233,4 @@ def initialize(name, age) assert_equal "stuff", JSON.parse(json)["each"] end -end \ No newline at end of file +end