Method: ShortStack::Controller#dispatch!

Defined in:
lib/short_stack/controller.rb

#dispatch!Object

Dispatches to an action based on the params parameter



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/short_stack/controller.rb', line 84

def dispatch!
  if logger
    logger.info "Request: #{request.path}"
    logger.info "Params: #{params.inspect}"
  end


  # Check that the action is available
  raise Pancake::Errors::NotFound, "No Action Found" unless allowed_action?(action)

  @action_opts  = actions[action]

  negotiate_content_type!(@action_opts.formats, params)

  # Set the layout defaults before the action is rendered
  if layout && stack_class.default_layout
    layout.template_name = stack_class.default_layout
  end

  layout.format = params['format'] if layout

  logger.info "Dispatching to #{action.inspect}" if logger

  result = catch(:halt){ self.send(action) }

  case result
  when Array
    result
  when Rack::Response
    result.finish
  when String
    out = if layout
      layout.content = result
      layout
    else
      result
    end
    Rack::Response.new(out, status, headers).finish
  else
    Rack::Response.new((result || ""), status, headers).finish
  end

rescue Pancake::Errors::HttpError => e
  if logger && log_http_error?(e)
    logger.error "Exception: #{e.message}"
    logger.error e.backtrace.join("\n")
  end
  handle_request_exception(e)
rescue Exception => e
  if Pancake.handle_errors?
    server_error = Pancake::Errors::Server.new(e.message)
    server_error.exceptions << e
    server_error.set_backtrace e.backtrace
  else
    server_error = e
  end
  handle_request_exception(server_error)
end