base.rb 3.0 KB
Newer Older
1
module ActionController
2
  class Base < Metal
3 4 5 6 7 8 9 10
    abstract!

    include AbstractController::Callbacks
    include AbstractController::Logger

    include ActionController::Helpers
    include ActionController::HideActions
    include ActionController::UrlFor
11
    include ActionController::Redirecting
12
    include ActionController::Rendering
13
    include ActionController::Renderers::All
14 15
    include ActionController::Layouts
    include ActionController::ConditionalGet
16
    include ActionController::RackDelegation
17
    include ActionController::Logger
18
    include ActionController::Benchmarking
Y
Yehuda Katz 已提交
19
    include ActionController::Configuration
20 21 22 23 24 25 26

    # Legacy modules
    include SessionManagement
    include ActionController::Caching
    include ActionController::MimeResponds

    # Rails 2.x compatibility
C
Carlhuda 已提交
27
    include ActionController::Compatibility
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    include ActionController::Cookies
    include ActionController::Flash
    include ActionController::Verification
    include ActionController::RequestForgeryProtection
    include ActionController::Streaming
    include ActionController::HttpAuthentication::Basic::ControllerMethods
    include ActionController::HttpAuthentication::Digest::ControllerMethods
    include ActionController::FilterParameterLogging
    include ActionController::Translation

    # TODO: Extract into its own module
    # This should be moved together with other normalizing behavior
    module ImplicitRender
      def send_action(*)
        ret = super
44
        default_render unless response_body
45 46 47 48 49
        ret
      end

      def default_render
        render
50
      end
51

52 53
      def method_for_action(action_name)
        super || begin
54
          if template_exists?(action_name.to_s, {:formats => formats}, :_prefix => controller_path)
55 56
            "default_render"
          end
57 58
        end
      end
D
Initial  
David Heinemeier Hansson 已提交
59 60
    end

61
    include ImplicitRender
62

63
    include ActionController::Rescue
64

65 66 67 68
    def self.inherited(klass)
      ::ActionController::Base.subclasses << klass.to_s
      super
    end
69

70 71 72
    def self.subclasses
      @subclasses ||= []
    end
73

74 75 76 77 78 79 80 81
    def _normalize_options(action = nil, options = {}, &blk)
      if action.is_a?(Hash)
        options, action = action, nil
      elsif action.is_a?(String) || action.is_a?(Symbol)
        key = case action = action.to_s
        when %r{^/} then :file
        when %r{/}  then :template
        else             :action
D
Initial  
David Heinemeier Hansson 已提交
82
        end
83 84 85
        options.merge! key => action
      elsif action
        options.merge! :partial => action
D
Initial  
David Heinemeier Hansson 已提交
86
      end
87

88 89
      if options.key?(:action) && options[:action].to_s.index("/")
        options[:template] = options.delete(:action)
90
      end
D
Initial  
David Heinemeier Hansson 已提交
91

92
      if options[:status]
93
        options[:status] = ActionDispatch::StatusCodes[options[:status]]
94
      end
D
Initial  
David Heinemeier Hansson 已提交
95

96 97 98
      options[:update] = blk if block_given?
      options
    end
99

100 101 102 103
    def render(action = nil, options = {}, &blk)
      options = _normalize_options(action, options, &blk)
      super(options)
    end
104

105 106 107 108
    def render_to_string(action = nil, options = {}, &blk)
      options = _normalize_options(action, options, &blk)
      super(options)
    end
109
  end
J
Jeremy Kemper 已提交
110
end