Class: Merb::Controller
- Defined in:
- lib/merb-core/controller/merb_controller.rb
Direct Known Subclasses
Constant Summary
Constants included from ResponderMixin
ResponderMixin::MIMES, ResponderMixin::TYPES
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
:api: public.
-
#request ⇒ Object
readonly
:api: public.
Class Method Summary collapse
-
._callable_methods ⇒ Object
All methods that are callable as actions.
-
._filter_params(params) ⇒ Object
This is a stub method so plugins can implement param filtering if they want.
-
.abstract! ⇒ Object
Sets a controller to be “abstract” This controller will not be able to be routed to and is used for super classing only.
-
.abstract? ⇒ Boolean
Asks a controller if it is abstract.
-
.callable_actions ⇒ Object
The list of actions that are callable, after taking defaults, _hidden_actions and _shown_actions into consideration.
-
.hide_action(*names) ⇒ Object
Hide each of the given methods from being callable as actions.
-
.inherited(klass) ⇒ Object
Parameters klass<Merb::Controller>:: The Merb::Controller inheriting from the base class.
-
.show_action(*names) ⇒ Object
Makes each of the given methods being callable as actions.
-
.subclasses_list ⇒ Object
:api: private.
Instance Method Summary collapse
-
#_absolute_template_location(template, type) ⇒ Object
The location to look for a template and mime-type.
-
#_dispatch(action = :index) ⇒ Object
Dispatch the action.
-
#_template_location(context, type, controller) ⇒ Object
The location to look for a template for a particular controller, context, and mime-type.
-
#absolute_url(*args) ⇒ Object
Returns the absolute url including the passed protocol and host.
-
#initialize(request, status = 200, headers = {'Content-Type' => 'text/html; charset=utf-8'}) ⇒ Controller
constructor
Build a new controller.
-
#params ⇒ Object
Returns Hash:: The parameters from the request object.
-
#rack_response ⇒ Object
The results of the controller’s render, to be returned to Rack.
-
#resource(*args) ⇒ Object
Generates a URL for a single or nested resource.
-
#status ⇒ Object
Returns Fixnum:: The response status code.
-
#status=(s) ⇒ Object
Set the response status code.
-
#url(name, *args) ⇒ Object
(also: #relative_url)
There are three possible ways to use this method.
Methods included from ConditionalGetMixin
#etag, #etag=, #etag_matches?, #last_modified, #last_modified=, #not_modified?, #request_fresh?
Methods included from AuthenticationMixin
Methods included from ControllerMixin
#delete_cookie, #escape_xml, #message, #nginx_send_file, #redirect, #render_chunked, #render_deferred, #render_then_call, #run_later, #send_chunk, #send_data, #send_file, #set_cookie, #stream_file
Methods included from ResponderMixin
#_perform_content_negotiation, #_provided_formats, #content_type, #content_type=, #does_not_provide, included, #only_provides, #provides
Constructor Details
#initialize(request, status = 200, headers = {'Content-Type' => 'text/html; charset=utf-8'}) ⇒ Controller
Build a new controller.
Sets the variables that came in through the dispatch as available to the controller.
Parameters
- request<Merb::Request>
-
The Merb::Request that came in from Rack.
- status<Integer>
-
An integer code for the status. Defaults to 200.
- headers<Hash=> value>
-
A hash of headers to start the controller with. These headers can be overridden later by the #headers method.
:api: plugin
170 171 172 173 |
# File 'lib/merb-core/controller/merb_controller.rb', line 170 def initialize(request, status=200, headers={'Content-Type' => 'text/html; charset=utf-8'}) super() @request, @_status, @headers = request, status, headers end |
Instance Attribute Details
#headers ⇒ Object (readonly)
:api: public
200 201 202 |
# File 'lib/merb-core/controller/merb_controller.rb', line 200 def headers @headers end |
#request ⇒ Object (readonly)
:api: public
200 201 202 |
# File 'lib/merb-core/controller/merb_controller.rb', line 200 def request @request end |
Class Method Details
._callable_methods ⇒ Object
All methods that are callable as actions.
Returns
- Array
-
A list of method names that are also actions
:api: private
107 108 109 110 111 112 113 114 115 |
# File 'lib/merb-core/controller/merb_controller.rb', line 107 def self._callable_methods callables = [] klass = self begin callables << (klass.public_instance_methods(false) + klass._shown_actions) - klass._hidden_actions klass = klass.superclass end until klass == Merb::AbstractController || klass == Object callables.flatten.reject{|action| action =~ /^_.*/} end |
._filter_params(params) ⇒ Object
This is a stub method so plugins can implement param filtering if they want.
Parameters
- params<Hash=> String>
-
A list of params
Returns
- Hash=> String
-
A new list of params, filtered as desired
:api: plugin
97 98 99 |
# File 'lib/merb-core/controller/merb_controller.rb', line 97 def self._filter_params(params) params end |
.abstract! ⇒ Object
Sets a controller to be “abstract” This controller will not be able to be routed to and is used for super classing only
:api: public
359 360 361 |
# File 'lib/merb-core/controller/merb_controller.rb', line 359 def self.abstract! @_abstract = true end |
.abstract? ⇒ Boolean
Asks a controller if it is abstract
Returns
Boolean
true if the controller has been set as abstract
:api: public
370 371 372 |
# File 'lib/merb-core/controller/merb_controller.rb', line 370 def self.abstract? !!@_abstract end |
.callable_actions ⇒ Object
The list of actions that are callable, after taking defaults, _hidden_actions and _shown_actions into consideration. It is calculated once, the first time an action is dispatched for this controller.
Returns
- SimpleSet
-
A set of actions that should be callable.
:api: public
83 84 85 |
# File 'lib/merb-core/controller/merb_controller.rb', line 83 def self.callable_actions @callable_actions ||= Extlib::SimpleSet.new(_callable_methods) end |
.hide_action(*names) ⇒ Object
Hide each of the given methods from being callable as actions.
Parameters
- *names<~to-s>
-
Actions that should be added to the list.
Returns
- Array
-
An array of actions that should not be possible to dispatch to.
:api: public
40 41 42 |
# File 'lib/merb-core/controller/merb_controller.rb', line 40 def self.hide_action(*names) self._hidden_actions = self._hidden_actions | names.map { |n| n.to_s } end |
.inherited(klass) ⇒ Object
Parameters
- klass<Merb::Controller>
-
The Merb::Controller inheriting from the base class.
:api: private
24 25 26 27 28 |
# File 'lib/merb-core/controller/merb_controller.rb', line 24 def self.inherited(klass) _subclasses << klass.to_s super klass._template_root = Merb.dir_for(:view) unless self._template_root end |
.show_action(*names) ⇒ Object
Makes each of the given methods being callable as actions. You can use this to make methods included from modules callable as actions.
Parameters
- *names<~to-s>
-
Actions that should be added to the list.
Returns
- Array
-
An array of actions that should be dispatched to even if they would not otherwise be.
Example
module Foo
def self.included(base)
base.show_action(:foo)
end
def foo
# some actiony stuff
end
def foo_helper
# this should not be an action
end
end
:api: public
71 72 73 |
# File 'lib/merb-core/controller/merb_controller.rb', line 71 def self.show_action(*names) self._shown_actions = self._shown_actions | names.map {|n| n.to_s} end |
.subclasses_list ⇒ Object
:api: private
12 |
# File 'lib/merb-core/controller/merb_controller.rb', line 12 def self.subclasses_list() _subclasses end |
Instance Method Details
#_absolute_template_location(template, type) ⇒ Object
The location to look for a template and mime-type. This is overridden from AbstractController, which defines a version of this that does not involve mime-types.
Parameters
- template<String>
-
The absolute path to a template - without mime and template extension. The mime-type extension is optional - it will be appended from the current content type if it hasn’t been added already.
- type<~to_s>
-
The mime-type of the template that will be rendered. Defaults to nil.
:api: public
152 153 154 |
# File 'lib/merb-core/controller/merb_controller.rb', line 152 def _absolute_template_location(template, type) _conditionally_append_extension(template, type) end |
#_dispatch(action = :index) ⇒ Object
Dispatch the action.
Parameters
- action<~to_s>
-
An action to dispatch to. Defaults to :index.
Returns
- String
-
The string sent to the logger for time spent.
Raises
- ActionNotFound
-
The requested action was not found in class.
:api: plugin
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/merb-core/controller/merb_controller.rb', line 187 def _dispatch(action=:index) Merb.logger.info("Params: #{self.class._filter_params(request.params).inspect}") start = Time.now if self.class.callable_actions.include?(action.to_s) super(action) else raise ActionNotFound, "Action '#{action}' was not found in #{self.class}" end @_benchmarks[:action_time] = Time.now - start self end |
#_template_location(context, type, controller) ⇒ Object
The location to look for a template for a particular controller, context, and mime-type. This is overridden from AbstractController, which defines a version of this that does not involve mime-types.
Parameters
- context<~to_s>
-
The name of the action or template basename that will be rendered.
- type<~to_s>
-
The mime-type of the template that will be rendered. Defaults to nil.
- controller<~to_s>
-
The name of the controller that will be rendered. Defaults to controller_name. This will be “layout” for rendering a layout.
Notes
By default, this renders “:controller/:action.:type”. To change this, override it in your application class or in individual controllers.
:api: public
135 136 137 |
# File 'lib/merb-core/controller/merb_controller.rb', line 135 def _template_location(context, type, controller) _conditionally_append_extension(controller ? "#{controller}/#{context}" : "#{context}", type) end |
#absolute_url(*args) ⇒ Object
Returns the absolute url including the passed protocol and host.
This uses the same arguments as the url method, with added requirements of protocol and host options.
:api: public
335 336 337 338 339 340 341 |
# File 'lib/merb-core/controller/merb_controller.rb', line 335 def absolute_url(*args) = (args) || {} [:protocol] ||= request.protocol [:host] ||= request.host args << super(*args) end |
#params ⇒ Object
Returns
- Hash
-
The parameters from the request object
:api: public
230 |
# File 'lib/merb-core/controller/merb_controller.rb', line 230 def params() request.params end |
#rack_response ⇒ Object
The results of the controller’s render, to be returned to Rack.
Returns
- Array[Integer, Hash, String]
-
The controller’s status code, headers, and body
:api: private
350 351 352 |
# File 'lib/merb-core/controller/merb_controller.rb', line 350 def rack_response [status, headers, Merb::Rack::StreamWrapper.new(body)] end |
#resource(*args) ⇒ Object
Generates a URL for a single or nested resource.
Parameters
- resources<Symbol,Object>
-
The resources for which the URL
should be generated. These resources should be specified
in the router.rb file using #resources and #resource.
- options<Hash>
-
Any extra parameters that are needed to
generate the URL.
Returns
- String
-
The generated URL.
Examples
Merb::Router.prepare do
resources :users do
resources :comments
end
end
resource(:users) # => /users resource(@user) # => /users/10 resource(@user, :comments) # => /users/10/comments resource(@user, @comment) # => /users/10/comments/15 resource(:users, :new) # => /users/new resource(:@user, :edit) # => /users/10/edit
:api: public
321 322 323 324 |
# File 'lib/merb-core/controller/merb_controller.rb', line 321 def resource(*args) args << params Merb::Router.resource(*args) end |
#status ⇒ Object
Returns
- Fixnum
-
The response status code
:api: public
206 207 208 |
# File 'lib/merb-core/controller/merb_controller.rb', line 206 def status @_status end |
#status=(s) ⇒ Object
Set the response status code.
Parameters
- s<Fixnum, Symbol>
-
A status-code or named http-status
:api: public
216 217 218 219 220 221 222 223 224 |
# File 'lib/merb-core/controller/merb_controller.rb', line 216 def status=(s) if s.is_a?(Symbol) && STATUS_CODES.key?(s) @_status = STATUS_CODES[s] elsif s.is_a?(Fixnum) @_status = s else raise ArgumentError, "Status should be of type Fixnum or Symbol, was #{s.class}" end end |
#url(name, *args) ⇒ Object Also known as: relative_url
There are three possible ways to use this method. First, if you have a named route, you can specify the route as the first parameter as a symbol and any paramters in a hash. Second, you can generate the default route by just passing the params hash, just passing the params hash. Finally, you can use the anonymous parameters. This allows you to specify the parameters to a named route in the order they appear in the router.
Parameters(Named Route)
- name<Symbol>
-
The name of the route.
- args<Hash>
-
Parameters for the route generation.
Parameters(Default Route)
- args<Hash>
-
Parameters for the route generation. This route will use the default route.
Parameters(Anonymous Parameters)
- name<Symbol>
-
The name of the route.
- args<Array>
-
An array of anonymous parameters to generate the route with. These parameters are assigned to the route parameters in the order that they are passed.
Returns
- String
-
The generated URL.
Examples
Named Route
Merb::Router.prepare do
match("/articles/:title").to(:controller => :articles, :action => :show).name("articles")
end
url(:articles, :title => “new_article”)
Default Route
Merb::Router.prepare do
default_routes
end
url(:controller => “articles”, :action => “new”)
Anonymous Paramters
Merb::Router.prepare do
match("/articles/:year/:month/:title").to(:controller => :articles, :action => :show).name("articles")
end
url(:articles, 2008, 10, “test_article”)
:api: public
286 287 288 289 290 |
# File 'lib/merb-core/controller/merb_controller.rb', line 286 def url(name, *args) args << params name = request.route if name == :this Merb::Router.url(name, *args) end |