Module: Mithril::Controllers::Mixins::ActionsBase::ClassMethods

Defined in:
lib/mithril/controllers/mixins/actions_base.rb

Overview

These methods get extended into the class of the controller through the magic of Mixin.

Instance Method Summary collapse

Instance Method Details

#actions(allow_private = false) ⇒ Hash

Lists the actions defined for the current controller by its base class. In almost all cases, the actions instance method should be used instead, as it handles class-based inheritance.

Parameters:

  • allow_private (Boolean) (defaults to: false)

    If true, will include private actions.

Returns:

  • (Hash)

    The actions defined on the current controller class.

See Also:



54
55
56
57
58
59
60
61
62
# File 'lib/mithril/controllers/mixins/actions_base.rb', line 54

def actions(allow_private = false)
  actions = @actions ||= {}
  
  unless allow_private
    actions = actions.select { |key, action| !action.has_key? :private }
  end # unless
  
  actions
end

#define_action(key, params = {}) {|session, arguments| ... } ⇒ Object

Defines an action to which the controller will respond.

Parameters:

  • key (Symbol, String)

    Best practice is to use snake_case, e.g. all lower-case letters, with words separated by underscores. It ought to work anyway, but caveat lector.

  • params (Hash) (defaults to: {})

    Optional. Expects a hash of configuration values.

Options Hash (params):

  • :private (Boolean)

    If set to true, creates a private action. Private actions are not listed by “help” and cannot be invoked directly by the user. They can be used to set up internal APIs.

Yield Parameters:

  • session (Hash)

    An object describing the current (volatile) state of the user session.

  • arguments (Object)

    Additional information from the request to be passed into the action. Using the default parser, the arguments object will be an Array, but other parsers may pass in other data structures.



37
38
39
40
41
42
43
44
# File 'lib/mithril/controllers/mixins/actions_base.rb', line 37

def define_action(key, params = {}, &block)
  key = key.to_s.downcase.gsub(/\s+|\-+/,'_').intern
  
  define_method :"action_#{key}", &block
  
  @actions ||= {}
  @actions[key] = params
end