From 533da82a16dd58a7b0082a7f4f4aa45979e4ed8a Mon Sep 17 00:00:00 2001 From: Michael Fairley Date: Sun, 25 Sep 2011 00:13:29 -0700 Subject: [PATCH 1/2] Use method_missing to delegate methods to the model --- lib/draper/base.rb | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index 7584d4b..25b9dc0 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -6,6 +6,11 @@ module Draper DEFAULT_DENIED = Object.new.methods << :method_missing FORCED_PROXY = [:to_param] + FORCED_PROXY.each do |method| + define_method method do |*args, &block| + model.send method, *args, &block + end + end self.denied = DEFAULT_DENIED # Initialize a new decorator instance by passing in @@ -20,7 +25,6 @@ module Draper self.class.model_class = input.class if model_class.nil? @model = input self.context = context - build_methods end # Proxies to the class specified by `decorates` to automatically @@ -133,20 +137,26 @@ module Draper @model == other.model end + def respond_to?(method) + if select_methods.include?(method) + model.repsond_to?(method) + else + super + end + end + + def method_missing(method, *args, &block) + if select_methods.include?(method) + model.send(method, *args, &block) + else + super + end + end + private def select_methods specified = self.allowed || (model.public_methods.map{|s| s.to_sym} - denied.map{|s| s.to_sym}) (specified - self.public_methods.map{|s| s.to_sym}) + FORCED_PROXY end - - def build_methods - select_methods.each do |method| - (class << self; self; end).class_eval do - define_method method do |*args, &block| - model.send method, *args, &block - end - end - end - end end end From 58823a3d4941fa4ec8cb6f698ac81f8d261a1425 Mon Sep 17 00:00:00 2001 From: Michael Fairley Date: Thu, 29 Sep 2011 11:57:49 -0700 Subject: [PATCH 2/2] Fix typo --- lib/draper/base.rb | 2 +- spec/base_spec.rb | 4 ++-- spec/samples/decorator_with_allows.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index 25b9dc0..4c9685b 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -139,7 +139,7 @@ module Draper def respond_to?(method) if select_methods.include?(method) - model.repsond_to?(method) + model.respond_to?(method) else super end diff --git a/spec/base_spec.rb b/spec/base_spec.rb index cba2af5..2c915fb 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -163,11 +163,11 @@ describe Draper::Base do let(:subject_with_allows){ DecoratorWithAllows.new(source) } it "should echo the allowed method" do - subject_with_allows.should respond_to(:upcase) + subject_with_allows.should respond_to(:goodnight_moon) end it "should echo _only_ the allowed method" do - subject_with_allows.should_not respond_to(:downcase) + subject_with_allows.should_not respond_to(:hello_world) end end diff --git a/spec/samples/decorator_with_allows.rb b/spec/samples/decorator_with_allows.rb index 1fc0921..eda9901 100644 --- a/spec/samples/decorator_with_allows.rb +++ b/spec/samples/decorator_with_allows.rb @@ -1,3 +1,3 @@ class DecoratorWithAllows < Draper::Base - allows :upcase + allows :goodnight_moon end \ No newline at end of file