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

Extended by:
MixinWithActions
Defined in:
lib/mithril/controllers/mixins/actions_base.rb

Overview

Core functions for implementing a command+args response model. ActionsBase should be mixed in to controllers, either directly or via an intermediate Mixin that implements default or shared actions.

See Also:

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin

#mixins, #mixins=

Instance Attribute Details

#requestMithril::Request (readonly)

Returns:



66
67
68
# File 'lib/mithril/controllers/mixins/actions_base.rb', line 66

def request
  @request
end

Instance Method Details

#actions(allow_private = false) ⇒ Hash

Lists the actions available to the current controller.

Parameters:

  • allow_private (Boolean) (defaults to: false)

    If true, will include private actions.

Returns:

  • (Hash)

    The actions available to this controller.



72
73
74
75
76
77
78
79
80
# File 'lib/mithril/controllers/mixins/actions_base.rb', line 72

def actions(allow_private = false)
  actions = {}
  
  actions.update(self.class.superclass.actions(allow_private)) if (klass = self.class.superclass).respond_to? :actions
  
  actions.update(self.class.actions(allow_private))
  
  actions
end

#has_action?(key, allow_private = false) ⇒ Boolean

Returns True if the action is available on this controller with the specified private setting; false otherwise.

Parameters:

  • key (Symbol, String)

    The action key to be checked.

  • allow_private (Boolean) (defaults to: false)

    If true, will include private actions.

Returns:

  • (Boolean)

    True if the action is available on this controller with the specified private setting; false otherwise.



86
87
88
89
90
# File 'lib/mithril/controllers/mixins/actions_base.rb', line 86

def has_action?(key, allow_private = false)
  return false if key.nil?
  
  self.actions(allow_private).has_key? key.intern
end

#invoke_action(command, arguments, allow_private = false) ⇒ String?

Searches for a matching action. If found, calls the action with the given session hash and arguments list.

Parameters:

  • command (Symbol, String)

    Converted to a string. The converted string must be an exact match (===) to the key passed in to klass.define_action.

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

  • allow_private (Boolean) (defaults to: false)

    If true, can invoke private actions.

Returns:

  • (String, nil)

    The result of the action (should be a string), or nil if no action was invoked.



105
106
107
108
109
110
111
112
# File 'lib/mithril/controllers/mixins/actions_base.rb', line 105

def invoke_action(command, arguments, allow_private = false)
  session = request ? request.session || {} : {}
  if self.has_action? command, allow_private
    self.send :"action_#{command}", session, arguments
  else
    nil
  end # if-else
end