aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/abstract/layouts.rb
blob: 557d68d866766ee1e51e7391b82b5b11ad87698c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module AbstractController
  module Layouts
    extend ActiveSupport::DependencyModule

    depends_on Renderer

    included do
      extlib_inheritable_accessor :_layout_conditions
      self._layout_conditions = {}
    end

    module ClassMethods
      def layout(layout, conditions = {})
        unless [String, Symbol, FalseClass, NilClass].include?(layout.class)
          raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
        end

        conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} }
        self._layout_conditions = conditions
        
        @_layout = layout || false # Converts nil to false
        _write_layout_method
      end
      
      def _implied_layout_name
        name.underscore
      end
      
      # Takes the specified layout and creates a _layout method to be called
      # by _default_layout
      # 
      # 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)
      def _write_layout_method
        case @_layout
        when String
          self.class_eval %{def _layout() #{@_layout.inspect} end}
        when Symbol
          self.class_eval %{def _layout() #{@_layout} end}
        when false
          self.class_eval %{def _layout() end}
        else
          self.class_eval %{
            def _layout
              if view_paths.find_by_parts?("#{_implied_layout_name}", {:formats => formats}, "layouts")
                "#{_implied_layout_name}"
              else
                super
              end
            end
          }
        end
      end
    end
    
    def _render_template(template, options)
      _action_view._render_template_from_controller(template, options[:_layout], options, options[:_partial])
    end
        
  private
  
    def _layout() end # This will be overwritten
    
    # :api: plugin
    # ====
    # Override this to mutate the inbound layout name
    def _layout_for_name(name)
      unless [String, FalseClass, NilClass].include?(name.class)
        raise ArgumentError, "String, false, or nil expected; you passed #{name.inspect}"
      end
      
      name && view_paths.find_by_parts(name, {:formats => formats}, _layout_prefix(name))
    end

    # TODO: Decide if this is the best hook point for the feature
    def _layout_prefix(name)
      "layouts"
    end
    
    def _default_layout(require_layout = false)
      if require_layout && _action_has_layout? && !_layout
        raise ArgumentError,
          "There was no default layout for #{self.class} in #{view_paths.inspect}"
      end

      begin
        _layout_for_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
    end

    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
  end
end