2009-02-27 14:42:13 -05:00
|
|
|
module AbstractController
|
|
|
|
module Layouts
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-07 11:29:22 -04:00
|
|
|
|
2009-05-29 18:03:23 -04:00
|
|
|
include Renderer
|
2009-05-07 11:29:22 -04:00
|
|
|
|
2009-05-21 21:14:20 -04:00
|
|
|
included do
|
|
|
|
extlib_inheritable_accessor :_layout_conditions
|
|
|
|
self._layout_conditions = {}
|
|
|
|
end
|
|
|
|
|
2009-03-23 18:45:01 -04:00
|
|
|
module ClassMethods
|
2009-05-21 21:14:20 -04:00
|
|
|
def layout(layout, conditions = {})
|
2009-03-23 21:06:47 -04:00
|
|
|
unless [String, Symbol, FalseClass, NilClass].include?(layout.class)
|
|
|
|
raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
|
|
|
|
end
|
2009-05-21 21:14:20 -04:00
|
|
|
|
|
|
|
conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} }
|
|
|
|
self._layout_conditions = conditions
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-04-07 17:57:18 -04:00
|
|
|
@_layout = layout || false # Converts nil to false
|
|
|
|
_write_layout_method
|
2009-03-23 21:06:47 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-04-07 18:54:02 -04:00
|
|
|
def _implied_layout_name
|
|
|
|
name.underscore
|
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-04-07 20:57:20 -04:00
|
|
|
# Takes the specified layout and creates a _layout method to be called
|
|
|
|
# by _default_layout
|
2009-05-28 10:49:02 -04:00
|
|
|
#
|
2009-04-07 20:57:20 -04:00
|
|
|
# If the specified layout is a:
|
|
|
|
# String:: return the string
|
|
|
|
# Symbol:: call the method specified by the symbol
|
|
|
|
# false:: return nil
|
|
|
|
# none:: If a layout is found in the view paths with the controller's
|
|
|
|
# name, return that string. Otherwise, use the superclass'
|
|
|
|
# layout (which might also be implied)
|
2009-03-23 21:06:47 -04:00
|
|
|
def _write_layout_method
|
2009-04-07 17:57:18 -04:00
|
|
|
case @_layout
|
2009-03-23 21:06:47 -04:00
|
|
|
when String
|
2009-05-23 03:39:32 -04:00
|
|
|
self.class_eval %{def _layout(details) #{@_layout.inspect} end}
|
2009-03-23 21:06:47 -04:00
|
|
|
when Symbol
|
2009-05-23 03:39:32 -04:00
|
|
|
self.class_eval %{def _layout(details) #{@_layout} end}
|
2009-03-23 21:06:47 -04:00
|
|
|
when false
|
2009-05-23 03:39:32 -04:00
|
|
|
self.class_eval %{def _layout(details) end}
|
2009-03-23 21:06:47 -04:00
|
|
|
else
|
|
|
|
self.class_eval %{
|
2009-05-23 03:39:32 -04:00
|
|
|
def _layout(details)
|
|
|
|
if view_paths.find_by_parts?("#{_implied_layout_name}", details, "layouts")
|
2009-04-07 18:54:02 -04:00
|
|
|
"#{_implied_layout_name}"
|
2009-03-23 21:06:47 -04:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2009-03-23 18:45:01 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-03-23 18:45:01 -04:00
|
|
|
private
|
2009-05-28 10:49:02 -04:00
|
|
|
# This will be overwritten
|
|
|
|
def _layout(details)
|
|
|
|
end
|
|
|
|
|
2009-05-01 19:02:37 -04:00
|
|
|
# :api: plugin
|
|
|
|
# ====
|
|
|
|
# Override this to mutate the inbound layout name
|
2009-05-22 18:16:28 -04:00
|
|
|
def _layout_for_name(name, details = {:formats => formats})
|
2009-04-07 20:57:20 -04:00
|
|
|
unless [String, FalseClass, NilClass].include?(name.class)
|
|
|
|
raise ArgumentError, "String, false, or nil expected; you passed #{name.inspect}"
|
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-22 18:16:28 -04:00
|
|
|
name && view_paths.find_by_parts(name, details, _layout_prefix(name))
|
2009-05-20 18:33:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Decide if this is the best hook point for the feature
|
|
|
|
def _layout_prefix(name)
|
|
|
|
"layouts"
|
2009-03-23 18:45:01 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-22 18:16:28 -04:00
|
|
|
def _default_layout(require_layout = false, details = {:formats => formats})
|
2009-05-23 03:39:32 -04:00
|
|
|
if require_layout && _action_has_layout? && !_layout(details)
|
2009-04-27 21:21:26 -04:00
|
|
|
raise ArgumentError,
|
2009-04-07 20:57:20 -04:00
|
|
|
"There was no default layout for #{self.class} in #{view_paths.inspect}"
|
|
|
|
end
|
2009-04-27 21:21:26 -04:00
|
|
|
|
2009-04-07 18:54:02 -04:00
|
|
|
begin
|
2009-05-23 03:39:32 -04:00
|
|
|
_layout_for_name(_layout(details), details) if _action_has_layout?
|
2009-04-07 18:54:02 -04:00
|
|
|
rescue NameError => e
|
2009-05-28 10:49:02 -04:00
|
|
|
raise NoMethodError,
|
2009-04-07 18:54:02 -04:00
|
|
|
"You specified #{@_layout.inspect} as the layout, but no such method was found"
|
|
|
|
end
|
2009-03-23 18:45:01 -04:00
|
|
|
end
|
2009-05-21 21:14:20 -04:00
|
|
|
|
|
|
|
def _action_has_layout?
|
|
|
|
conditions = _layout_conditions
|
|
|
|
if only = conditions[:only]
|
|
|
|
only.include?(action_name)
|
|
|
|
elsif except = conditions[:except]
|
|
|
|
!except.include?(action_name)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
end
|