Skip to content

Commit

Permalink
memoize helpers
Browse files Browse the repository at this point in the history
this does not instantiate new instance of HelpersWrapper
every time "#helpers" is accessed
  • Loading branch information
dqminh committed Oct 12, 2012
1 parent 02add97 commit e2a757d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/draper/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def ==(other)
#
# @return [Object] proxy
def helpers
HelpersWrapper.new self.class.helpers
@helpers ||= HelpersWrapper.new self.class.helpers
end
alias :h :helpers

Expand Down
7 changes: 7 additions & 0 deletions spec/draper/decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
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
Expand Down

0 comments on commit e2a757d

Please sign in to comment.