Module: Cuprum::Rails::Controllers::ClassMethods::Actions

Defined in:
lib/cuprum/rails/controllers/class_methods/actions.rb

Overview

Provides a DSL for defining controller actions.

Instance Method Summary collapse

Instance Method Details

#action(action_name, action_class, member: false) ⇒ Object

Defines a controller action.

Parameters:

  • action_name (String, Symbol)

    The name of the action.

  • action_class (Class)

    The class of the action command. Must be constructible with keyword :resource.

  • member (Boolean) (defaults to: false)

    True if the action acts on a collection item, not on the collection as a whole.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cuprum/rails/controllers/class_methods/actions.rb', line 16

def action(action_name, action_class, member: false)
  validate_name(action_name, as: 'action name')
  validate_class(action_class, as: 'action class')

  action_name              = action_name.intern
  own_actions[action_name] = Cuprum::Rails::Controllers::Action.new(
    action_class:  action_class,
    action_name:   action_name,
    member_action: member
  )

  define_action(action_name)
end

#actionsHash<Symbol, Cuprum::Rails::Controllers::Action>

Returns the actions defined for the controller.

Returns:



32
33
34
35
36
37
38
# File 'lib/cuprum/rails/controllers/class_methods/actions.rb', line 32

def actions
  ancestors
    .select { |ancestor| ancestor.respond_to?(:own_actions) }
    .reverse_each
    .map(&:own_actions)
    .reduce(&:merge)
end

#apply_request_defaults(request) ⇒ Object



41
42
43
# File 'lib/cuprum/rails/controllers/class_methods/actions.rb', line 41

def apply_request_defaults(request)
  request.format ||= configuration.default_format
end

#build_request(context, **options) ⇒ Cuprum::Rails::Request

Generates a Cuprum::Rails::Request from a native request.

Override this method to generate a request subclass.

Parameters:

  • context (#request)

    the controller or controller context.

  • options (Hash{Symbol=>Object})

    additional options for the request.

Returns:



53
54
55
56
57
58
59
# File 'lib/cuprum/rails/controllers/class_methods/actions.rb', line 53

def build_request(context, **options)
  Cuprum::Rails::Request.build(
    context: context,
    request: context.request,
    **options
  )
end

#own_actionsObject



62
63
64
65
66
# File 'lib/cuprum/rails/controllers/class_methods/actions.rb', line 62

def own_actions
  # :nocov:
  @own_actions ||= {}
  # :nocov:
end