aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/abstract_controller/layouts.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-03-08 02:58:16 +0100
committerJosé Valim <jose.valim@gmail.com>2010-03-08 02:58:16 +0100
commit4bae77a89baf0fee15c6b2cfd3987f7344b56a1f (patch)
tree07ff2cc5b6dfb4ec54708fcf34bdd843d44c5fa7 /actionpack/lib/abstract_controller/layouts.rb
parentffd8d753f171a33cb0f8dadaff7fc5ba12b8f6b0 (diff)
downloadrails-4bae77a89baf0fee15c6b2cfd3987f7344b56a1f.tar.gz
rails-4bae77a89baf0fee15c6b2cfd3987f7344b56a1f.tar.bz2
rails-4bae77a89baf0fee15c6b2cfd3987f7344b56a1f.zip
More cleanup on the layouts side.
Diffstat (limited to 'actionpack/lib/abstract_controller/layouts.rb')
-rw-r--r--actionpack/lib/abstract_controller/layouts.rb81
1 files changed, 28 insertions, 53 deletions
diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb
index ac0f646e19..c6a94c5220 100644
--- a/actionpack/lib/abstract_controller/layouts.rb
+++ b/actionpack/lib/abstract_controller/layouts.rb
@@ -239,10 +239,10 @@ module AbstractController
def _write_layout_method
case defined?(@_layout) ? @_layout : nil
when String
- self.class_eval %{def _layout(details) #{@_layout.inspect} end}
+ self.class_eval %{def _layout; #{@_layout.inspect} end}
when Symbol
self.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
- def _layout(details)
+ def _layout
#{@_layout}.tap do |layout|
unless layout.is_a?(String) || !layout
raise ArgumentError, "Your layout method :#{@_layout} returned \#{layout}. It " \
@@ -253,9 +253,9 @@ module AbstractController
ruby_eval
when Proc
define_method :_layout_from_proc, &@_layout
- self.class_eval %{def _layout(details) _layout_from_proc(self) end}
+ self.class_eval %{def _layout; _layout_from_proc(self) end}
when false
- self.class_eval %{def _layout(details) end}
+ self.class_eval %{def _layout; end}
when true
raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
when nil
@@ -263,7 +263,7 @@ module AbstractController
_prefix = "layouts" unless _implied_layout_name =~ /\blayouts/
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
- def _layout(details)
+ def _layout
if template_exists?("#{_implied_layout_name}", :_prefix => #{_prefix.inspect})
"#{_implied_layout_name}"
else
@@ -277,43 +277,24 @@ module AbstractController
end
end
- def render_to_body(options = {})
- # In the case of a partial with a layout, handle the layout
- # here, and make sure the view does not try to handle it
- layout = options.delete(:layout) if options.key?(:partial)
+ def render_to_body(options={})
+ if (options.keys & [:text, :inline, :partial]).empty? || options.key?(:layout)
+ layout = options.key?(:layout) ? options[:layout] : :default
+ value = _layout_for_option(layout)
- response = super
+ # TODO Revisit this. Maybe we should pass a :layout_prefix?
+ options[:layout] = ((!value || value =~ /\blayouts/) ? value : "layouts/#{value}")
- # This is a little bit messy. We need to explicitly handle partial
- # layouts here since the core lookup logic is in the view, but
- # we need to determine the layout based on the controller
- #
- # TODO: An easier way to handle this would probably be to override
- # render_template
- if layout
- layout = _layout_for_option(layout, options[:_template].details)
- layout = find_template(layout, {})
- response = layout.render(view_context, options[:locals] || {}) { response }
+ # TODO Revisit this. :layout with :partial from controllers are not the same as in views
+ options[:layout] = view_context._find_layout(options[:layout]) if options.key?(:partial)
end
-
- response
+ super
end
private
# This will be overwritten by _write_layout_method
- def _layout(details) end
-
- # Determine the layout for a given name and details.
- #
- # ==== Parameters
- # name<String>:: The name of the template
- # details<Hash{Symbol => Object}>:: A list of details to restrict
- # the lookup to. By default, layout lookup is limited to the
- # formats specified for the current request.
- def _layout_for_name(name, details)
- name
- end
+ def _layout; end
# Determine the layout for a given name and details, taking into account
# the name type.
@@ -323,11 +304,11 @@ module AbstractController
# details<Hash{Symbol => Object}>:: A list of details to restrict
# the lookup to. By default, layout lookup is limited to the
# formats specified for the current request.
- def _layout_for_option(name, details)
+ def _layout_for_option(name)
case name
- when String then _layout_for_name(name, details)
- when true then _default_layout(details, true)
- when :default then _default_layout(details, false)
+ when String then name
+ when true then _default_layout(true)
+ when :default then _default_layout(false)
when false, nil then nil
else
raise ArgumentError,
@@ -335,14 +316,6 @@ module AbstractController
end
end
- def _determine_template(options)
- super
-
- 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
-
# Returns the default layout for this controller and a given set of details.
# Optionally raises an exception if the layout could not be found.
#
@@ -355,18 +328,20 @@ module AbstractController
#
# ==== Returns
# Template:: The template object for the default layout (or nil)
- def _default_layout(details, require_layout = false)
- if require_layout && _action_has_layout? && !_layout(details)
- raise ArgumentError,
- "There was no default layout for #{self.class} in #{view_paths.inspect}"
- end
-
+ def _default_layout(require_layout = false)
begin
- _layout_for_name(_layout(details), details) if _action_has_layout?
+ layout_name = _layout if _action_has_layout?
rescue NameError => e
raise NoMethodError,
"You specified #{@_layout.inspect} as the layout, but no such method was found"
end
+
+ if require_layout && _action_has_layout? && !layout_name
+ raise ArgumentError,
+ "There was no default layout for #{self.class} in #{view_paths.inspect}"
+ end
+
+ layout_name
end
def _action_has_layout?