diff options
author | José Valim <jose.valim@gmail.com> | 2010-03-08 02:04:18 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-03-08 02:04:18 +0100 |
commit | ffd8d753f171a33cb0f8dadaff7fc5ba12b8f6b0 (patch) | |
tree | fca50e828f91520f2e99323eff90e1bbc81bea7d /actionpack | |
parent | c7564d74e8a9b451f9fc78566ab0c734671f9612 (diff) | |
download | rails-ffd8d753f171a33cb0f8dadaff7fc5ba12b8f6b0.tar.gz rails-ffd8d753f171a33cb0f8dadaff7fc5ba12b8f6b0.tar.bz2 rails-ffd8d753f171a33cb0f8dadaff7fc5ba12b8f6b0.zip |
Move layout lookup to views.
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/abstract_controller/base.rb | 6 | ||||
-rw-r--r-- | actionpack/lib/abstract_controller/compatibility.rb | 18 | ||||
-rw-r--r-- | actionpack/lib/abstract_controller/layouts.rb | 25 | ||||
-rw-r--r-- | actionpack/lib/abstract_controller/rendering.rb | 8 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/implicit_render.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/rendering.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/base.rb | 6 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/partials.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/rendering.rb | 16 | ||||
-rw-r--r-- | actionpack/lib/action_view/template/lookup.rb | 8 | ||||
-rw-r--r-- | actionpack/test/abstract/layouts_test.rb | 2 |
11 files changed, 39 insertions, 57 deletions
diff --git a/actionpack/lib/abstract_controller/base.rb b/actionpack/lib/abstract_controller/base.rb index ea88a2d24d..c12b584144 100644 --- a/actionpack/lib/abstract_controller/base.rb +++ b/actionpack/lib/abstract_controller/base.rb @@ -7,7 +7,6 @@ module AbstractController class Base attr_internal :response_body attr_internal :action_name - attr_internal :formats class << self attr_reader :abstract @@ -100,11 +99,6 @@ module AbstractController abstract! - # Initialize controller with nil formats. - def initialize #:nodoc: - @_formats = nil - end - def config @config ||= ActiveSupport::InheritableOptions.new(self.class.config) end diff --git a/actionpack/lib/abstract_controller/compatibility.rb b/actionpack/lib/abstract_controller/compatibility.rb deleted file mode 100644 index 85fb1364b7..0000000000 --- a/actionpack/lib/abstract_controller/compatibility.rb +++ /dev/null @@ -1,18 +0,0 @@ -module AbstractController - module Compatibility - extend ActiveSupport::Concern - - def _find_layout(name, details) - details[:prefix] = nil if name =~ /\blayouts/ - super - end - - # Move this into a "don't run in production" module - def _default_layout(details, require_layout = false) - super - rescue ActionView::MissingTemplate - _layout_for_name(_layout({}), {}) - nil - end - end -end diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index c26593dd19..ac0f646e19 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -260,9 +260,11 @@ module AbstractController raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" when nil if name + _prefix = "layouts" unless _implied_layout_name =~ /\blayouts/ + self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def _layout(details) - if template_exists?("#{_implied_layout_name}", :_prefix => "layouts") + if template_exists?("#{_implied_layout_name}", :_prefix => #{_prefix.inspect}) "#{_implied_layout_name}" else super @@ -290,6 +292,7 @@ module AbstractController # render_template if layout layout = _layout_for_option(layout, options[:_template].details) + layout = find_template(layout, {}) response = layout.render(view_context, options[:locals] || {}) { response } end @@ -309,7 +312,7 @@ module AbstractController # the lookup to. By default, layout lookup is limited to the # formats specified for the current request. def _layout_for_name(name, details) - name && _find_layout(name, details) + name end # Determine the layout for a given name and details, taking into account @@ -337,23 +340,7 @@ module AbstractController return unless (options.keys & [:text, :inline, :partial]).empty? || options.key?(:layout) layout = options.key?(:layout) ? options[:layout] : :default - options[:_layout] = _layout_for_option(layout, options[:_template].details) - end - - # Take in the name and details and find a Template. - # - # ==== Parameters - # name<String>:: The name of the template to retrieve - # details<Hash>:: A list of details to restrict the search by. This - # might include details like the format or locale of the template. - # - # ==== Returns - # Template:: A template object matching the name and details - def _find_layout(name, details) - prefix = details.key?(:prefix) ? details.delete(:prefix) : "layouts" - # TODO This should happen automatically - template_lookup.details = template_lookup.details.merge(:formats => details[:formats]) - find_template(name, :_prefix => prefix) + options[:layout] = _layout_for_option(layout, options[:_template].details) end # Returns the default layout for this controller and a given set of details. diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index df33e8b480..8125badc75 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -15,10 +15,12 @@ module AbstractController included do class_attribute :_view_paths - delegate :_view_paths, :to => :'self.class' self._view_paths = ActionView::PathSet.new end + delegate :formats, :formats=, :to => :template_lookup + delegate :_view_paths, :to => :'self.class' + # An instance of a view class. The default view class is ActionView::Base # # The view class must have the following methods: @@ -180,11 +182,11 @@ module AbstractController end def details_for_render - { :formats => formats, :locale => [I18n.locale] } + { } end def _normalize_details(options) - details = details_for_render + details = template_lookup.details details[:formats] = Array(options[:format]) if options[:format] details[:locale] = Array(options[:locale]) if options[:locale] details diff --git a/actionpack/lib/action_controller/metal/implicit_render.rb b/actionpack/lib/action_controller/metal/implicit_render.rb index da717fcce2..c3e3b8fdf5 100644 --- a/actionpack/lib/action_controller/metal/implicit_render.rb +++ b/actionpack/lib/action_controller/metal/implicit_render.rb @@ -12,8 +12,7 @@ module ActionController def method_for_action(action_name) super || begin - # TODO This should use template lookup - if view_paths.exists?(action_name.to_s, details_for_render, controller_path) + if template_exists?(action_name.to_s, :_prefix => controller_path) "default_render" end end diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index 0a4215028b..e2d2e2312b 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -5,7 +5,7 @@ module ActionController include ActionController::RackDelegation include AbstractController::Rendering - def process_action(*) + def process(*) self.formats = request.formats.map {|x| x.to_sym } super end diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 22bc390dc5..5acd2a8b82 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -263,11 +263,7 @@ module ActionView #:nodoc: attr_internal :controller, :template, :config attr_reader :template_lookup - delegate :find, :view_paths, :view_paths=, :to => :template_lookup - - def formats=(formats) - update_details(:formats => Array(formats)) - end + delegate :find, :formats, :formats=, :view_paths, :view_paths=, :to => :template_lookup def update_details(details) old_details = template_lookup.details diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 74513935a7..52cb188508 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -330,7 +330,7 @@ module ActionView details = options[:_details] # TODO This should happen automatically as well - self.formats = details[:formats] if details + self.formats = details[:formats] if details[:formats] renderer = PartialRenderer.new(self, options, nil) text = renderer.render options[:_template] = renderer.template diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 1be5675e37..b92a03ddbd 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -88,12 +88,26 @@ module ActionView # _layout:: The layout, if any, to wrap the Template in def render_template(options) _evaluate_assigns_and_ivars - template, layout = options.values_at(:_template, :_layout) + template, layout = options.values_at(:_template, :layout) _render_template(template, layout, options) end + def _find_layout(template, layout) + begin + prefix = "layouts" unless layout =~ /\blayouts/ + layout = find(layout, prefix) + rescue ActionView::MissingTemplate => e + update_details(:formats => nil) do + raise unless template_lookup.exists?(layout, prefix) + end + end + end + def _render_template(template, layout = nil, options = {}) + self.formats = template.details[:formats] + locals = options[:locals] || {} + layout = _find_layout(template, layout) if layout.is_a?(String) ActiveSupport::Notifications.instrument("action_view.render_template", :identifier => template.identifier, :layout => layout.try(:identifier)) do diff --git a/actionpack/lib/action_view/template/lookup.rb b/actionpack/lib/action_view/template/lookup.rb index ea3a12615b..30de093934 100644 --- a/actionpack/lib/action_view/template/lookup.rb +++ b/actionpack/lib/action_view/template/lookup.rb @@ -23,6 +23,14 @@ module ActionView self.view_paths = view_paths end + def formats + @details[:formats] + end + + def formats=(value) + self.details = @details.merge(:formats => Array(value)) + end + def view_paths=(paths) @view_paths = ActionView::Base.process_view_paths(paths) end diff --git a/actionpack/test/abstract/layouts_test.rb b/actionpack/test/abstract/layouts_test.rb index b6d89ea489..fcc91d03f1 100644 --- a/actionpack/test/abstract/layouts_test.rb +++ b/actionpack/test/abstract/layouts_test.rb @@ -11,7 +11,7 @@ module AbstractControllerTests self.view_paths = [ActionView::FixtureResolver.new( "layouts/hello.erb" => "With String <%= yield %>", "layouts/hello_override.erb" => "With Override <%= yield %>", - "layouts/abstract_controller_tests/layouts/with_string_implied_child.erb" => + "abstract_controller_tests/layouts/with_string_implied_child.erb" => "With Implied <%= yield %>", "layouts/overwrite.erb" => "Overwrite <%= yield %>", "layouts/with_false_layout.erb" => "False Layout <%= yield %>" |