memoize helpers

this does not instantiate new instance of HelpersWrapper
every time "#helpers" is accessed
This commit is contained in:
Daniel, Dao Quang Minh 2012-10-13 00:10:11 +08:00
parent 02add9741c
commit e2a757dd2d
2 changed files with 8 additions and 1 deletions

View File

@ -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

View File

@ -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