Partials should not raise an error with empty object

This commit is contained in:
ccocchi 2012-03-29 10:58:20 +02:00
parent 62693749c5
commit 3e9c903077
2 changed files with 37 additions and 3 deletions

View File

@ -62,8 +62,11 @@ module RablFastJson
end
def partial(template_path, options = {})
raise "No object was given to partial" if options[:object].blank?
raise "No object was given to partial" if options[:object].nil?
object = options[:object]
return [] if object.respond_to?(:empty?) && object.empty?
template = Library.instance.get(template_path)
object.respond_to?(:each) ? template.render_collection(object) : template.render_resource(object)
end

View File

@ -57,4 +57,35 @@ class TestCompiledTemplate < ActiveSupport::TestCase
@template.source = { :name => [condition, proc] }
assert_equal({}, @template.render)
end
test "partial should be evaluated at rendering time" do
# Set assigns
@context.stub(:instance_variable_get).with(:@_assigns).and_return({'user' => @data})
@data.stub(:respond_to?).with(:empty?).and_return(false)
# Stub Library#get
t = RablFastJson::CompiledTemplate.new
t.source, t.context = { :name => :name }, @context
RablFastJson::Library.reset_instance
RablFastJson::Library.instance.should_receive(:get).with('users/base').and_return(t)
@template.data = false
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
assert_equal({ :user => { :name => 'foobar' } }, @template.render)
end
test "partial with nil values should raise an error" do
@template.data = false
@template.source = { :user => ->(s) { partial('users/base') } }
assert_raises(RuntimeError) { @template.render }
end
test "partial with empty values should not raise an error" do
@template.data = false
@template.source = { :users => ->(s) { partial('users/base', :object => []) } }
assert_equal({ :users => [] }, @template.render)
end
end