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
Y
Yehuda Katz 已提交
18
    include ActionController::Configuration
19 20 21 22 23 24 25

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

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

    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
43
        default_render unless response_body
44 45 46 47 48
        ret
      end

      def default_render
        render
49
      end
50

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

60
    include ImplicitRender
61

62
    include ActionController::Rescue
63

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

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

73 74 75 76 77 78 79 80
    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 已提交
81
        end
82 83 84
        options.merge! key => action
      elsif action
        options.merge! :partial => action
D
Initial  
David Heinemeier Hansson 已提交
85
      end
86

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

91
      if options[:status]
92
        options[:status] = Rack::Utils.status_code(options[:status])
93
      end
D
Initial  
David Heinemeier Hansson 已提交
94

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

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

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