Skip to content

Commit

Permalink
Make Blank object serializable with Marshal
Browse files Browse the repository at this point in the history
[Fix #242]
  • Loading branch information
rwz committed Feb 15, 2015
1 parent 5962229 commit 7083f28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/jbuilder.rb
@@ -1,4 +1,5 @@
require 'jbuilder/jbuilder'
require 'jbuilder/blank'
require 'jbuilder/key_formatter'
require 'jbuilder/errors'
require 'multi_json'
Expand All @@ -21,7 +22,7 @@ def self.encode(*args, &block)
new(*args, &block).target!
end

BLANK = ::Object.new
BLANK = Blank.new

def set!(key, value = BLANK, *args, &block)
result = if block
Expand Down
7 changes: 7 additions & 0 deletions lib/jbuilder/blank.rb
@@ -0,0 +1,7 @@
class Jbuilder
class Blank
def ==(other)
super || Blank === other
end
end
end
21 changes: 21 additions & 0 deletions test/jbuilder_template_test.rb
Expand Up @@ -190,6 +190,27 @@ def assert_collection_rendered(json, context = nil)
assert_equal '{"posts":[]}', json
end

test 'cache an empty block' do
undef_context_methods :fragment_name_with_digest, :cache_fragment_name

render_jbuilder <<-JBUILDER
json.cache! 'nothing' do
end
JBUILDER

json = nil

assert_nothing_raised do
json = render_jbuilder <<-JBUILDER
json.foo 'bar'
json.cache! 'nothing' do
end
JBUILDER
end

assert_equal 'bar', MultiJson.load(json)['foo']
end

test 'fragment caching a JSON object' do
undef_context_methods :fragment_name_with_digest, :cache_fragment_name

Expand Down

0 comments on commit 7083f28

Please sign in to comment.