Class: Rory::Controller

Inherits:
Object
  • Object
show all
Includes:
PathGeneration
Defined in:
lib/rory/controller.rb

Overview

Interface for Controller class. Subclass this to create controllers with actions that will be called by the Dispatcher when a route matches.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PathGeneration

#path_to

Constructor Details

#initialize(request, routing, app = nil) ⇒ Controller

Returns a new instance of Controller.



39
40
41
42
43
44
45
46
# File 'lib/rory/controller.rb', line 39

def initialize(request, routing, app = nil)
  @request = request
  @dispatcher = routing[:dispatcher]
  @route = routing[:route]
  @params = request.params
  @app = app
  @locals = {}
end

Instance Attribute Details

#localsObject

Returns the value of attribute locals.



10
11
12
# File 'lib/rory/controller.rb', line 10

def locals
  @locals
end

Class Method Details

.after_action(method_name, opts = {}) ⇒ Object

Register a method to run after the action method.



27
28
29
# File 'lib/rory/controller.rb', line 27

def after_action(method_name, opts = {})
  after_actions << opts.merge(:method_name => method_name)
end

.after_actionsObject



17
18
19
# File 'lib/rory/controller.rb', line 17

def after_actions
  @after_actions ||= ancestor_actions(:after)
end

.ancestor_actions(action_type) ⇒ Object



31
32
33
34
35
36
# File 'lib/rory/controller.rb', line 31

def ancestor_actions(action_type)
  (ancestors - [self]).reverse.map { |c|
    query_method = :"#{action_type}_actions"
    c.send(query_method) if c.respond_to?(query_method)
  }.flatten.compact
end

.before_action(method_name, opts = {}) ⇒ Object

Register a method to run before the action method.



22
23
24
# File 'lib/rory/controller.rb', line 22

def before_action(method_name, opts = {})
  before_actions << opts.merge(:method_name => method_name)
end

.before_actionsObject



13
14
15
# File 'lib/rory/controller.rb', line 13

def before_actions
  @before_actions ||= ancestor_actions(:before)
end

Instance Method Details

#base_pathObject



64
65
66
# File 'lib/rory/controller.rb', line 64

def base_path
  @request.script_name
end

#default_renderer_optionsObject



68
69
70
71
72
73
74
75
# File 'lib/rory/controller.rb', line 68

def default_renderer_options
  {
    :layout => layout,
    :locals => locals,
    :app => @app,
    :base_path => base_path
  }
end

#expose(hsh) ⇒ Object



48
49
50
# File 'lib/rory/controller.rb', line 48

def expose(hsh)
  locals.merge!(hsh)
end

#layoutObject



60
61
62
# File 'lib/rory/controller.rb', line 60

def layout
  nil
end

#paramsObject



52
53
54
# File 'lib/rory/controller.rb', line 52

def params
  @converted_params ||= Rory::HashWithDubiousSemantics.new(@params)
end

#presentObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rory/controller.rb', line 91

def present
  # Call all before and after filters, and if a method exists on the
  # controller for the requested action, call it in between.
  call_filtered_action(@route.action.to_sym)

  if @response
    # that method may have resulted in a response already being generated
    # (such as a redirect, or 404, or other non-HTML response).  if so,
    # just return that response.
    @response
  else
    # even if there wasn't a full response generated, we might already have
    # a @body, if render was explicitly called to render an alternate
    # template, or if @body was explicitly assigned for some other reason.
    # don't render the default template, in that case.
    @body ||= render(route_template)
    [200, {'Content-type' => 'text/html', 'charset' => 'UTF-8'}, [@body]]
  end
end

#redirect(path) ⇒ Object



83
84
85
# File 'lib/rory/controller.rb', line 83

def redirect(path)
  @response = @dispatcher.redirect(path)
end

#render(template_name, opts = {}) ⇒ Object



77
78
79
80
81
# File 'lib/rory/controller.rb', line 77

def render(template_name, opts = {})
  opts = default_renderer_options.merge(opts)
  renderer = Rory::Renderer.new(template_name, opts)
  @body = renderer.render
end

#render_not_foundObject



87
88
89
# File 'lib/rory/controller.rb', line 87

def render_not_found
  @response = @dispatcher.render_not_found
end

#route_templateObject



56
57
58
# File 'lib/rory/controller.rb', line 56

def route_template
  "#{@route.controller}/#{@route.action}"
end