From 5dd5757b25e77f262d3ca794952f6e3541824aaa Mon Sep 17 00:00:00 2001 From: Alex Okolish Date: Sat, 20 Oct 2012 15:20:14 -0700 Subject: [PATCH] fix bug: unitialized constant NilClassDecorator no need to decorate an empty array anyways --- lib/draper/decorator.rb | 2 +- spec/draper/decorator_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/draper/decorator.rb b/lib/draper/decorator.rb index 8d72d37..0a078d8 100755 --- a/lib/draper/decorator.rb +++ b/lib/draper/decorator.rb @@ -60,7 +60,7 @@ module Draper define_method(association_symbol) do orig_association = model.send(association_symbol) - return orig_association if orig_association.nil? + return orig_association if orig_association.nil? || orig_association == [] return decorated_associations[association_symbol] if decorated_associations[association_symbol] orig_association = orig_association.send(options[:scope]) if options[:scope] diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index 1e62aa2..b9dee6b 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -198,6 +198,24 @@ describe Draper::Decorator do end end + context "when the association returns an empty collection" do + before(:each) do + subject.class_eval{ decorates_association :poro_similar_products } + source.stub(:poro_similar_products ){ [] } + end + + context "when find_association_reflection returns nil" do + before(:each) do + subject.stub(:find_association_reflection => nil) + end + + it "causes the association's method to return the empty collection" do + subject.poro_similar_products.should eq([]) + subject.poro_similar_products.should be_instance_of(Array) + end + end + end + context "#find" do before(:each){ subject.class_eval{ decorates_association :similar_products } } context "with a block" do