From 032778eefb4439a72c2933ea0bd4a7a0ef776234 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 15 Apr 2015 15:30:27 -0300 Subject: Add ActionController API functionality --- actionpack/lib/action_controller/api.rb | 157 ++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 actionpack/lib/action_controller/api.rb (limited to 'actionpack/lib/action_controller/api.rb') diff --git a/actionpack/lib/action_controller/api.rb b/actionpack/lib/action_controller/api.rb new file mode 100644 index 0000000000..f9cbbf227f --- /dev/null +++ b/actionpack/lib/action_controller/api.rb @@ -0,0 +1,157 @@ +require 'action_view' +require 'action_controller' +require 'action_controller/log_subscriber' + +module ActionController + # API Controller is a lightweight version of ActionController::Base, + # created for applications that don't require all functionality that a complete + # \Rails controller provides, allowing you to create faster controllers for + # example for API only applications. + # + # 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, + # flash, assets, and so on. This makes the entire controller stack thinner and + # faster, suitable for API applications. It doesn't mean you won't have such + # features if you need them: they're all available for you to include in + # your application, they're just not part of the default API Controller stack. + # + # By default, only the ApplicationController in a \Rails application inherits + # from ActionController::API. All other controllers in turn inherit + # from ApplicationController. + # + # A sample controller could look like this: + # + # class PostsController < ApplicationController + # def index + # @posts = Post.all + # render json: @posts + # end + # end + # + # Request, response and parameters objects all work the exact same way as + # ActionController::Base. + # + # == Renders + # + # The default API Controller stack includes all renderers, which means you + # can use render :json and brothers freely in your controllers. Keep + # in mind that templates are not going to be rendered, so you need to ensure + # your controller is calling either render or redirect in + # all actions. + # + # def show + # @post = Post.find(params[:id]) + # render json: @post + # end + # + # == Redirects + # + # Redirects are used to move from one action to another. You can use the + # redirect method in your controllers in the same way as + # ActionController::Base. For example: + # + # def create + # redirect_to root_url and return if not_authorized? + # # do stuff here + # end + # + # == Adding new behavior + # + # In some scenarios you may want to add back some functionality provided by + # ActionController::Base that is not present by default in + # ActionController::API, for instance MimeResponds. This + # module gives you the respond_to and respond_with methods. + # 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 to your entire app: + # + # class ApplicationController < ActionController::API + # include ActionController::MimeResponds + # end + # + # class PostsController < ApplicationController + # respond_to :json, :xml + # + # def index + # @posts = Post.all + # respond_with @posts + # end + # end + # + # Quite straightforward. Make sure to check ActionController::Base + # available modules if you want to include any other functionality that is + # not provided by ActionController::API out of the box. + class API < Metal + abstract! + + module Compatibility + def cache_store; end + def cache_store=(*); end + def assets_dir=(*); end + def javascripts_dir=(*); end + def stylesheets_dir=(*); end + def page_cache_directory=(*); end + def asset_path=(*); end + def asset_host=(*); end + def relative_url_root=(*); end + def perform_caching=(*); end + def helpers_path=(*); end + def allow_forgery_protection=(*); end + def helper_method(*); end + def helper(*); end + end + + extend Compatibility + + # Shortcut helper that returns all the ActionController::API modules except the ones passed in the argument: + # + # class MetalController + # ActionController::API.without_modules(:Redirecting, :DataStreaming).each do |left| + # include left + # end + # end + # + # This gives better control over what you want to exclude and makes it easier + # to create an api controller class, instead of listing the modules required manually. + 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, + ApiRendering, + Renderers::All, + ConditionalGet, + RackDelegation, + StrongParameters, + + ForceSSL, + DataStreaming, + + # Before callbacks should also be executed the earliest as possible, so + # 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. + Instrumentation + ] + + MODULES.each do |mod| + include mod + end + + ActiveSupport.run_load_hooks(:action_controller, self) + end +end -- cgit v1.2.3 From 38818c93a9959118a87bbdeda16e0766adc67598 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 21 Apr 2015 16:35:18 -0400 Subject: Remove api_rendering is not needed --- actionpack/lib/action_controller/api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_controller/api.rb') diff --git a/actionpack/lib/action_controller/api.rb b/actionpack/lib/action_controller/api.rb index f9cbbf227f..2b31ed2cbe 100644 --- a/actionpack/lib/action_controller/api.rb +++ b/actionpack/lib/action_controller/api.rb @@ -127,7 +127,7 @@ module ActionController UrlFor, Redirecting, - ApiRendering, + Rendering, Renderers::All, ConditionalGet, RackDelegation, -- cgit v1.2.3 From fd2508522c341c3f708219b5fc1834f24caf04e3 Mon Sep 17 00:00:00 2001 From: Jorge Bejar Date: Tue, 5 May 2015 14:50:41 -0300 Subject: Remove Compatibility module since we don't remember why it was added :smile: --- actionpack/lib/action_controller/api.rb | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'actionpack/lib/action_controller/api.rb') diff --git a/actionpack/lib/action_controller/api.rb b/actionpack/lib/action_controller/api.rb index 2b31ed2cbe..ff199567b9 100644 --- a/actionpack/lib/action_controller/api.rb +++ b/actionpack/lib/action_controller/api.rb @@ -85,25 +85,6 @@ module ActionController class API < Metal abstract! - module Compatibility - def cache_store; end - def cache_store=(*); end - def assets_dir=(*); end - def javascripts_dir=(*); end - def stylesheets_dir=(*); end - def page_cache_directory=(*); end - def asset_path=(*); end - def asset_host=(*); end - def relative_url_root=(*); end - def perform_caching=(*); end - def helpers_path=(*); end - def allow_forgery_protection=(*); end - def helper_method(*); end - def helper(*); end - end - - extend Compatibility - # Shortcut helper that returns all the ActionController::API modules except the ones passed in the argument: # # class MetalController -- cgit v1.2.3 From 2487bfb39a8ef5e18fd9cc8f971395139e7c727c Mon Sep 17 00:00:00 2001 From: Jorge Bejar Date: Tue, 5 May 2015 15:08:21 -0300 Subject: Do not say that Api Controllers are faster than regular ones in docs --- actionpack/lib/action_controller/api.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_controller/api.rb') diff --git a/actionpack/lib/action_controller/api.rb b/actionpack/lib/action_controller/api.rb index ff199567b9..cb819067da 100644 --- a/actionpack/lib/action_controller/api.rb +++ b/actionpack/lib/action_controller/api.rb @@ -5,14 +5,14 @@ require 'action_controller/log_subscriber' module ActionController # API Controller is a lightweight version of ActionController::Base, # created for applications that don't require all functionality that a complete - # \Rails controller provides, allowing you to create faster controllers for - # example for API only applications. + # \Rails controller provides, allowing you to create controllers with just the + # features that you need for API only applications. # # 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, - # flash, assets, and so on. This makes the entire controller stack thinner and - # faster, suitable for API applications. It doesn't mean you won't have such + # 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 # features if you need them: they're all available for you to include in # your application, they're just not part of the default API Controller stack. # -- cgit v1.2.3 From 7db63f3d35577e457250ba68d7a77651b2feb362 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 13 May 2015 14:54:12 -0300 Subject: Fix MimeResponds example in AC::API documentation --- actionpack/lib/action_controller/api.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_controller/api.rb') diff --git a/actionpack/lib/action_controller/api.rb b/actionpack/lib/action_controller/api.rb index cb819067da..9dc96bd3e6 100644 --- a/actionpack/lib/action_controller/api.rb +++ b/actionpack/lib/action_controller/api.rb @@ -71,11 +71,13 @@ module ActionController # end # # class PostsController < ApplicationController - # respond_to :json, :xml - # # def index # @posts = Post.all - # respond_with @posts + # + # respond_to do |format| + # format.json { render json: @posts } + # format.xml { render xml: @posts } + # end # end # end # -- cgit v1.2.3 From a2c9a7308474bc1d7fc35df9560c46125d94f5a4 Mon Sep 17 00:00:00 2001 From: Jorge Bejar Date: Tue, 2 Jun 2015 15:48:20 -0300 Subject: Include ParamsWrapper in AC::API ParamsWrapper was initially removed from API controllers according to the following discusision: https://github.com/rails-api/rails-api/issues/33 However, we're including it again so Rails API devs can decide whether to enable or disable it. --- actionpack/lib/action_controller/api.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_controller/api.rb') diff --git a/actionpack/lib/action_controller/api.rb b/actionpack/lib/action_controller/api.rb index 9dc96bd3e6..b4583a073b 100644 --- a/actionpack/lib/action_controller/api.rb +++ b/actionpack/lib/action_controller/api.rb @@ -128,7 +128,11 @@ module ActionController # Add instrumentations hooks at the bottom, to ensure they instrument # all the methods properly. - Instrumentation + Instrumentation, + + # Params wrapper should come before instrumentation so they are + # properly showed in logs + ParamsWrapper ] MODULES.each do |mod| -- cgit v1.2.3 From 6c165773117dc7e60f5bb4762e58c7c522d69fcc Mon Sep 17 00:00:00 2001 From: Jorge Bejar Date: Tue, 2 Jun 2015 17:12:50 -0300 Subject: Return 204 if render is not called in API controllers --- actionpack/lib/action_controller/api.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack/lib/action_controller/api.rb') diff --git a/actionpack/lib/action_controller/api.rb b/actionpack/lib/action_controller/api.rb index b4583a073b..6fab19296d 100644 --- a/actionpack/lib/action_controller/api.rb +++ b/actionpack/lib/action_controller/api.rb @@ -114,6 +114,7 @@ module ActionController Renderers::All, ConditionalGet, RackDelegation, + BasicImplicitRender, StrongParameters, ForceSSL, -- cgit v1.2.3 From 1fd42f33385fb2b6647d2d43faec0399e2e3118c Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 5 Jun 2015 16:04:07 -0300 Subject: Mention that doing nothing in Rails API controllers returns 204 --- actionpack/lib/action_controller/api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_controller/api.rb') diff --git a/actionpack/lib/action_controller/api.rb b/actionpack/lib/action_controller/api.rb index 6fab19296d..d8149e0232 100644 --- a/actionpack/lib/action_controller/api.rb +++ b/actionpack/lib/action_controller/api.rb @@ -38,7 +38,7 @@ module ActionController # can use render :json and brothers freely in your controllers. Keep # in mind that templates are not going to be rendered, so you need to ensure # your controller is calling either render or redirect in - # all actions. + # all actions, otherwise it will return 204 No Content response. # # def show # @post = Post.find(params[:id]) -- cgit v1.2.3