Skip to content

Commit

Permalink
Implement forced proxy for to_param
Browse files Browse the repository at this point in the history
  • Loading branch information
jcasimir committed Jul 23, 2011
1 parent cadac79 commit 9e8f1a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/draper/base.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion spec/base_spec.rb
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions spec/samples/decorator_with_application_helper.rb
Expand Up @@ -14,4 +14,8 @@ def sample_link
def sample_truncate
h.truncate("Once upon a time", :length => 7)
end

def length
"overridden"
end
end

0 comments on commit 9e8f1a3

Please sign in to comment.