Permalink
Browse files

Fixes expected behavior with nil value passed to collection keyword

  • Loading branch information...
1 parent 142fa12 commit 3078e84ff84715eaa2402ad6e99043307663c1e9 @nesquena nesquena committed Apr 18, 2012
Showing with 22 additions and 1 deletion.
  1. +1 −1 lib/rabl/engine.rb
  2. +8 −0 test/engine_test.rb
  3. +13 −0 test/renderer_test.rb
View
@@ -118,7 +118,7 @@ def collection(data, options={})
@_collection_name = options[:root] if options[:root]
@_collection_name ||= data.values.first if data.respond_to?(:each_pair)
@_object_root_name = options[:object_root] if options.has_key?(:object_root)
- self.object(data_object(data).to_a) if data
+ self.object(data_object(data || []).to_a) if data
end
# Sets the cache key to be used by ActiveSupport::Cache.expand_cache_key
View
@@ -111,6 +111,14 @@
end
context "#collection" do
+ asserts "that it sets object to be blank array" do
+ template = rabl %{
+ collection []
+ }
+ scope = Object.new
+ template.render(scope)
+ end.equals "[]"
+
asserts "that it sets object to be casted as a simple array" do
template = rabl %{
collection @users
View
@@ -4,6 +4,19 @@
context "Rabl::Renderer" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
context "#render" do
+ asserts 'renders empty array' do
+ source = %q{
+ collection @users
+ attribute :name, :as => 'city'
+ }
+
+ scope = Object.new
+ scope.instance_variable_set :@users, []
+
+ renderer = Rabl::Renderer.new(source, [], { :format => 'json', :root => true, :view_path => '/path/to/views', :scope => scope })
+ renderer.render
+ end.equals "[]"
+
asserts 'renders string as source' do
source = %q{
object @user

0 comments on commit 3078e84

Please sign in to comment.