2016-08-06 12:51:43 -04:00
|
|
|
require "action_view"
|
|
|
|
require "action_controller"
|
|
|
|
require "action_controller/log_subscriber"
|
2015-04-15 14:30:27 -04:00
|
|
|
|
|
|
|
module ActionController
|
|
|
|
# API Controller is a lightweight version of <tt>ActionController::Base</tt>,
|
2015-06-15 07:27:38 -04:00
|
|
|
# created for applications that don't require all functionalities that a complete
|
2015-05-05 14:08:21 -04:00
|
|
|
# \Rails controller provides, allowing you to create controllers with just the
|
|
|
|
# features that you need for API only applications.
|
2015-04-15 14:30:27 -04:00
|
|
|
#
|
|
|
|
# An API Controller is different from a normal controller in the sense that
|
|
|
|
# by default it doesn't include a number of features that are usually required
|
|
|
|
# by browser access only: layouts and templates rendering, cookies, sessions,
|
2015-05-05 14:08:21 -04:00
|
|
|
# flash, assets, and so on. This makes the entire controller stack thinner,
|
|
|
|
# suitable for API applications. It doesn't mean you won't have such
|
2015-04-15 14:30:27 -04:00
|
|
|
# features if you need them: they're all available for you to include in
|
2016-04-05 11:05:09 -04:00
|
|
|
# your application, they're just not part of the default API controller stack.
|
2015-04-15 14:30:27 -04:00
|
|
|
#
|
2016-04-05 11:05:09 -04:00
|
|
|
# Normally, +ApplicationController+ is the only controller that inherits from
|
|
|
|
# <tt>ActionController::API</tt>. All other controllers in turn inherit from
|
|
|
|
# +ApplicationController+.
|
2015-04-15 14:30:27 -04:00
|
|
|
#
|
|
|
|
# A sample controller could look like this:
|
|
|
|
#
|
|
|
|
# class PostsController < ApplicationController
|
|
|
|
# def index
|
2016-04-05 11:05:09 -04:00
|
|
|
# posts = Post.all
|
|
|
|
# render json: posts
|
2015-04-15 14:30:27 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2016-04-05 11:05:09 -04:00
|
|
|
# Request, response, and parameters objects all work the exact same way as
|
2015-04-15 14:30:27 -04:00
|
|
|
# <tt>ActionController::Base</tt>.
|
|
|
|
#
|
|
|
|
# == Renders
|
|
|
|
#
|
|
|
|
# The default API Controller stack includes all renderers, which means you
|
|
|
|
# can use <tt>render :json</tt> and brothers freely in your controllers. Keep
|
|
|
|
# in mind that templates are not going to be rendered, so you need to ensure
|
2016-04-05 11:05:09 -04:00
|
|
|
# your controller is calling either <tt>render</tt> or <tt>redirect_to</tt> in
|
|
|
|
# all actions, otherwise it will return 204 No Content.
|
2015-04-15 14:30:27 -04:00
|
|
|
#
|
|
|
|
# def show
|
2016-04-05 11:05:09 -04:00
|
|
|
# post = Post.find(params[:id])
|
|
|
|
# render json: post
|
2015-04-15 14:30:27 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# == Redirects
|
|
|
|
#
|
|
|
|
# Redirects are used to move from one action to another. You can use the
|
2016-04-05 11:05:09 -04:00
|
|
|
# <tt>redirect_to</tt> method in your controllers in the same way as in
|
2015-04-15 14:30:27 -04:00
|
|
|
# <tt>ActionController::Base</tt>. For example:
|
|
|
|
#
|
|
|
|
# def create
|
|
|
|
# redirect_to root_url and return if not_authorized?
|
|
|
|
# # do stuff here
|
|
|
|
# end
|
|
|
|
#
|
2016-04-05 11:05:09 -04:00
|
|
|
# == Adding New Behavior
|
2015-04-15 14:30:27 -04:00
|
|
|
#
|
|
|
|
# In some scenarios you may want to add back some functionality provided by
|
|
|
|
# <tt>ActionController::Base</tt> that is not present by default in
|
|
|
|
# <tt>ActionController::API</tt>, for instance <tt>MimeResponds</tt>. This
|
2015-06-15 07:27:38 -04:00
|
|
|
# module gives you the <tt>respond_to</tt> method. Adding it is quite simple,
|
|
|
|
# you just need to include the module in a specific controller or in
|
|
|
|
# +ApplicationController+ in case you want it available in your entire
|
|
|
|
# application:
|
2015-04-15 14:30:27 -04:00
|
|
|
#
|
|
|
|
# class ApplicationController < ActionController::API
|
|
|
|
# include ActionController::MimeResponds
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# class PostsController < ApplicationController
|
|
|
|
# def index
|
2016-04-05 11:05:09 -04:00
|
|
|
# posts = Post.all
|
2015-05-13 13:54:12 -04:00
|
|
|
#
|
|
|
|
# respond_to do |format|
|
2016-04-05 11:05:09 -04:00
|
|
|
# format.json { render json: posts }
|
|
|
|
# format.xml { render xml: posts }
|
2015-05-13 13:54:12 -04:00
|
|
|
# end
|
2015-04-15 14:30:27 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2016-04-05 11:05:09 -04:00
|
|
|
# Quite straightforward. Make sure to check the modules included in
|
|
|
|
# <tt>ActionController::Base</tt> if you want to use any other
|
|
|
|
# functionality that is not provided by <tt>ActionController::API</tt>
|
|
|
|
# out of the box.
|
2015-04-15 14:30:27 -04:00
|
|
|
class API < Metal
|
|
|
|
abstract!
|
|
|
|
|
2015-06-15 07:27:38 -04:00
|
|
|
# Shortcut helper that returns all the ActionController::API modules except
|
|
|
|
# the ones passed as arguments:
|
2015-04-15 14:30:27 -04:00
|
|
|
#
|
2015-08-01 06:20:20 -04:00
|
|
|
# class MyAPIBaseController < ActionController::Metal
|
2015-06-15 07:27:38 -04:00
|
|
|
# ActionController::API.without_modules(:ForceSSL, :UrlFor).each do |left|
|
2015-04-15 14:30:27 -04:00
|
|
|
# include left
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This gives better control over what you want to exclude and makes it easier
|
2015-06-15 07:27:38 -04:00
|
|
|
# to create an API controller class, instead of listing the modules required
|
|
|
|
# manually.
|
2015-04-15 14:30:27 -04:00
|
|
|
def self.without_modules(*modules)
|
|
|
|
modules = modules.map do |m|
|
|
|
|
m.is_a?(Symbol) ? ActionController.const_get(m) : m
|
|
|
|
end
|
|
|
|
|
|
|
|
MODULES - modules
|
|
|
|
end
|
|
|
|
|
|
|
|
MODULES = [
|
|
|
|
AbstractController::Rendering,
|
|
|
|
|
|
|
|
UrlFor,
|
|
|
|
Redirecting,
|
2016-01-20 19:16:23 -05:00
|
|
|
ApiRendering,
|
2015-04-15 14:30:27 -04:00
|
|
|
Renderers::All,
|
|
|
|
ConditionalGet,
|
2015-06-02 16:12:50 -04:00
|
|
|
BasicImplicitRender,
|
2015-04-15 14:30:27 -04:00
|
|
|
StrongParameters,
|
|
|
|
|
|
|
|
ForceSSL,
|
|
|
|
DataStreaming,
|
|
|
|
|
2015-06-15 07:27:38 -04:00
|
|
|
# Before callbacks should also be executed as early as possible, so
|
2015-04-15 14:30:27 -04:00
|
|
|
# also include them at the bottom.
|
|
|
|
AbstractController::Callbacks,
|
|
|
|
|
|
|
|
# Append rescue at the bottom to wrap as much as possible.
|
|
|
|
Rescue,
|
|
|
|
|
|
|
|
# Add instrumentations hooks at the bottom, to ensure they instrument
|
|
|
|
# all the methods properly.
|
2015-06-02 14:48:20 -04:00
|
|
|
Instrumentation,
|
|
|
|
|
|
|
|
# Params wrapper should come before instrumentation so they are
|
|
|
|
# properly showed in logs
|
|
|
|
ParamsWrapper
|
2015-04-15 14:30:27 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
MODULES.each do |mod|
|
|
|
|
include mod
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveSupport.run_load_hooks(:action_controller, self)
|
|
|
|
end
|
|
|
|
end
|