Class: Pancake::Stacks::Short::Controller

Inherits:
Object
  • Object
show all
Extended by:
Mixins::Publish
Includes:
Mixins::Render, Mixins::RequestHelper, Mixins::ResponseHelper, Mixins::StackHelper
Defined in:
lib/pancake/stacks/short/controller.rb

Constant Summary collapse

DEFAULT_EXCEPTION_HANDLER =
lambda do |error|
  "#{error.name}: #{error.description}"
end

Constants included from Mixins::RequestHelper

Mixins::RequestHelper::VARS_KEY

Constants included from Mixins::Render

Mixins::Render::RENDER_SETUP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Publish

as, extended, method_added, provides, publish, validate_and_coerce_params

Methods included from Mixins::StackHelper

included

Methods included from Mixins::ResponseHelper

#headers, #redirect

Methods included from Mixins::RequestHelper

#base_url, #configuration, #env, #env=, #logger, #request, #url, #url_for, #vars

Methods included from Mixins::Render

included

Constructor Details

#initialize(env) ⇒ Controller

Returns a new instance of Controller.



30
31
32
33
# File 'lib/pancake/stacks/short/controller.rb', line 30

def initialize(env)
  @env, @request = env, Rack::Request.new(env)
  @status = 200
end

Instance Attribute Details

#statusObject



28
29
30
# File 'lib/pancake/stacks/short/controller.rb', line 28

def status
  @status
end

Class Method Details

._template_name_for(name, opts) ⇒ Object



116
117
118
119
# File 'lib/pancake/stacks/short/controller.rb', line 116

def self._template_name_for(name, opts)
  opts[:format] ||= :html
  "#{name}.#{opts[:format]}"
end

.call(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
# File 'lib/pancake/stacks/short/controller.rb', line 22

def self.call(env)
  app = new(env)
  app.dispatch!
end

.handle_exception(&block) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/pancake/stacks/short/controller.rb', line 91

def self.handle_exception(&block)
  if block_given?
    self._handle_exception = block
  else
    self._handle_exception || DEFAULT_EXCEPTION_HANDLER
  end
end

.rootsObject



112
113
114
# File 'lib/pancake/stacks/short/controller.rb', line 112

def self.roots
  stack_class.roots
end

Instance Method Details

#_tempate_name_for(name, opts = {}) ⇒ Object



121
122
123
124
# File 'lib/pancake/stacks/short/controller.rb', line 121

def _tempate_name_for(name, opts = {})
  opts[:format] ||= content_type
  self.class._template_name_for(name, opts)
end

#dispatch!Object

Dispatches to an action based on the params parameter



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pancake/stacks/short/controller.rb', line 42

def dispatch!
  params["action"] ||= params[:action]
  params[:format]  ||= params["format"]

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

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

  @action_opts  = actions[params["action"]]

  negotiate_content_type!(@action_opts.formats, params)

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

  result = catch(:halt){ self.send(params['action']) }
  case result
  when Array
    result
  when Rack::Response
    result.finish
  else
    Rack::Response.new((result || ""), status, headers).finish
  end

rescue 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 = 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

#handle_request_exception(error) ⇒ Object

Raises:

  • (error.class)


99
100
101
102
103
104
# File 'lib/pancake/stacks/short/controller.rb', line 99

def handle_request_exception(error)
  raise(error.class, error.message, error.backtrace) unless Pancake.handle_errors?
  self.status = error.code
  result = instance_exec error, &self.class.handle_exception
  Rack::Response.new(result, status, headers).finish
end

#log_http_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/pancake/stacks/short/controller.rb', line 87

def log_http_error?(error)
  true
end

#paramsObject

Provides access to the request params



37
38
39
# File 'lib/pancake/stacks/short/controller.rb', line 37

def params
  request.params
end