From 759906d51272534941843fc80ae9f579b28c336c Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Sun, 6 Nov 2011 14:43:11 +0100 Subject: Fixed stale doc in AbstractController::Layouts --- actionpack/lib/abstract_controller/layouts.rb | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'actionpack/lib/abstract_controller/layouts.rb') diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index 10aa34c76b..bbf5efe565 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -213,7 +213,7 @@ module AbstractController # true:: raise an ArgumentError # # ==== Parameters - # * String, Symbol, false - The layout to use. + # * layout - The layout to use. # # ==== Options (conditions) # * :only - A list of actions to apply this layout to. @@ -310,14 +310,10 @@ module AbstractController # This will be overwritten by _write_layout_method def _layout; end - # Determine the layout for a given name and details, taking into account - # the name type. + # Determine the layout for a given name, taking into account the name type. # # ==== Parameters # * name - The name of the template - # * details - 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) case name when String then name @@ -330,15 +326,12 @@ module AbstractController end end - # Returns the default layout for this controller and a given set of details. + # Returns the default layout for this controller. # Optionally raises an exception if the layout could not be found. # # ==== Parameters - # * details - A list of details to restrict the search by. This - # might include details like the format or locale of the template. - # * require_layout - If this is true, raise an ArgumentError - # with details about the fact that the exception could not be - # found (defaults to false) + # * require_layout - If set to true and layout is not found, + # an ArgumentError exception is raised (defaults to false) # # ==== Returns # * template - The template object for the default layout (or nil) -- cgit v1.2.3 From 18ceed201b37d91ad6598d0f8b3c010e6cc48b15 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Tue, 6 Dec 2011 21:05:56 -0500 Subject: Allow layout fallback when using `layout` method Rails will now use your default layout (such as "layouts/application") when you specify a layout with `:only` and `:except` condition, and those conditions fail. For example, consider this snippet: class CarsController layout 'single_car', :only => :show end Rails will use 'layouts/single_car' when a request comes in `:show` action, and use 'layouts/application' (or 'layouts/cars', if exists) when a request comes in for any other actions. --- actionpack/lib/abstract_controller/layouts.rb | 59 +++++++++++++++------------ 1 file changed, 34 insertions(+), 25 deletions(-) (limited to 'actionpack/lib/abstract_controller/layouts.rb') diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index bbf5efe565..79edd5418e 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -244,42 +244,51 @@ module AbstractController def _write_layout_method remove_possible_method(:_layout) - case defined?(@_layout) ? @_layout : nil - when String - self.class_eval %{def _layout; #{@_layout.inspect} end}, __FILE__, __LINE__ - when Symbol - self.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1 - def _layout + prefixes = _implied_layout_name =~ /\blayouts/ ? [] : ["layouts"] + layout_definition = case defined?(@_layout) ? @_layout : nil + when String + @_layout.inspect + when Symbol + <<-RUBY #{@_layout}.tap do |layout| unless layout.is_a?(String) || !layout raise ArgumentError, "Your layout method :#{@_layout} returned \#{layout}. It " \ "should have returned a String, false, or nil" end end - end - ruby_eval - when Proc - define_method :_layout_from_proc, &@_layout - self.class_eval %{def _layout; _layout_from_proc(self) end}, __FILE__, __LINE__ - when false - self.class_eval %{def _layout; end}, __FILE__, __LINE__ - when true - raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" - when nil - if name - _prefixes = _implied_layout_name =~ /\blayouts/ ? [] : ["layouts"] - - self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def _layout - if template_exists?("#{_implied_layout_name}", #{_prefixes.inspect}) + RUBY + when Proc + define_method :_layout_from_proc, &@_layout + "_layout_from_proc(self)" + when false + nil + when true + raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" + when nil + if name + <<-RUBY + if template_exists?("#{_implied_layout_name}", #{prefixes.inspect}) "#{_implied_layout_name}" else super end + RUBY + end + end + + self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def _layout + if action_has_layout? + #{layout_definition} + elsif self.class.name + if template_exists?("#{_implied_layout_name}", #{prefixes.inspect}) + "#{_implied_layout_name}" + else + super end - RUBY + end end - end + RUBY self.class_eval { private :_layout } end end @@ -337,7 +346,7 @@ module AbstractController # * template - The template object for the default layout (or nil) def _default_layout(require_layout = false) begin - layout_name = _layout if action_has_layout? + layout_name = _layout rescue NameError => e raise e, "Could not render layout: #{e.message}" end -- cgit v1.2.3 From 239262fee03096d1e52acf8fe69de736726d87e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 8 Dec 2011 16:37:56 +0100 Subject: Optimize layout lookup to avoid double calls. --- actionpack/lib/abstract_controller/layouts.rb | 50 ++++++++++++++------------- 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'actionpack/lib/abstract_controller/layouts.rb') diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index 79edd5418e..6b6b38c64f 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -168,11 +168,12 @@ module AbstractController included do class_attribute :_layout_conditions remove_possible_method :_layout_conditions - delegate :_layout_conditions, :to => :'self.class' self._layout_conditions = {} _write_layout_method end + delegate :_layout_conditions, :to => "self.class" + module ClassMethods def inherited(klass) super @@ -188,7 +189,7 @@ module AbstractController # # ==== Returns # * Boolean - True if the action has a layout, false otherwise. - def action_has_layout? + def conditional_layout? return unless super conditions = _layout_conditions @@ -244,7 +245,13 @@ module AbstractController def _write_layout_method remove_possible_method(:_layout) - prefixes = _implied_layout_name =~ /\blayouts/ ? [] : ["layouts"] + prefixes = _implied_layout_name =~ /\blayouts/ ? [] : ["layouts"] + name_clause = if name + <<-RUBY + lookup_context.find_all("#{_implied_layout_name}", #{prefixes.inspect}).first || super + RUBY + end + layout_definition = case defined?(@_layout) ? @_layout : nil when String @_layout.inspect @@ -265,27 +272,15 @@ module AbstractController when true raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" when nil - if name - <<-RUBY - if template_exists?("#{_implied_layout_name}", #{prefixes.inspect}) - "#{_implied_layout_name}" - else - super - end - RUBY - end + name_clause end self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def _layout - if action_has_layout? + if conditional_layout? #{layout_definition} - elsif self.class.name - if template_exists?("#{_implied_layout_name}", #{prefixes.inspect}) - "#{_implied_layout_name}" - else - super - end + else + #{name_clause} end end RUBY @@ -298,8 +293,7 @@ module AbstractController if _include_layout?(options) layout = options.key?(:layout) ? options.delete(:layout) : :default - value = _layout_for_option(layout) - options[:layout] = (value =~ /\blayouts/ ? value : "layouts/#{value}") if value + options[:layout] = Proc.new { _normalize_layout(_layout_for_option(layout)) } end end @@ -314,6 +308,10 @@ module AbstractController @_action_has_layout end + def conditional_layout? + true + end + private # This will be overwritten by _write_layout_method @@ -335,6 +333,10 @@ module AbstractController end end + def _normalize_layout(value) + value.is_a?(String) && value !~ /\blayouts/ ? "layouts/#{value}" : value + end + # Returns the default layout for this controller. # Optionally raises an exception if the layout could not be found. # @@ -346,17 +348,17 @@ module AbstractController # * template - The template object for the default layout (or nil) def _default_layout(require_layout = false) begin - layout_name = _layout + value = _layout if action_has_layout? rescue NameError => e raise e, "Could not render layout: #{e.message}" end - if require_layout && action_has_layout? && !layout_name + if require_layout && action_has_layout? && !value raise ArgumentError, "There was no default layout for #{self.class} in #{view_paths.inspect}" end - layout_name + value end def _include_layout?(options) -- cgit v1.2.3 From 67cc07e0c4bd071dd3d2f4098e5a4c31ba590684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 8 Dec 2011 16:51:47 +0100 Subject: Just use the proc if there is a chance of layout lookup. --- actionpack/lib/abstract_controller/layouts.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/abstract_controller/layouts.rb') diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index 6b6b38c64f..8356d822da 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -293,7 +293,7 @@ module AbstractController if _include_layout?(options) layout = options.key?(:layout) ? options.delete(:layout) : :default - options[:layout] = Proc.new { _normalize_layout(_layout_for_option(layout)) } + options[:layout] = _layout_for_option(layout) end end @@ -323,9 +323,10 @@ module AbstractController # * name - The name of the template def _layout_for_option(name) case name - when String then name - when true then _default_layout(true) - when :default then _default_layout(false) + when String then _normalize_layout(name) + when Proc then name + when true then Proc.new { _default_layout(true) } + when :default then Proc.new { _default_layout(false) } when false, nil then nil else raise ArgumentError, @@ -358,7 +359,7 @@ module AbstractController "There was no default layout for #{self.class} in #{view_paths.inspect}" end - value + _normalize_layout(value) end def _include_layout?(options) -- cgit v1.2.3 From 5ad52152117ecda1166359c499bcd03ae6be3365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 9 Dec 2011 07:20:55 +0100 Subject: Deprecate implicit layout lookup in favor of inheriting the _layout config. --- actionpack/lib/abstract_controller/layouts.rb | 118 +++++++++++++++++++------- 1 file changed, 86 insertions(+), 32 deletions(-) (limited to 'actionpack/lib/abstract_controller/layouts.rb') diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index 8356d822da..8b6136d6ba 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -66,27 +66,40 @@ module AbstractController # == Inheritance Examples # # class BankController < ActionController::Base - # layout "bank_standard" + # # bank.html.erb exists + # + # class ExchangeController < BankController + # # exchange.html.erb exists + # + # class CurrencyController < BankController # # class InformationController < BankController + # layout "information" # - # class TellerController < BankController + # class TellerController < InformationController # # teller.html.erb exists # - # class TillController < TellerController + # class EmployeeController < InformationController + # # employee.html.erb exists + # layout nil # # class VaultController < BankController # layout :access_level_layout # - # class EmployeeController < BankController - # layout nil + # class TillController < BankController + # layout false + # + # In these examples, we have three implicit lookup scenrios: + # * The BankController uses the "bank" layout. + # * The ExchangeController uses the "exchange" layout. + # * The CurrencyController inherits the layout from BankController. # - # In these examples: - # * The InformationController uses the "bank_standard" layout, inherited from BankController. - # * The TellerController follows convention and uses +app/views/layouts/teller.html.erb+. - # * The TillController inherits the layout from TellerController and uses +teller.html.erb+ as well. + # However, when a layout is explicitly set, the explicitly set layout wins: + # * The InformationController uses the "information" layout, explicitly set. + # * The TellerController also uses the "information" layout, because the parent explicitly set it. + # * The EmployeeController uses the "employee" layout, because it set the layout to nil, resetting the parent configuration. # * The VaultController chooses a layout dynamically by calling the access_level_layout method. - # * The EmployeeController does not use a layout at all. + # * The TillController does not use a layout at all. # # == Types of layouts # @@ -126,6 +139,22 @@ module AbstractController # If no directory is specified for the template name, the template will by default be looked for in app/views/layouts/. # Otherwise, it will be looked up relative to the template root. # + # Setting the layout to nil forces it to be looked up in the filesystem and fallbacks to the parent behavior if none exists. + # Setting it to nil is useful to re-enable template lookup overriding a previous configuration set in the parent: + # + # class ApplicationController < ActionController::Base + # layout "application" + # end + # + # class PostsController < ApplicationController + # # Will use "application" layout + # end + # + # class CommentsController < ApplicationController + # # Will search for "comments" layout and fallback "application" layout + # layout nil + # end + # # == Conditional layouts # # If you have a layout that by default is applied to all the actions of a controller, you still have the option of rendering @@ -252,28 +281,53 @@ module AbstractController RUBY end - layout_definition = case defined?(@_layout) ? @_layout : nil - when String - @_layout.inspect - when Symbol - <<-RUBY - #{@_layout}.tap do |layout| - unless layout.is_a?(String) || !layout - raise ArgumentError, "Your layout method :#{@_layout} returned \#{layout}. It " \ - "should have returned a String, false, or nil" + if defined?(@_layout) + layout_definition = case @_layout + when String + @_layout.inspect + when Symbol + <<-RUBY + #{@_layout}.tap do |layout| + unless layout.is_a?(String) || !layout + raise ArgumentError, "Your layout method :#{@_layout} returned \#{layout}. It " \ + "should have returned a String, false, or nil" + end end - end - RUBY - when Proc - define_method :_layout_from_proc, &@_layout - "_layout_from_proc(self)" - when false - nil - when true - raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" - when nil - name_clause - end + RUBY + when Proc + define_method :_layout_from_proc, &@_layout + "_layout_from_proc(self)" + when false + nil + when true + raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" + when nil + name_clause + end + else + # Add a deprecation if the parent layout was explicitly set and the child + # still does a dynamic lookup. In next Rails release, we should @_layout + # to be inheritable so we can skip the child lookup if the parent explicitly + # set the layout. + parent = self.superclass.instance_variable_get(:@_layout) + @_layout = nil + inspect = parent.is_a?(Proc) ? parent.inspect : parent + + layout_definition = if parent.nil? + name_clause + elsif name + <<-RUBY + if template = lookup_context.find_all("#{_implied_layout_name}", #{prefixes.inspect}).first + ActiveSupport::Deprecation.warn 'Layout found at "#{_implied_layout_name}" for #{name} but parent controller ' \ + 'set layout to #{inspect.inspect}. Please explicitly set your layout to "#{_implied_layout_name}" ' \ + 'or set it to nil to force a dynamic lookup.' + template + else + super + end + RUBY + end + end self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def _layout @@ -283,8 +337,8 @@ module AbstractController #{name_clause} end end + private :_layout RUBY - self.class_eval { private :_layout } end end -- cgit v1.2.3 From cae1768c6a0e3d1cd4a2c2d836ef213438689db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 20 Dec 2011 14:44:48 +0100 Subject: Remove deprecated layout lookup. --- actionpack/lib/abstract_controller/layouts.rb | 72 +++++++++------------------ 1 file changed, 24 insertions(+), 48 deletions(-) (limited to 'actionpack/lib/abstract_controller/layouts.rb') diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index 8b6136d6ba..6a6387632c 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -195,8 +195,9 @@ module AbstractController include Rendering included do - class_attribute :_layout_conditions - remove_possible_method :_layout_conditions + class_attribute :_layout, :_layout_conditions, + :instance_reader => false, :instance_writer => false + self._layout = nil self._layout_conditions = {} _write_layout_method end @@ -254,7 +255,7 @@ module AbstractController conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} } self._layout_conditions = conditions - @_layout = layout || false # Converts nil to false + self._layout = layout _write_layout_method end @@ -281,52 +282,27 @@ module AbstractController RUBY end - if defined?(@_layout) - layout_definition = case @_layout - when String - @_layout.inspect - when Symbol - <<-RUBY - #{@_layout}.tap do |layout| - unless layout.is_a?(String) || !layout - raise ArgumentError, "Your layout method :#{@_layout} returned \#{layout}. It " \ - "should have returned a String, false, or nil" - end + layout_definition = case _layout + when String + _layout.inspect + when Symbol + <<-RUBY + #{_layout}.tap do |layout| + unless layout.is_a?(String) || !layout + raise ArgumentError, "Your layout method :#{_layout} returned \#{layout}. It " \ + "should have returned a String, false, or nil" end - RUBY - when Proc - define_method :_layout_from_proc, &@_layout - "_layout_from_proc(self)" - when false - nil - when true - raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" - when nil - name_clause - end - else - # Add a deprecation if the parent layout was explicitly set and the child - # still does a dynamic lookup. In next Rails release, we should @_layout - # to be inheritable so we can skip the child lookup if the parent explicitly - # set the layout. - parent = self.superclass.instance_variable_get(:@_layout) - @_layout = nil - inspect = parent.is_a?(Proc) ? parent.inspect : parent - - layout_definition = if parent.nil? - name_clause - elsif name - <<-RUBY - if template = lookup_context.find_all("#{_implied_layout_name}", #{prefixes.inspect}).first - ActiveSupport::Deprecation.warn 'Layout found at "#{_implied_layout_name}" for #{name} but parent controller ' \ - 'set layout to #{inspect.inspect}. Please explicitly set your layout to "#{_implied_layout_name}" ' \ - 'or set it to nil to force a dynamic lookup.' - template - else - super - end - RUBY - end + end + RUBY + when Proc + define_method :_layout_from_proc, &_layout + "_layout_from_proc(self)" + when false + nil + when true + raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" + when nil + name_clause end self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 -- cgit v1.2.3