Class: ClassAction::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/class_action/action.rb

Overview

Base class for controller actions.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Action

Initialization



12
13
14
15
# File 'lib/class_action/action.rb', line 12

def initialize(controller)
  @_controller = controller
  @_controller.singleton_class.send :include, self.class.helpers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



53
54
55
56
57
58
59
60
# File 'lib/class_action/action.rb', line 53

def method_missing(method, *args, &block)
  if controller.respond_to?(method, true)
    self.class._controller_method method
    send method, *args, &block
  else
    super
  end
end

Class Attribute Details

._respondersObject (readonly)

Responders



167
168
169
# File 'lib/class_action/action.rb', line 167

def _responders
  @_responders
end

._responsesObject (readonly)

Responders



167
168
169
# File 'lib/class_action/action.rb', line 167

def _responses
  @_responses
end

.helpersObject

Helpers



142
143
144
# File 'lib/class_action/action.rb', line 142

def helpers
  @helpers
end

Class Method Details

._action_methodsObject



68
69
70
71
72
73
# File 'lib/class_action/action.rb', line 68

def _action_methods
  methods  = public_instance_methods
  methods -= [ :_execute, :available? ]
  methods -= Object.public_instance_methods
  methods
end

._controller_method(method) ⇒ Object

Exposes the given controller methods into the action.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/class_action/action.rb', line 35

def _controller_method(method)
  class_eval <<-RUBY, __FILE__, __LINE__+1
    def #{method}(*args, &block)
      copy_assigns_to_controller
      controller.send :#{method}, *args, &block
    ensure
      copy_assigns_from_controller
    end
    protected :#{method}
  RUBY
end

.helper_method(*methods) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/class_action/action.rb', line 149

def helper_method(*methods)
  methods.each do |method|
    helpers.class_eval <<-RUBY, __FILE__, __LINE__+1
      def #{method}(*args, &block)
        controller = if respond_to?(:class_action)
          self
        else
          self.controller
        end
        controller.class_action.send(:#{method}, *args, &block)
      end
    RUBY
  end
end

.respond_to(*formats, on: nil, &block) ⇒ Object

Defines a response block for the given format(s). Specify an optional precondition in the ‘on` parameter.



195
196
197
198
199
# File 'lib/class_action/action.rb', line 195

def respond_to(*formats, on: nil, &block)
  formats.each do |format|
    _responders[ [format.to_sym, on.try(:to_sym)] ] = block
  end
end

.respond_to_any(on: nil, &block) ⇒ Object

Defines a response block for any remaining format. Specify an optional precondition in the ‘on` parameter.



202
203
204
# File 'lib/class_action/action.rb', line 202

def respond_to_any(on: nil, &block)
  respond_to :any, on: on, &block
end

.respond_with(method, on: nil) ⇒ Object

Defines a method that returns the response. Specify an optional precondition in the ‘on` parameter.



190
191
192
# File 'lib/class_action/action.rb', line 190

def respond_with(method, on: nil)
  _responses[on.try(:to_sym)] = method
end

Instance Method Details

#_executeObject

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/class_action/action.rb', line 77

def _execute
  raise ActionNotAvailable unless available?

  # Execute the action by running all public methods in order.
  self.class._action_methods.each do |method|
    next if self.method(method).arity != 0

    send method

    # Break execution of the action when some response body is set.
    # E.g. when the action decides to redirect halfway.
    break if controller.response_body
  end

  # Perform a default response if not done so yet.
  _respond unless controller.response_body
end

#available?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/class_action/action.rb', line 25

def available?
  true
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/class_action/action.rb', line 49

def respond_to?(method, include_private = false)
  super || (include_private && controller.respond_to?(method, true))
end