Class: Merb::PartController

Inherits:
AbstractController
  • Object
show all
Includes:
Mixins::WebController
Defined in:
lib/merb-parts/part_controller.rb

Overview

A Merb::PartController is a light weight way to share logic and templates amongst controllers. Merb::PartControllers work just like Merb::controller. There is a filter stack, layouts (if needed) all the render functions, and url generation.

Cookies, params, and even the request object are shared with the web controller

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::WebController

included

Constructor Details

#initialize(web_controller, opts = {}) ⇒ PartController

Parameters

web_controller<Merb::Controller>

The controller calling this part.

opts<Hash>

Additional options for this part.



68
69
70
71
72
73
74
# File 'lib/merb-parts/part_controller.rb', line 68

def initialize(web_controller, opts = {})
  @web_controller = web_controller
  @params = @web_controller.params.dup
  @params.merge!(opts) unless opts.empty?
  super
  @content_type = @web_controller.content_type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object

Send any methods that are missing back up to the web controller Patched to set partial locals on the web controller



89
90
91
92
# File 'lib/merb-parts/part_controller.rb', line 89

def method_missing(sym, *args, &blk)
  @web_controller.instance_variable_set(:@_merb_partial_locals, @_merb_partial_locals)
  @web_controller.send(sym, *args, &blk)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



14
15
16
# File 'lib/merb-parts/part_controller.rb', line 14

def params
  @params
end

Class Method Details

.inherited(klass) ⇒ Object

Sets the template root to the default parts view directory.

Parameters

klass<Class>

The Merb::PartController inheriting from the base class.



59
60
61
62
63
# File 'lib/merb-parts/part_controller.rb', line 59

def self.inherited(klass)
  _subclasses << klass.to_s
  super
  klass._template_root = Merb.dir_for(:part) / "views" unless self._template_root
end

.subclasses_listObject

Returns

Array

Classes that inherit from Merb::PartController.



21
# File 'lib/merb-parts/part_controller.rb', line 21

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.



50
51
52
# File 'lib/merb-parts/part_controller.rb', line 50

def _absolute_template_location(template, type)
  template.match(/\.#{type.to_s.escape_regexp}$/) ? template : "#{template}.#{type}"
end

#_dispatch(action = :to_s) ⇒ Object

Parameters

action<~to_s>

An action to dispatch to. Defaults to :to_s.

Returns

String

The part body.



81
82
83
84
85
# File 'lib/merb-parts/part_controller.rb', line 81

def _dispatch(action=:to_s)
  self.action_name = action
  super(action)
  @body
end

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

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

controller<~to_s>

The name of the controller that will be rendered. Defaults to controller_name.

Returns

String

The template location, i.e. “:controller/:action.:type”.



33
34
35
# File 'lib/merb-parts/part_controller.rb', line 33

def _template_location(action, type = :html, controller = controller_name)
  "#{controller}/#{action}.#{type}"
end