Class: Crystal::Processors::ControllerCaller

Inherits:
Crystal::Processor show all
Defined in:
lib/crystal/controller/processors/controller_caller.rb

Instance Attribute Summary collapse

Attributes inherited from Crystal::Processor

#next_processor

Instance Method Summary collapse

Methods inherited from Crystal::Processor

inspect

Constructor Details

#initialize(next_processor, result_variable = 'content') ⇒ ControllerCaller

Returns a new instance of ControllerCaller.



6
7
8
9
10
11
# File 'lib/crystal/controller/processors/controller_caller.rb', line 6

def initialize next_processor, result_variable = 'content'
  result_variable.must_be.present
  
  super(next_processor)                
  @result_variable = result_variable
end

Instance Attribute Details

#result_variableObject

Returns the value of attribute result_variable.



4
5
6
# File 'lib/crystal/controller/processors/controller_caller.rb', line 4

def result_variable
  @result_variable
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/crystal/controller/processors/controller_caller.rb', line 13

def call                
  # prepare
  klass = workspace.class.must_be.present
  raise "The controller class #{klass} must be a Crystal::AbstractController!" unless klass.is? Crystal::AbstractController
  controller = workspace.controller = klass.new                  
  action = workspace.action = workspace.method_name
  format = workspace.params.format

  # call
  special_result = catch :special_result do
    controller.run_callbacks :action, :method => action do          
      # call controller
      controller.send action
      # render view
      workspace[result_variable] = controller.render_action :action => action          
    end
  end
  
  if special_result
    workspace[result_variable] = special_result
  else
    workspace[result_variable].must_be.defined
  end        
end