Class: Merb::Controller

Inherits:
AbstractController
  • Object
show all
Includes:
AuthenticationMixin, ConditionalGetMixin, ControllerMixin, ResponderMixin
Defined in:
lib/merb-core/controller/merb_controller.rb

Direct Known Subclasses

Dispatcher::DefaultException

Constant Summary

Constants included from ResponderMixin

ResponderMixin::MIMES, ResponderMixin::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConditionalGetMixin

#etag, #etag=, #etag_matches?, #last_modified, #last_modified=, #not_modified?, #request_fresh?

Methods included from AuthenticationMixin

#basic_authentication

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.




169
170
171
172
# File 'lib/merb-core/controller/merb_controller.rb', line 169

def initialize(request, status=200, headers={'Content-Type' => 'text/html; charset=utf-8'})
  super()
  @request, @_status, @headers = request, status, headers
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



198
199
200
# File 'lib/merb-core/controller/merb_controller.rb', line 198

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



198
199
200
# File 'lib/merb-core/controller/merb_controller.rb', line 198

def request
  @request
end

Class Method Details

._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




95
96
97
# File 'lib/merb-core/controller/merb_controller.rb', line 95

def _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



261
262
263
# File 'lib/merb-core/controller/merb_controller.rb', line 261

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

Returns:

  • (Boolean)


270
271
272
# File 'lib/merb-core/controller/merb_controller.rb', line 270

def self.abstract?
  !!@_abstract 
end

.callable_actionsObject

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.



82
83
84
# File 'lib/merb-core/controller/merb_controller.rb', line 82

def 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.




40
41
42
# File 'lib/merb-core/controller/merb_controller.rb', line 40

def 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.



23
24
25
26
27
# File 'lib/merb-core/controller/merb_controller.rb', line 23

def 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



72
73
74
# File 'lib/merb-core/controller/merb_controller.rb', line 72

def show_action(*names)
  self._shown_actions = self._shown_actions | names.map {|n| n.to_s}
end

.subclasses_listObject



11
# File 'lib/merb-core/controller/merb_controller.rb', line 11

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.



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.




186
187
188
189
190
191
192
193
194
195
196
# File 'lib/merb-core/controller/merb_controller.rb', line 186

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.

Notes

By default, this renders “:controller/:action.:type”. To change this, override it in your application class or in individual controllers.




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



241
242
243
244
245
246
247
# File 'lib/merb-core/controller/merb_controller.rb', line 241

def absolute_url(*args)
  options  = extract_options_from_args!(args) || {}
  options[:protocol] ||= request.protocol
  options[:host] ||= request.host
  args << options
  super(args.first, *args[1..-1])
end

#paramsObject

Returns

Hash

The parameters from the request object



220
# File 'lib/merb-core/controller/merb_controller.rb', line 220

def params()  request.params  end

#rack_responseObject

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



254
255
256
# File 'lib/merb-core/controller/merb_controller.rb', line 254

def rack_response
  [status, headers, Merb::Rack::StreamWrapper.new(body)]
end

#statusObject



200
201
202
# File 'lib/merb-core/controller/merb_controller.rb', line 200

def status
  @_status
end

#status=(s) ⇒ Object

Set the response status code.

Parameters

s<Fixnum, Symbol>

A status-code or named http-status



208
209
210
211
212
213
214
215
216
# File 'lib/merb-core/controller/merb_controller.rb', line 208

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

Parameters

name<~to_sym, Hash>

The name of the URL to generate.

rparams<Hash>

Parameters for the route generation.

Returns

String

The generated URL.

Alternatives

If a hash is used as the first argument, a default route will be generated based on it and rparams.

TODO: Update this documentation



234
235
236
237
# File 'lib/merb-core/controller/merb_controller.rb', line 234

def url(name, *args)
  args << params
  Merb::Router.url(name, *args)
end