Parent

Class Index [+]

Quicksearch

Merb::AbstractController

Constants

FILTER_OPTIONS

Attributes

body[RW]

:api: plugin

action_name[RW]

:api: plugin

_benchmarks[RW]

:api: plugin

_thrown_content[RW]

:api: private

content_type[RW]

Stub so content-type support in RenderMixin doesn’t throw errors :api: private

Public Class Methods

_reset_template_roots() click to toggle source

Reset the template root based on the @_template_root ivar.

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 203
203:     def self._reset_template_roots
204:       self.template_roots = [[self._template_root, :_template_location]]
205:     end
_template_root=(root) click to toggle source

Resets the template roots to the template root passed in.

Parameters

root<~to_s>

The new path to set the template root to.

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 195
195:     def self._template_root=(root)
196:       @_template_root = root
197:       _reset_template_roots
198:     end
_template_roots() click to toggle source

Returns

roots

Template roots as pairs of template root path and template location method.

:api: plugin

     # File lib/merb-core/controller/abstract_controller.rb, line 213
213:     def self._template_roots
214:       self.template_roots || _reset_template_roots
215:     end
_template_roots=(roots) click to toggle source

Parameters

roots

Template roots as pairs of template root path and template location method.

:api: plugin

     # File lib/merb-core/controller/abstract_controller.rb, line 223
223:     def self._template_roots=(roots)
224:       self.template_roots = roots
225:     end
after(filter = nil, opts = {}, &block) click to toggle source

Adds a filter to the after filter chain

Parameters

filter

The filter to add. Defaults to nil.

opts

Filter options (see class documentation under Filter Options).

&block

A block to use as a filter if filter is nil.

Notes

If the filter already exists, its options will be replaced with opts.;

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 446
446:     def self.after(filter = nil, opts = {}, &block)
447:       add_filter(self._after_filters, filter || block, opts)
448:     end
before(filter = nil, opts = {}, &block) click to toggle source

Adds a filter to the before filter chain.

Parameters

filter

The filter to add. Defaults to nil.

opts

Filter options (see class documentation under Filter Options).

&block

A block to use as a filter if filter is nil.

Notes

If the filter already exists, its options will be replaced with opts.

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 462
462:     def self.before(filter = nil, opts = {}, &block)
463:       add_filter(self._before_filters, filter || block, opts)
464:     end
controller_name() click to toggle source

Returns

String

The controller name in path form, e.g. “admin/items“.

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 132
132:     def self.controller_name() @controller_name ||= self.name.to_const_path end
inherited(klass) click to toggle source

Parameters

klass

The controller that is being inherited from Merb::AbstractController

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 241
241:     def self.inherited(klass)
242:       _abstract_subclasses << klass.to_s
243:       helper_module_name = klass.to_s =~ /^(#|Merb::)/ ? "#{klass}Helper" : "Merb::#{klass}Helper"
244:       # support for unnamed module like "#<Class:0xa2e5e50>::TestController"
245:       helper_module_name.gsub!(/(::)|[:#<>]/, "\\1")
246: 
247:       Object.make_module helper_module_name
248:       klass.class_eval         include Object.full_const_get("#{helper_module_name}") rescue nil
249:       super
250:     end
new(*args) click to toggle source

This will initialize the controller, it is designed to be overridden in subclasses (like MerbController)

Parameters

*args

The args are ignored in this class, but we need this so that subclassed initializes can have parameters

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 259
259:     def initialize(*args)
260:       @_benchmarks = {}
261:       @_caught_content = {}
262:     end
skip_after(filter) click to toggle source

Removes a filter from the after filter chain. This removes the filter from the filter chain for the whole controller and does not take any options.

Parameters

filter

A filter name to skip.

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 474
474:     def self.skip_after(filter)
475:       skip_filter(self._after_filters, filter)
476:     end
skip_before(filter) click to toggle source

Removes a filter from the before filter chain. This removes the filter from the filter chain for the whole controller and does not take any options.

Parameters

filter

A filter name to skip.

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 486
486:     def self.skip_before(filter)
487:       skip_filter(self._before_filters , filter)
488:     end
subclasses_list() click to toggle source

Returns the list of classes that have specifically subclassed AbstractController. Does not include all decendents.

Returns

Set

The subclasses.

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 234
234:     def self.subclasses_list() _abstract_subclasses end

Private Class Methods

add_filter(filters, filter, opts={}) click to toggle source

adds a filter to the specified filter chain

Parameters

filters

The filter chain that this should be added to.

filter

A filter that should be added.

opts

Filter options (see class documentation under Filter Options).

Raises

ArgumentError

Both :only and :exclude, or :if and :unless given, if filter is not a Symbol, String or Proc, or if an unknown option is passed.

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 652
652:     def self.add_filter(filters, filter, opts={})
653:       raise(ArgumentError,
654:         "You can specify either :only or :exclude but 
655:          not both at the same time for the same filter.") if opts.key?(:only) && opts.key?(:exclude)
656:        
657:        raise(ArgumentError,
658:          "You can specify either :if or :unless but 
659:           not both at the same time for the same filter.") if opts.key?(:if) && opts.key?(:unless)
660:         
661:       opts.each_key do |key| raise(ArgumentError,
662:         "You can only specify known filter options, #{key} is invalid.") unless FILTER_OPTIONS.include?(key)
663:       end
664: 
665:       opts = normalize_filters!(opts)
666:     
667:       case filter
668:       when Proc
669:         # filters with procs created via class methods have identical signature
670:         # regardless if they handle content differently or not. So procs just
671:         # get appended
672:         filters << [filter, opts]
673:       when Symbol, String
674:         if existing_filter = filters.find {|f| f.first.to_s == filter.to_s}
675:           filters[ filters.index(existing_filter) ] = [filter, opts]
676:         else
677:           filters << [filter, opts]
678:         end
679:       else
680:         raise(ArgumentError, 
681:           'Filters need to be either a Symbol, String or a Proc'
682:         )        
683:       end
684:     end
normalize_filters!(opts={}) click to toggle source

Ensures that the passed in hash values are always arrays.

Parameters

opts

Options for the filters (see below).

Options (opts)

:only

A list of actions.

:exclude

A list of actions.

Examples

  normalize_filters!(:only => :new) #=> {:only => [:new]}

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 718
718:     def self.normalize_filters!(opts={})
719:       opts[:only]     = Array(opts[:only]).map {|x| x.to_s} if opts[:only]
720:       opts[:exclude]  = Array(opts[:exclude]).map {|x| x.to_s} if opts[:exclude]
721:       return opts
722:     end
skip_filter(filters, filter) click to toggle source

Skip a filter that was previously added to the filter chain. Useful in inheritence hierarchies.

Parameters

filters

The filter chain that this should be removed from.

filter

A filter that should be removed.

Raises

ArgumentError

filter not Symbol or String.

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 697
697:     def self.skip_filter(filters, filter)
698:       raise(ArgumentError, 'You can only skip filters that have a String or Symbol name.') unless
699:         [Symbol, String].include? filter.class
700: 
701:       Merb.logger.warn("Filter #{filter} was not found in your filter chain.") unless
702:         filters.reject! {|f| f.first.to_s[filter.to_s] }
703:     end

Public Instance Methods

_absolute_template_location(template, type) click to toggle source

The location to look for a template - override this method for particular behaviour.

Parameters

template

The absolute path to a template - without template extension.

type<~to_s>

The mime-type of the template that will be rendered. Defaults to being called with nil.

:api: public @overridable

     # File lib/merb-core/controller/abstract_controller.rb, line 184
184:     def _absolute_template_location(template, type)
185:       template
186:     end
_call_action(action) click to toggle source

This method exists to provide an overridable hook for ActionArgs. It uses # to call the action method.

Parameters

action<~to_s>

the action method to dispatch to

:api: plugin @overridable

     # File lib/merb-core/controller/abstract_controller.rb, line 320
320:     def _call_action(action)
321:       send(action)
322:     end
_call_filter_for_action?(rule, action_name) click to toggle source

Determine whether the filter should be called for the current action using :only and :exclude.

Parameters

rule

Rules for the filter (see below).

action_name<~to_s>

The name of the action to be called.

Options (rule)

:only

Optional list of actions to fire. If given, action_name must be a part of it for this function to return true.

:exclude

Optional list of actions not to fire. If given, action_name must not be a part of it for this function to return true.

Returns

Boolean

True if the action should be called.

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 379
379:     def _call_filter_for_action?(rule, action_name)
380:       # Both:
381:       # * no :only or the current action is in the :only list
382:       # * no :exclude or the current action is not in the :exclude list
383:       (!rule.key?(:only) || rule[:only].include?(action_name)) &&
384:       (!rule.key?(:exclude) || !rule[:exclude].include?(action_name))
385:     end
_call_filters(filter_set) click to toggle source

Calls a filter chain.

Parameters

filter_set

A set of filters in the form [[:filter, rule], [:filter, rule]]

Returns

Symbol

:filter_chain_completed.

Notes

Filter rules can be Symbols, Strings, or Procs.

Symbols or Strings

Call the method represented by the Symbol or String.

Procs

Execute the Proc, in the context of the controller (self will be the controller)

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 343
343:     def _call_filters(filter_set)
344:       (filter_set || []).each do |filter, rule|
345:         if _call_filter_for_action?(rule, action_name) && _filter_condition_met?(rule)
346:           case filter
347:           when Symbol, String
348:             if rule.key?(:with)
349:               args = rule[:with]
350:               send(filter, *args)
351:             else
352:               send(filter)
353:             end
354:           when Proc then self.instance_eval(&filter)
355:           end
356:         end
357:       end
358:       return :filter_chain_completed
359:     end
_dispatch(action) click to toggle source

This will dispatch the request, calling internal before/after dispatch callbacks. If the return value of _call_filters is not :filter_chain_completed the action is not called, and the return from the filters is used instead.

Parameters

action<~to_s>

The action to dispatch to. This will be #‘ed in _call_action. Defaults to :to_s.

Returns

<~to_s>

Returns the string that was returned from the action.

Raises

ArgumentError

Invalid result caught from before filters.

:api: plugin

     # File lib/merb-core/controller/abstract_controller.rb, line 280
280:     def _dispatch(action)
281:       self.action_name = action
282:       self._before_dispatch_callbacks.each { |cb| cb.call(self) }
283: 
284:       caught = catch(:halt) do
285:         start = Time.now
286:         result = _call_filters(_before_filters)
287:         @_benchmarks[:before_filters_time] = Time.now - start if _before_filters
288: 
289:         @body = _call_action(action_name) if result == :filter_chain_completed
290: 
291:         result
292:       end
293:   
294:       @body = case caught
295:       when :filter_chain_completed  then @body
296:       when String                   then caught
297:       # return *something* if you throw halt with nothing
298:       when nil                      then "<html><body><h1>Filter Chain Halted!</h1></body></html>"
299:       when Symbol                   then __send__(caught)
300:       when Proc                     then self.instance_eval(&caught)
301:       else
302:         raise ArgumentError, "Threw :halt, #{caught}. Expected String, nil, Symbol, Proc."
303:       end
304:       start = Time.now
305:       _call_filters(_after_filters)
306:       @_benchmarks[:after_filters_time] = Time.now - start if _after_filters
307:     
308:       self._after_dispatch_callbacks.each { |cb| cb.call(self) }
309:     
310:       @body
311:     end
_evaluate_condition(condition) click to toggle source

Evaluates a filter condition (:if or :unless)

Parameters

condition

The condition to evaluate.

Raises

ArgumentError

condition not a Symbol or Proc.

Returns

Boolean

True if the condition is met.

Alternatives

If condition is a symbol, it will be send’ed. If it is a Proc it will be called directly with self as an argument.

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 425
425:     def _evaluate_condition(condition)
426:       case condition
427:       when Symbol then self.send(condition)
428:       when Proc then self.instance_eval(&condition)
429:       else
430:         raise ArgumentError,
431:               'Filter condtions need to be either a Symbol or a Proc'
432:       end
433:     end
_filter_condition_met?(rule) click to toggle source

Determines whether the filter should be run based on the conditions passed (:if and :unless)

Parameters

rule

Rules for the filter (see below).

Options (rule)

:if

Optional conditions that must be met for the filter to fire.

:unless

Optional conditions that must not be met for the filter to fire.

Returns

Boolean

True if the conditions are met.

:api: private

     # File lib/merb-core/controller/abstract_controller.rb, line 401
401:     def _filter_condition_met?(rule)
402:       # Both:
403:       # * no :if or the if condition evaluates to true
404:       # * no :unless or the unless condition evaluates to false
405:       (!rule.key?(:if) || _evaluate_condition(rule[:if])) &&
406:       (!rule.key?(:unless) || ! _evaluate_condition(rule[:unless]))
407:     end
_template_location(context, type, controller) click to toggle source

This is called after the controller is instantiated to figure out where to look for templates under the _template_root. Override this to define a new structure for your app.

Parameters

context<~to_s>

The controller context (the action or template name).

type<~to_s>

The content type. Could be nil.

controller<~to_s>

The name of the controller. Defaults to being called with the controller_name. Set t

Returns

String

Indicating where to look for the template for the current controller, context, and content-type.

Notes

The type is irrelevant for controller-types that don’t support content-type negotiation, so we default to not include it in the superclass.

Examples

  def _template_location
    "#{params[:controller]}.#{params[:action]}.#{content_type}"
  end

This would look for templates at controller.action.mime.type instead of controller/action.mime.type

:api: public @overridable

     # File lib/merb-core/controller/abstract_controller.rb, line 171
171:     def _template_location(context, type, controller)
172:       controller ? "#{controller}/#{context}" : context
173:     end
absolute_url(*args) click to toggle source

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

     # File lib/merb-core/controller/abstract_controller.rb, line 557
557:     def absolute_url(*args)
558:       # FIXME: arrgh, why request.protocol returns http://?
559:       # :// is not part of protocol name
560:       options  = extract_options_from_args!(args) || {}
561:       protocol = options.delete(:protocol)
562:       host     = options.delete(:host)
563:     
564:       raise ArgumentError, "The :protocol option must be specified" unless protocol
565:       raise ArgumentError, "The :host option must be specified"     unless host
566:     
567:       args << options
568:     
569:       protocol + "://" + host + url(*args)
570:     end
capture(*args, &block) click to toggle source

Calls the capture method for the selected template engine.

Parameters

*args

Arguments to pass to the block.

&block

The block to call.

Returns

String

The output of a template block or the return value of a non-template block converted to a string.

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 616
616:     def capture(*args, &block)
617:       ret = nil
618: 
619:       captured = send("capture_#{@_engine}", *args) do |*args|
620:         ret = yield *args
621:       end
622: 
623:       # return captured value only if it is not empty
624:       captured.empty? ? ret.to_s : captured
625:     end
concat(str, binding) click to toggle source

Calls the concatenate method for the selected template engine.

Parameters

str

The string to concatenate to the buffer.

binding

The binding to use for the buffer.

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 634
634:     def concat(str, binding)
635:       send("concat_#{@_engine}", str, binding)
636:     end
controller_name() click to toggle source

Returns

String

The controller name in path form, e.g. “admin/items“.

:api: public

     # File lib/merb-core/controller/abstract_controller.rb, line 138
138:     def controller_name()      self.class.controller_name                   end
relative_url(name, *args) click to toggle source
Alias for: url
resource(*args) click to toggle source

Generates a URL for a single or nested resource.

Parameters

resources

The resources for which the URL

  should be generated. These resources should be specified
  in the router.rb file using #resources and #resource.
options

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

     # File lib/merb-core/controller/abstract_controller.rb, line 601
601:     def resource(*args)
602:       args << {}
603:       Merb::Router.resource(*args)
604:     end
url(name, *args) click to toggle source

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

The name of the route.

args

Parameters for the route generation.

Parameters(Default Route)

args

Parameters for the route generation. This route will use the default route.

Parameters(Anonymous Parameters)

name

The name of the route.

args

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

     # File lib/merb-core/controller/abstract_controller.rb, line 544
544:     def url(name, *args)
545:       args << {}
546:       Merb::Router.url(name, *args)
547:     end
Also aliased as: relative_url

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.