2013-01-14 03:27:58 +00:00
|
|
|
module Draper
|
|
|
|
module AutomaticDelegation
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# Delegates missing instance methods to the source object.
|
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
return super unless delegatable?(method)
|
|
|
|
|
|
|
|
self.class.delegate method
|
|
|
|
send(method, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks if the decorator responds to an instance method, or is able to
|
|
|
|
# proxy it to the source object.
|
2013-02-11 23:08:21 +00:00
|
|
|
def respond_to_missing?(method, include_private = false)
|
2013-01-14 03:27:58 +00:00
|
|
|
super || delegatable?(method)
|
|
|
|
end
|
|
|
|
|
|
|
|
# @private
|
|
|
|
def delegatable?(method)
|
2013-04-30 13:10:52 +00:00
|
|
|
object.respond_to?(method)
|
2013-01-14 03:27:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
# Proxies missing class methods to the source class.
|
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
return super unless delegatable?(method)
|
|
|
|
|
|
|
|
source_class.send(method, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks if the decorator responds to a class method, or is able to proxy
|
|
|
|
# it to the source class.
|
2013-02-11 23:08:21 +00:00
|
|
|
def respond_to_missing?(method, include_private = false)
|
2013-01-14 03:27:58 +00:00
|
|
|
super || delegatable?(method)
|
|
|
|
end
|
|
|
|
|
|
|
|
# @private
|
|
|
|
def delegatable?(method)
|
|
|
|
source_class? && source_class.respond_to?(method)
|
|
|
|
end
|
2013-02-17 10:38:47 +00:00
|
|
|
|
|
|
|
# @private
|
|
|
|
# Avoids reloading the model class when ActiveSupport clears autoloaded
|
|
|
|
# dependencies in development mode.
|
|
|
|
def before_remove_const
|
|
|
|
end
|
2013-01-14 03:27:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
|
|
|
private :delegatable?
|
|
|
|
private_class_method :delegatable?
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|