1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Implement forced proxy for to_param

This commit is contained in:
Jeff Casimir 2011-07-23 08:12:26 -07:00
parent cadac79498
commit 9e8f1a3a02
3 changed files with 20 additions and 7 deletions

View file

@ -5,6 +5,7 @@ module Draper
attr_accessor :model
DEFAULT_DENIED = Object.new.methods
FORCED_PROXY = [:to_param]
self.denied = DEFAULT_DENIED
def initialize(input)
@ -45,16 +46,15 @@ module Draper
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

View file

@ -17,7 +17,16 @@ describe Draper::Base do
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)

View file

@ -14,4 +14,8 @@ class DecoratorWithApplicationHelper < Draper::Base
def sample_truncate
h.truncate("Once upon a time", :length => 7)
end
def length
"overridden"
end
end