Proxy class methods using method_missing
This commit is contained in:
parent
6abf506c82
commit
4f3bf6ebb7
|
@ -115,14 +115,6 @@ module Draper
|
||||||
self.send(:include, Draper::LazyHelpers)
|
self.send(:include, Draper::LazyHelpers)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Use primarily by `form_for`, this returns an instance of
|
|
||||||
# `ActiveModel::Name` set to the wrapped model's class name
|
|
||||||
#
|
|
||||||
# @return [ActiveModel::Name] model_name
|
|
||||||
def self.model_name
|
|
||||||
ActiveModel::Name.new(model_class)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Fetch the original wrapped model.
|
# Fetch the original wrapped model.
|
||||||
#
|
#
|
||||||
# @return [Object] original_model
|
# @return [Object] original_model
|
||||||
|
@ -137,7 +129,7 @@ module Draper
|
||||||
@model == other.model
|
@model == other.model
|
||||||
end
|
end
|
||||||
|
|
||||||
def respond_to?(method)
|
def respond_to?(method, include_private = false)
|
||||||
if select_methods.include?(method)
|
if select_methods.include?(method)
|
||||||
model.respond_to?(method)
|
model.respond_to?(method)
|
||||||
else
|
else
|
||||||
|
@ -153,6 +145,14 @@ module Draper
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.method_missing(method, *args, &block)
|
||||||
|
model_class.send(method, *args, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.respond_to?(method, include_private = false)
|
||||||
|
super || model_class.respond_to?(method)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def select_methods
|
def select_methods
|
||||||
specified = self.allowed || (model.public_methods.map{|s| s.to_sym} - denied.map{|s| s.to_sym})
|
specified = self.allowed || (model.public_methods.map{|s| s.to_sym} - denied.map{|s| s.to_sym})
|
||||||
|
|
|
@ -3,9 +3,23 @@ require 'draper'
|
||||||
|
|
||||||
describe Draper::Base do
|
describe Draper::Base do
|
||||||
before(:each){ ApplicationController.new.set_current_view_context }
|
before(:each){ ApplicationController.new.set_current_view_context }
|
||||||
subject{ Draper::Base.new(source) }
|
subject{ Decorator.new(source) }
|
||||||
let(:source){ Product.new }
|
let(:source){ Product.new }
|
||||||
|
|
||||||
|
context("proxying class methods") do
|
||||||
|
it "should pass missing class method calls on to the wrapped class" do
|
||||||
|
subject.class.sample_class_method.should == "sample class method"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should respond_to a wrapped class method" do
|
||||||
|
subject.class.should respond_to(:sample_class_method)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should still respond_to it's own class methods" do
|
||||||
|
subject.class.should respond_to(:own_class_method)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context(".helpers") do
|
context(".helpers") do
|
||||||
it "should have a valid view_context" do
|
it "should have a valid view_context" do
|
||||||
subject.helpers.should be
|
subject.helpers.should be
|
||||||
|
@ -19,12 +33,6 @@ describe Draper::Base do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context(".model_name") do
|
|
||||||
it "should return an ActiveModel::Name instance" do
|
|
||||||
Draper::Base.model_name.should be_instance_of(ActiveModel::Name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context(".decorates") do
|
context(".decorates") do
|
||||||
it "sets the model class for the decorator" do
|
it "sets the model class for the decorator" do
|
||||||
ProductDecorator.new(source).model_class.should == Product
|
ProductDecorator.new(source).model_class.should == Product
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class Decorator < Draper::Base
|
||||||
|
def self.own_class_method
|
||||||
|
"own class method"
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,6 +3,10 @@ class Product < ActiveRecord::Base
|
||||||
return Product.new
|
return Product.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.sample_class_method
|
||||||
|
"sample class method"
|
||||||
|
end
|
||||||
|
|
||||||
def hello_world
|
def hello_world
|
||||||
"Hello, World"
|
"Hello, World"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue