Permalink
Browse files

More AV::Base cleanup.

  • Loading branch information...
1 parent 30bb17e commit 1632a3a49fb318be25d832b7efb17093bd7ef8ae @josevalim josevalim committed May 4, 2011
@@ -46,20 +46,9 @@ def process(*) #:nodoc:
module ClassMethods
def view_context_class
@view_context_class ||= begin
- controller = self
- Class.new(ActionView::Base) do
- if controller.respond_to?(:_routes) && controller._routes
- include controller._routes.url_helpers
- include controller._routes.mounted_helpers
- end
-
- if controller.respond_to?(:_helpers)
- include controller._helpers
-
- # TODO: Fix RJS to not require this
- self.helpers = controller._helpers
- end
- end
+ routes = _routes if respond_to?(:_routes)
+ helpers = _helpers if respond_to?(:_helpers)
+ ActionView::Base.prepare(routes, helpers)
end
end
end
@@ -121,7 +110,6 @@ def _render_template(options) #:nodoc:
view_renderer.render(view_context, options)
end
-
private
# This method should return a hash with assigns.
@@ -156,41 +156,45 @@ def cache_template_loading
def cache_template_loading=(value)
ActionView::Resolver.caching = value
end
+
+ def process_view_paths(value)
+ value.is_a?(PathSet) ?
+ value.dup : ActionView::PathSet.new(Array.wrap(value))
+ end
+
+ def xss_safe? #:nodoc:
+ true
+ end
+
+ # This method receives routes and helpers from the controller
+ # and return a subclass ready to be used as view context.
+ def prepare(routes, helpers) #:nodoc:
+ Class.new(self) do
+ if routes
+ include routes.url_helpers
+ include routes.mounted_helpers
+ end
+
+ if helpers
+ include helpers
+ self.helpers = helpers
@apotonick

apotonick May 25, 2012

Contributor

Why is self.helpers= set here and self._routes= is not? In cells-sinatra we have to do that ourselves so I wonder why it isn't done in the core itself.

+ end
+ end
+ end
end
attr_accessor :view_renderer
- attr_internal :request, :controller, :config, :assigns
+ attr_internal :config, :assigns
delegate :lookup_context, :to => :view_renderer
delegate :formats, :formats=, :locale, :locale=, :view_paths, :view_paths=, :to => :lookup_context
- delegate :request_forgery_protection_token, :params, :session, :cookies, :response, :headers,
- :flash, :action_name, :controller_name, :to => :controller
-
- delegate :logger, :to => :controller, :allow_nil => true
-
- def self.xss_safe? #:nodoc:
- true
- end
-
- def self.process_view_paths(value)
- value.is_a?(PathSet) ?
- value.dup : ActionView::PathSet.new(Array.wrap(value))
- end
-
def assign(new_assigns) # :nodoc:
@_assigns = new_assigns.each { |key, value| instance_variable_set("@#{key}", value) }
end
- def initialize(context = nil, assigns_for_first_render = {}, controller = nil, formats = nil) #:nodoc:
- assign(assigns_for_first_render)
- self.helpers = Module.new unless self.class.helpers
-
+ def initialize(context = nil, assigns = {}, controller = nil, formats = nil) #:nodoc:
@_config = {}
- if @_controller = controller
- @_request = controller.request if controller.respond_to?(:request)
- @_config = controller.config.inheritable_copy if controller.respond_to?(:config)
- end
# Handle all these for backwards compatibility.
# TODO Provide a new API for AV::Base and deprecate this one.
@@ -204,14 +208,11 @@ def initialize(context = nil, assigns_for_first_render = {}, controller = nil, f
@view_renderer = ActionView::Renderer.new(lookup_context)
end
+ assign(assigns)
+ assign_controller(controller)
_prepare_context
end
- # TODO Is this needed anywhere? Maybe deprecate it?
- def controller_path
- @controller_path ||= controller && controller.controller_path
- end
-
ActiveSupport.run_load_hooks(:action_view, self)
end
end
@@ -9,6 +9,7 @@ module Helpers #:nodoc:
autoload :AtomFeedHelper
autoload :CacheHelper
autoload :CaptureHelper
+ autoload :ControllerHelper
autoload :CsrfHelper
autoload :DateHelper
autoload :DebugHelper
@@ -39,6 +40,7 @@ module Helpers #:nodoc:
include AtomFeedHelper
include CacheHelper
include CaptureHelper
+ include ControllerHelper
include CsrfHelper
include DateHelper
include DebugHelper
@@ -0,0 +1,21 @@
+module ActionView
+ module Helpers
+ # This module keeps all methods and behavior in ActionView
+ # that simply delegates to the controller.
+ module ControllerHelper #:nodoc:
+ attr_internal :controller, :request
+
+ delegate :request_forgery_protection_token, :params, :session, :cookies, :response, :headers,
+ :flash, :action_name, :controller_name, :controller_path, :to => :controller
+
+ delegate :logger, :to => :controller, :allow_nil => true
+
+ def assign_controller(controller)
+ if @_controller = controller
+ @_request = controller.request if controller.respond_to?(:request)
+ @_config = controller.config.inheritable_copy if controller.respond_to?(:config)
+ end
+ end
+ end
+ end
+end

2 comments on commit 1632a3a

Contributor

ernie replied Jun 1, 2011

José, it looks like when you extracted controller_helper, you may have also wanted to copy the require for attr_internal (and maybe delegate?) from ActiveSupport over, as well. Just ran into an undefined method error when inheriting from FormBuilder, due to loading the ActionView::Helper module to do so:

class FormBuilder < ::ActionView::Helpers::FormBuilder

Confirm/deny? If "confirm", I can send a pull request.

Contributor

josevalim replied Jun 1, 2011

Please sign in to comment.