From 1632a3a49fb318be25d832b7efb17093bd7ef8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 4 May 2011 12:04:29 +0200 Subject: More AV::Base cleanup. --- actionpack/lib/abstract_controller/rendering.rb | 18 ++----- actionpack/lib/action_view/base.rb | 57 +++++++++++----------- actionpack/lib/action_view/helpers.rb | 2 + .../lib/action_view/helpers/controller_helper.rb | 21 ++++++++ 4 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/controller_helper.rb (limited to 'actionpack') diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index a5f983a55b..f78365afdb 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -46,20 +46,9 @@ module AbstractController 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 @@ module AbstractController view_renderer.render(view_context, options) end - private # This method should return a hash with assigns. diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index db3c83d028..c98110353f 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -156,41 +156,45 @@ module ActionView #:nodoc: 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 + 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 @@ module ActionView #:nodoc: @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 diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index 3ef9826d30..78a68db282 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -9,6 +9,7 @@ module ActionView #:nodoc: autoload :AtomFeedHelper autoload :CacheHelper autoload :CaptureHelper + autoload :ControllerHelper autoload :CsrfHelper autoload :DateHelper autoload :DebugHelper @@ -39,6 +40,7 @@ module ActionView #:nodoc: include AtomFeedHelper include CacheHelper include CaptureHelper + include ControllerHelper include CsrfHelper include DateHelper include DebugHelper diff --git a/actionpack/lib/action_view/helpers/controller_helper.rb b/actionpack/lib/action_view/helpers/controller_helper.rb new file mode 100644 index 0000000000..e22331cb3c --- /dev/null +++ b/actionpack/lib/action_view/helpers/controller_helper.rb @@ -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 \ No newline at end of file -- cgit v1.2.3