Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 180 lines (162 sloc) 5.842 kB
1fa9266 @spastorino Revert "Merge pull request #120 from strzalek/unneeded-require"
spastorino authored
1 require 'action_view'
1bc322b @spastorino Initial commit
spastorino authored
2 require 'action_controller'
cf2364a @spastorino Remove Rails::ApiApplication, just configure Rails::Application
spastorino authored
3 require 'action_controller/log_subscriber'
1bc322b @spastorino Initial commit
spastorino authored
4
5 module ActionController
51008d4 @spastorino Rename to rails-api
spastorino authored
6 # API Controller is a lightweight version of <tt>ActionController::Base</tt>,
1bc322b @spastorino Initial commit
spastorino authored
7 # created for applications that don't require all functionality that a complete
51008d4 @spastorino Rename to rails-api
spastorino authored
8 # \Rails controller provides, allowing you to create faster controllers for
9 # example for API only applications.
1bc322b @spastorino Initial commit
spastorino authored
10 #
51008d4 @spastorino Rename to rails-api
spastorino authored
11 # An API Controller is different from a normal controller in the sense that
1bc322b @spastorino Initial commit
spastorino authored
12 # by default it doesn't include a number of features that are usually required
13 # by browser access only: layouts and templates rendering, cookies, sessions,
14 # flash, assets, and so on. This makes the entire controller stack thinner and
15 # faster, suitable for API applications. It doesn't mean you won't have such
16 # features if you need them: they're all available for you to include in
51008d4 @spastorino Rename to rails-api
spastorino authored
17 # your application, they're just not part of the default API Controller stack.
1bc322b @spastorino Initial commit
spastorino authored
18 #
19 # By default, only the ApplicationController in a \Rails application inherits
51008d4 @spastorino Rename to rails-api
spastorino authored
20 # from <tt>ActionController::API</tt>. All other controllers in turn inherit
1bc322b @spastorino Initial commit
spastorino authored
21 # from ApplicationController.
22 #
23 # A sample controller could look like this:
24 #
25 # class PostsController < ApplicationController
26 # def index
27 # @posts = Post.all
28 # render json: @posts
29 # end
30 # end
31 #
32 # Request, response and parameters objects all work the exact same way as
33 # <tt>ActionController::Base</tt>.
34 #
35 # == Renders
36 #
51008d4 @spastorino Rename to rails-api
spastorino authored
37 # The default API Controller stack includes all renderers, which means you
1bc322b @spastorino Initial commit
spastorino authored
38 # can use <tt>render :json</tt> and brothers freely in your controllers. Keep
39 # in mind that templates are not going to be rendered, so you need to ensure
40 # your controller is calling either <tt>render</tt> or <tt>redirect</tt> in
41 # all actions.
42 #
43 # def show
44 # @post = Post.find(params[:id])
45 # render json: @post
46 # end
47 #
48 # == Redirects
49 #
50 # Redirects are used to move from one action to another. You can use the
51 # <tt>redirect</tt> method in your controllers in the same way as
52 # <tt>ActionController::Base</tt>. For example:
53 #
54 # def create
55 # redirect_to root_url and return if not_authorized?
56 # # do stuff here
57 # end
58 #
59 # == Adding new behavior
60 #
61 # In some scenarios you may want to add back some functionality provided by
62 # <tt>ActionController::Base</tt> that is not present by default in
51008d4 @spastorino Rename to rails-api
spastorino authored
63 # <tt>ActionController::API</tt>, for instance <tt>MimeResponds</tt>. This
1bc322b @spastorino Initial commit
spastorino authored
64 # module gives you the <tt>respond_to</tt> and <tt>respond_with</tt> methods.
65 # Adding it is quite simple, you just need to include the module in a specific
66 # controller or in <tt>ApplicationController</tt> in case you want it
67 # available to your entire app:
68 #
51008d4 @spastorino Rename to rails-api
spastorino authored
69 # class ApplicationController < ActionController::API
1bc322b @spastorino Initial commit
spastorino authored
70 # include ActionController::MimeResponds
71 # end
72 #
73 # class PostsController < ApplicationController
74 # respond_to :json, :xml
75 #
76 # def index
77 # @posts = Post.all
78 # respond_with @posts
79 # end
80 # end
81 #
82 # Quite straightforward. Make sure to check <tt>ActionController::Base</tt>
83 # available modules if you want to include any other functionality that is
51008d4 @spastorino Rename to rails-api
spastorino authored
84 # not provided by <tt>ActionController::API</tt> out of the box.
85 class API < Metal
1bc322b @spastorino Initial commit
spastorino authored
86 abstract!
87
862a093 @knoopx Renamed "Compabitility" to "Compatibility"
knoopx authored
88 module Compatibility
1bc322b @spastorino Initial commit
spastorino authored
89 def cache_store; end
90 def cache_store=(*); end
91 def assets_dir=(*); end
92 def javascripts_dir=(*); end
93 def stylesheets_dir=(*); end
94 def page_cache_directory=(*); end
95 def asset_path=(*); end
96 def asset_host=(*); end
97 def relative_url_root=(*); end
98 def perform_caching=(*); end
99 def helpers_path=(*); end
100 def allow_forgery_protection=(*); end
eceb531 @educobuci added compatibility to 'helper_method'
educobuci authored
101 def helper_method(*); end
fb1d9fc @wintery Added Devise helper method stub to Compatibility
wintery authored
102 def helper(*); end
1bc322b @spastorino Initial commit
spastorino authored
103 end
104
862a093 @knoopx Renamed "Compabitility" to "Compatibility"
knoopx authored
105 extend Compatibility
1bc322b @spastorino Initial commit
spastorino authored
106
51008d4 @spastorino Rename to rails-api
spastorino authored
107 # Shortcut helper that returns all the ActionController::API modules except the ones passed in the argument:
1bc322b @spastorino Initial commit
spastorino authored
108 #
109 # class MetalController
51008d4 @spastorino Rename to rails-api
spastorino authored
110 # ActionController::API.without_modules(:Redirecting, :DataStreaming).each do |left|
1bc322b @spastorino Initial commit
spastorino authored
111 # include left
112 # end
113 # end
114 #
115 # This gives better control over what you want to exclude and makes it easier
51008d4 @spastorino Rename to rails-api
spastorino authored
116 # to create an api controller class, instead of listing the modules required manually.
1bc322b @spastorino Initial commit
spastorino authored
117 def self.without_modules(*modules)
118 modules = modules.map do |m|
119 m.is_a?(Symbol) ? ActionController.const_get(m) : m
120 end
121
122 MODULES - modules
123 end
124
125 MODULES = [
126 UrlFor,
127 Redirecting,
128 Rendering,
129 Renderers::All,
130 ConditionalGet,
131 RackDelegation,
132
133 ForceSSL,
134 DataStreaming,
135
136 # Before callbacks should also be executed the earliest as possible, so
137 # also include them at the bottom.
138 AbstractController::Callbacks,
139
140 # Append rescue at the bottom to wrap as much as possible.
141 Rescue,
142
143 # Add instrumentations hooks at the bottom, to ensure they instrument
144 # all the methods properly.
145 Instrumentation
d13821b @adomokos Freeze the array that is not supposed to change
adomokos authored
146 ].freeze
1bc322b @spastorino Initial commit
spastorino authored
147
1409e41 @alexey-chebotar * ActionDispatch::Head replaced by Rack::Head;
alexey-chebotar authored
148 if Rails::VERSION::MAJOR == 5 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR > 0)
af9de70 @bmorton Fix required modules due to changes in ActionView and rendering in Ra…
bmorton authored
149 include AbstractController::Rendering
150 include ActionView::Rendering
151 end
152
5817bd3 @alexey-chebotar Remove ActionController::HideActions
alexey-chebotar authored
153 if Rails::VERSION::MAJOR < 5
154 include ActionController::HideActions
155 end
156
1bc322b @spastorino Initial commit
spastorino authored
157 MODULES.each do |mod|
158 include mod
159 end
944e5ed @spastorino Check Rails major >= 4
spastorino authored
160
e277b3b @j-wilkins protected_instance_variables is compatible with Rails < 4.1
j-wilkins authored
161 DEFAULT_PROTECTED_INSTANCE_VARIABLES = begin
1409e41 @alexey-chebotar * ActionDispatch::Head replaced by Rack::Head;
alexey-chebotar authored
162 if Rails::VERSION::MAJOR == 5 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR > 0)
163 Set
164 else
165 Array
166 end
e277b3b @j-wilkins protected_instance_variables is compatible with Rails < 4.1
j-wilkins authored
167 end.new
168
591fcfc Adds protected_instance_variables to ActionController::API for compat…
Edward Dowling authored
169 def self.protected_instance_variables
e277b3b @j-wilkins protected_instance_variables is compatible with Rails < 4.1
j-wilkins authored
170 DEFAULT_PROTECTED_INSTANCE_VARIABLES
591fcfc Adds protected_instance_variables to ActionController::API for compat…
Edward Dowling authored
171 end
172
944e5ed @spastorino Check Rails major >= 4
spastorino authored
173 if Rails::VERSION::MAJOR >= 4
4197012 strong parameters fix, I think
J-P Teti authored
174 include StrongParameters
175 end
1bc322b @spastorino Initial commit
spastorino authored
176
177 ActiveSupport.run_load_hooks(:action_controller, self)
178 end
179 end
Something went wrong with that request. Please try again.