Class: Merb::Controller

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

Constant Summary

Constants included from ResponderMixin

ResponderMixin::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ControllerMixin

#delete_cookie, #escape_xml, #nginx_send_file, #redirect, #render_chunked, #render_deferred, #render_then_call, #send_chunk, #send_data, #send_file, #set_cookie, #stream_file

Methods included from ResponderMixin

#_perform_content_negotiation, #_provided_formats, #_set_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.

This method uses the :session_id_cookie_only and :query_string_whitelist configuration options. See CONFIG for more details.

Parameters

request<Merb::Request>

The Merb::Request that came in from Mongrel.

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.




155
156
157
158
# File 'lib/merb-core/controller/merb_controller.rb', line 155

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.



182
183
184
# File 'lib/merb-core/controller/merb_controller.rb', line 182

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



182
183
184
# File 'lib/merb-core/controller/merb_controller.rb', line 182

def request
  @request
end

#routeObject

Returns the value of attribute route.



16
17
18
# File 'lib/merb-core/controller/merb_controller.rb', line 16

def route
  @route
end

#statusObject

Returns the value of attribute status.



183
184
185
# File 'lib/merb-core/controller/merb_controller.rb', line 183

def status
  @status
end

Class Method Details

._hidden_actionsObject

This list of actions that should not be callable.

Returns

Array

An array of actions that should not be dispatchable.



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

def _hidden_actions
  actions = read_inheritable_attribute(:_hidden_actions)
  actions ? actions : write_inheritable_attribute(:_hidden_actions, [])
end

._shown_actionsObject

This list of actions that should be callable.

Returns

Array

An array of actions that should be dispatched to even if they would not otherwise be.



91
92
93
94
# File 'lib/merb-core/controller/merb_controller.rb', line 91

def _shown_actions
  actions = read_inheritable_attribute(:_shown_actions)
  actions ? actions : write_inheritable_attribute(:_shown_actions, [])      
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

Array

A list of actions that should be callable.



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/merb-core/controller/merb_controller.rb', line 102

def callable_actions
  unless @callable_actions
    callables = []
    klass = self
    begin
      callables << (klass.public_instance_methods(false) + klass._shown_actions) - klass._hidden_actions
      klass = klass.superclass
    end until klass == Merb::Controller || klass == Object
    @callable_actions = Merb::SimpleSet.new(callables.flatten)
  end
  @callable_actions
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
  self._template_root = Merb.dir_for(:view) unless self._template_root
  super
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



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

def self.subclasses_list() _subclasses end

Instance Method Details

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




172
173
174
175
176
177
178
179
180
# File 'lib/merb-core/controller/merb_controller.rb', line 172

def _dispatch(action=:index)
  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
end

#_template_location(action, type = nil, controller = controller_name) ⇒ Object

The location to look for a template for a particular controller, action, and mime-type. This is overridden from AbstractController, which defines a version of this that does not involve mime-types.

Parameters

action<~to_s>

The name of the action 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.

Note

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(action, type = nil, controller = controller_name)
  "#{controller}/#{action}.#{type}"
end

#cookiesObject

Returns

Merb::Cookies

A new Merb::Cookies instance representing the cookies that came in from the request object

Note

Headers are passed into the cookie object so that you can do:

cookies[:foo] = "bar"


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

def cookies() @_cookies ||= _setup_cookies end

#paramsObject

Returns

Hash

The parameters from the request object



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

def params()  request.params  end

#sessionObject

Returns

Hash

The session that was extracted from the request object.



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

def session() request.session end