diff options
author | José Valim <jose.valim@gmail.com> | 2010-03-08 02:58:16 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-03-08 02:58:16 +0100 |
commit | 4bae77a89baf0fee15c6b2cfd3987f7344b56a1f (patch) | |
tree | 07ff2cc5b6dfb4ec54708fcf34bdd843d44c5fa7 /actionpack/lib | |
parent | ffd8d753f171a33cb0f8dadaff7fc5ba12b8f6b0 (diff) | |
download | rails-4bae77a89baf0fee15c6b2cfd3987f7344b56a1f.tar.gz rails-4bae77a89baf0fee15c6b2cfd3987f7344b56a1f.tar.bz2 rails-4bae77a89baf0fee15c6b2cfd3987f7344b56a1f.zip |
More cleanup on the layouts side.
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/abstract_controller.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/abstract_controller/layouts.rb | 81 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/compatibility.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/partials.rb | 13 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/rendering.rb | 10 |
5 files changed, 34 insertions, 74 deletions
diff --git a/actionpack/lib/abstract_controller.rb b/actionpack/lib/abstract_controller.rb index c1b035306b..05348d8ba0 100644 --- a/actionpack/lib/abstract_controller.rb +++ b/actionpack/lib/abstract_controller.rb @@ -14,8 +14,6 @@ module AbstractController autoload :Base autoload :Callbacks autoload :Collector - autoload :Compatibility - autoload :DetailsCache autoload :Helpers autoload :Layouts autoload :Logger 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? diff --git a/actionpack/lib/action_controller/metal/compatibility.rb b/actionpack/lib/action_controller/metal/compatibility.rb index 518272b4cf..0cb20b2f0f 100644 --- a/actionpack/lib/action_controller/metal/compatibility.rb +++ b/actionpack/lib/action_controller/metal/compatibility.rb @@ -2,8 +2,6 @@ module ActionController module Compatibility extend ActiveSupport::Concern - include AbstractController::Compatibility - class ::ActionController::ActionControllerError < StandardError #:nodoc: end diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 52cb188508..eecc88a1e0 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -182,9 +182,6 @@ module ActionView @view = view_context @partial_names = PARTIAL_NAMES[@view.controller.class] - key = Thread.current[:format_locale_key] - @templates = TEMPLATES[key] if key - setup(options, block) end @@ -296,15 +293,9 @@ module ActionView end end - def find_template(path = @path) - unless @templates - path && _find_template(path) - else - path && @templates[path] ||= _find_template(path) - end - end + def find_template(path=@path) + return path unless path.is_a?(String) - def _find_template(path) if controller = @view.controller prefix = controller.controller_path unless path.include?(?/) end diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index b92a03ddbd..17fb110eb4 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -34,7 +34,6 @@ module ActionView end if template - layout = find(layout) if layout _render_template(template, layout, :locals => options[:locals]) end when :update @@ -92,13 +91,12 @@ module ActionView _render_template(template, layout, options) end - def _find_layout(template, layout) + def _find_layout(layout) begin - prefix = "layouts" unless layout =~ /\blayouts/ - layout = find(layout, prefix) + find(layout) rescue ActionView::MissingTemplate => e update_details(:formats => nil) do - raise unless template_lookup.exists?(layout, prefix) + raise unless template_lookup.exists?(layout) end end end @@ -107,7 +105,7 @@ module ActionView self.formats = template.details[:formats] locals = options[:locals] || {} - layout = _find_layout(template, layout) if layout.is_a?(String) + layout = _find_layout(layout) if layout ActiveSupport::Notifications.instrument("action_view.render_template", :identifier => template.identifier, :layout => layout.try(:identifier)) do |