From e2a757dd2d4d17be4816d495cd53c6de0844a7a8 Mon Sep 17 00:00:00 2001 From: "Daniel, Dao Quang Minh" Date: Sat, 13 Oct 2012 00:10:11 +0800 Subject: [PATCH] memoize helpers this does not instantiate new instance of HelpersWrapper every time "#helpers" is accessed --- lib/draper/decorator.rb | 2 +- spec/draper/decorator_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/draper/decorator.rb b/lib/draper/decorator.rb index 1b9f904..74672fe 100755 --- a/lib/draper/decorator.rb +++ b/lib/draper/decorator.rb @@ -196,7 +196,7 @@ module Draper # # @return [Object] proxy def helpers - HelpersWrapper.new self.class.helpers + @helpers ||= HelpersWrapper.new self.class.helpers end alias :h :helpers diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index 71f7805..0e1cd51 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -28,6 +28,13 @@ describe Draper::Decorator do it "is aliased to .h" do subject.h.should == subject.helpers end + + it "initializes the wrapper only once" do + helper_proxy = subject.helpers + helper_proxy.stub(:test_method) { "test_method" } + subject.helpers.test_method.should eq("test_method") + subject.helpers.test_method.should eq("test_method") + end end context("#helpers") do