diff --git a/lib/draper/base.rb b/lib/draper/base.rb index 6f45dc58..0afae223 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -5,6 +5,7 @@ class Base attr_accessor :model DEFAULT_DENIED = Object.new.methods + FORCED_PROXY = [:to_param] self.denied = DEFAULT_DENIED def initialize(input) @@ -45,16 +46,15 @@ def to_model private def select_methods - self.allowed || (model.public_methods - denied) + specified = self.allowed || (model.public_methods - denied) + (specified - self.public_methods) + FORCED_PROXY end def build_methods select_methods.each do |method| - unless self.respond_to?(method) - (class << self; self; end).class_eval do - define_method method do |*args, &block| - model.send method, *args, &block - end + (class << self; self; end).class_eval do + define_method method do |*args, &block| + model.send method, *args, &block end end end diff --git a/spec/base_spec.rb b/spec/base_spec.rb index 9ca34e83..9fc98b0a 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -17,7 +17,16 @@ subject.gsub("Sample"){|match| "Super"}.should == "Super String" end - context ".draper" do + it "should not override a defined method with a source method" do + DecoratorWithApplicationHelper.new(source).length.should == "overridden" + end + + it "should always proxy to_param" do + source.send :class_eval, "def to_param; 1; end" + Draper::Base.new(source).to_param.should == 1 + end + + context ".decorate" do it "should return a collection of wrapped objects when given a collection of source objects" do sources = ["one", "two", "three"] output = Draper::Base.decorate(sources) diff --git a/spec/samples/decorator_with_application_helper.rb b/spec/samples/decorator_with_application_helper.rb index 3e006b43..edc8956d 100644 --- a/spec/samples/decorator_with_application_helper.rb +++ b/spec/samples/decorator_with_application_helper.rb @@ -14,4 +14,8 @@ def sample_link def sample_truncate h.truncate("Once upon a time", :length => 7) end + + def length + "overridden" + end end