require File.dirname(__FILE__)+'/mixins/erubis_capture_mixin' require File.dirname(__FILE__)+'/mixins/view_context_mixin' require File.dirname(__FILE__)+'/mixins/form_control_mixin' module Merb PROTECTED_IVARS = %w[@_new_cookie @method @env @controller @_body @_fingerprint_before @_session @_headers @_cookies @_request @_status @_view_context_cache @_response @_params] module GlobalHelper end # The ViewContext is really just an empty container for us to fill with # instance variables from the controller, include helpers into and then use as # the context object passed to Erubis when evaluating the templates. class ViewContext include Merb::ViewContextMixin include Merb::FormControls include Merb::GlobalHelper def initialize(controller) @_merb_partial_locals = {} @controller = controller (@controller.instance_variables - PROTECTED_IVARS).each do |ivar| self.instance_variable_set(ivar, @controller.instance_variable_get(ivar)) end begin self.class.class_eval("include Merb::#{@controller.class.name}Helper") rescue NameError MERB_LOGGER.info("Missing Helper: Merb::#{@controller.class.name}Helper") end end # hack so markaby doesn't dup us and lose ivars. def dup self end # accessor for the view. refers to the current @controller object def controller @controller end def request @controller.request end def params @controller.params end def cookies @controller.cookies end def headers @controller.headers end def session @controller.session end def response @controller.response end alias_method :old_respond_to?, :respond_to? def respond_to?(sym, include_private=false) old_respond_to?(sym, include_private) || @controller.respond_to?(sym, include_private) || @_merb_partial_locals.key?(sym) end # catch any method calls that the controller responds to # and delegate them back to the controller. def method_missing(sym, *args, &blk) if @controller.respond_to? sym @controller.send(sym, *args, &blk) elsif @_merb_partial_locals.key? sym @_merb_partial_locals[sym] else super end end end end