Merge pull request #238 from fringd/master

fix problem with html_escape being private
This commit is contained in:
Steve Klabnik 2012-07-17 17:44:00 -07:00
commit c54341c1d9
4 changed files with 31 additions and 2 deletions

View File

@ -161,12 +161,30 @@ module Draper
decorate(model_class.last, options)
end
# Some helpers are private, for example html_escape... as a workaround
# we are wrapping the helpers in a delegator that passes the methods
# along through a send, which will ignore private/public distinctions
class HelpersWrapper
def initialize(helpers)
@helpers = helpers
end
def method_missing(method, *args, &block)
@helpers.send(method, *args, &block)
end
#needed for tests
def ==(other)
other.instance_variable_get(:@helpers) == @helpers
end
end
# Access the helpers proxy to call built-in and user-defined
# Rails helpers. Aliased to `.h` for convenience.
#
# @return [Object] proxy
def helpers
self.class.helpers
HelpersWrapper.new self.class.helpers
end
alias :h :helpers

View File

@ -706,9 +706,13 @@ describe Draper::Base do
it "is able to use l rather than helpers.l" do
now = Time.now
decorator.helpers.should_receive(:localize).with(now)
decorator.helpers.instance_variable_get(:@helpers).should_receive(:localize).with(now)
decorator.l now
end
it "is able to access html_escape, a private method" do
decorator.sample_html_escaped_text.should == '<script>danger</script>'
end
end
context "#method_missing" do

View File

@ -1,4 +1,7 @@
require 'active_support/core_ext/string/output_safety.rb'
module ApplicationHelper
include ERB::Util
def hello_world
"Hello, World!"
end

View File

@ -18,4 +18,8 @@ class DecoratorWithApplicationHelper < Draper::Base
def length
"overridden"
end
def sample_html_escaped_text
h.html_escape '<script>danger</script>'
end
end