Method: Egalite::Handler#run_controller

Defined in:
lib/egalite.rb

#run_controller(controller, action, req) ⇒ Object

Raises:

  • (SecurityError)


554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/egalite.rb', line 554

def run_controller(controller, action, req)
  # invoke controller
  controller.env = @env
  controller.req = req
  controller.params = req.params
  
  before_filter_result = controller.before_filter
  if before_filter_result != true
    return before_filter_result if before_filter_result.is_a?(Array)
    return [200,{'Content-Type' => "text/html"},[before_filter_result]] if before_filter_result.is_a?(String)
    return forbidden unless before_filter_result.respond_to?(:command)
    response = case before_filter_result.command
     when :delegate
      inner_dispatch(req, before_filter_result.param)
     when :redirect
      redirect(before_filter_result.param)
     when :notfound
      display_notfound
     else
      forbidden
    end
    set_cookies_to_response(response,req)
    return response
  end
  
  nargs = controller.method(action).arity
  args = req.path_params[0,nargs.abs] || []
  if nargs > 0
    args.size.upto(nargs-1) { args << nil }
  end
  raise SecurityError unless controller.respond_to?(action, false)

  s = Time.now
  values = controller.send(action,*args)
  t = Time.now - s
  @profile_logger.puts "#{Time.now}: ctrl #{t}sec #{controller.class.name}.#{action} (#{req.path_info})" if @profile_logger
  
  values = controller.after_filter_return_value(values)
  
  # result handling
  result = if values.respond_to?(:command)
    case values.command
     when :delegate
      inner_dispatch(req, values.param)
     when :redirect
      redirect(values.param)
     when :notfound
      display_notfound
    end
  elsif values.is_a?(Array)
    values
  elsif values.is_a?(String)
    html = controller.after_filter_html(values)
    [200,{'Content-Type' => "text/html"},[html]]
  elsif values.is_a?(Rack::Response)
    values.to_a
  elsif values == nil
    raise "egalite error: controller returned nil as a response."
  else
    htmlfile = controller.template_file
    unless htmlfile
      htmlfile = [req.controller,req.action].compact.join('_')
      htmlfile = 'index' if htmlfile.blank?
      htmlfile += '.html'
    end
    html = load_template(@template_path + htmlfile)
    return [404, {"Content-Type" => "text/plain"}, ["Template not found: #{htmlfile}\n"]] unless html
    
    # apply on_html_load filter
    html = controller.filter_on_html_load(html, htmlfile)
    
    # apply html template
    template = @template_engine.new
    template.controller = controller

    s = Time.now
    template.handleTemplate(html,values) { |values|
      inner_dispatch(req,values)[2].join("")
    }
    t = Time.now - s
    
    html = controller.after_filter_html(html)
    
    @profile_logger.puts "#{Time.now}: view #{t}sec #{controller.class.name}.#{action} (#{req.path_info})" if @profile_logger

    [200,{"Content-Type"=>"text/html"},[html]]
  end
  set_cookies_to_response(result,req)
  return result
end