aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/abstract/layouts.rb
blob: 20c9abb9e500c111a9fdf8cf65ec48c6a865f5eb (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
module AbstractController
  module Layouts
    
    def self.included(base)
      base.extend ClassMethods
    end
    
    module ClassMethods
      def layout(layout)
        unless [String, Symbol, FalseClass, NilClass].include?(layout.class)
          raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
        end
        
        @layout = layout || false # Converts nil to false
      end
      
      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?("#{controller_path}", formats, "layouts")
                "#{controller_path}"
              else
                super
              end
            end
          }
        end
      end
    end
    
    def _render_template(template, options)
      _action_view._render_template_with_layout(template, options[:_layout])
    end
        
  private
  
    def _layout() end # This will be overwritten
    
    def _layout_for_option(name)
      case name
      when String then _layout_for_name(name)
      when true   then _default_layout(true)
      when false  then nil
      end
    end
    
    def _layout_for_name(name)
      view_paths.find_by_parts(name, formats, "layouts")
    end
    
    def _default_layout(require_layout = false)
      _layout_for_option(_layout)
    end
  end
end