Skip to content

Commit

Permalink
Correctly handle false single values.
Browse files Browse the repository at this point in the history
The use of the #one? method was causing a boolean value of false to be
skipped.  This is unexpected and problematic in most situations.
  • Loading branch information
bigjason committed Feb 8, 2012
1 parent 46679c6 commit 9ea8c97
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/jbuilder.rb
Expand Up @@ -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| ... }
Expand Down Expand Up @@ -182,4 +182,4 @@ def _inline_extract(container, record, attributes)
end
end

require "jbuilder_template" if defined?(ActionView::Template)
require "jbuilder_template" if defined?(ActionView::Template)
10 changes: 9 additions & 1 deletion test/jbuilder_test.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -225,4 +233,4 @@ def initialize(name, age)

assert_equal "stuff", JSON.parse(json)["each"]
end
end
end

0 comments on commit 9ea8c97

Please sign in to comment.