Class: Poniard::ControllerSource::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/poniard/controller_source.rb

Overview

A wrapper around the Rails response that provides a more OO-friendly interface, specifically designed with isolated unit testing in mind.

This class should not be explictly constructed by users. It is provided in the response parameter provided by Poniard::ControllerSource.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, injector) ⇒ Response

Returns a new instance of Response.



16
17
18
19
# File 'lib/poniard/controller_source.rb', line 16

def initialize(controller, injector)
  @controller = controller
  @injector = injector
end

Instance Attribute Details

#controllerObject (readonly)



13
14
15
# File 'lib/poniard/controller_source.rb', line 13

def controller
  @controller
end

#injectorObject (readonly)



13
14
15
# File 'lib/poniard/controller_source.rb', line 13

def injector
  @injector
end

Instance Method Details

#default(ivars = {}) ⇒ Object

Renders default template for the current action. Equivalent to not call any ‘render` method in a normal Rails controller. Unlike Rails, poniard does not support empty method bodies.

Parameters:

  • ivars (Hash) (defaults to: {})

    instance variables to be set for view rendering



97
98
99
100
101
# File 'lib/poniard/controller_source.rb', line 97

def default(ivars = {})
  ivars.each do |name, val|
    controller.instance_variable_set("@#{name}", val)
  end
end

#head(*args) ⇒ Object

Delegates directly to ActionController::Head#head.



62
63
64
# File 'lib/poniard/controller_source.rb', line 62

def head(*args)
  controller.head *args
end

#redirect_to(path, *args) ⇒ Object

Calls the given route method with arguments, then redirects to the result. _path is automatically appended so does not need to be included in the path. _url suffixes are handled correctly, in that they are used as is without adding a _path suffix. That is a little magic, justified by _path being what you want 90% of the time.

If path is a string, redirect to it as is without calling any route helpers.

Examples:

def index(response)
  response.redirect_to :user, 1 # user_path(1)
end


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/poniard/controller_source.rb', line 34

def redirect_to(path, *args)
  case path
  when Symbol
    unless path.to_s.ends_with?('_url')
      path = "#{path}_path"
    end

    controller.redirect_to(controller.send(path, *args))
  else
    controller.redirect_to path, *args
  end
end

#redirect_to_action(action) ⇒ Object

Redirect to the given action on the current controller.



48
49
50
# File 'lib/poniard/controller_source.rb', line 48

def redirect_to_action(action)
  controller.redirect_to action: action
end

#render(*args) ⇒ Object

Delegates to ‘ActionController::Base#render`, with two exceptions if the last argument is a hash:

  • ivars is deleted from the hash and used to set instance variables that can then be accessed by any templates.

  • headers is deleted from the hash and used to set response headers.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/poniard/controller_source.rb', line 72

def render(*args)
  opts = args.last
  if opts.is_a?(Hash)
    ivars   = opts.delete(:ivars)
    headers = opts.delete(:headers)
  end
  ivars ||= {}
  headers ||= {}

  ivars.each do |name, val|
    controller.instance_variable_set("@#{name}", val)
  end

  headers.each do |name, val|
    controller.headers[name] = val
  end

  controller.render *args
end

#render_action(action, ivars = {}) ⇒ Object

Render the view associated with given action with the given instance variables.

Parameters:

  • action (Symbol/String)

    name of the action

  • ivars (Hash) (defaults to: {})

    instance variables to be set for view rendering



57
58
59
# File 'lib/poniard/controller_source.rb', line 57

def render_action(action, ivars = {})
  render action: action, ivars: ivars
end

#respond_with(klass, *args) ⇒ Object

Object-oriented replacement for ActionController::MimeResponds#respond_to method. The given class is instantiated with the remaining arguments, and is expected to implement a method named after each format it supports. The methods are called via the injector.

If the requested format is not implemented, a 406 “Not Acceptable” response is returned.

Examples:

class MyController
  IndexResponse = Struct.new(:results) do
    def html(response)
      response.default results: results
    end

    def json(response)
      response.render json: results
    end
  end

  def index(response)
    response.respond_with IndexResponse, Things.all
  end
end


128
129
130
131
132
133
134
135
136
# File 'lib/poniard/controller_source.rb', line 128

def respond_with(klass, *args)
  obj = klass.new(*args)
  format = controller.request.format.symbol
  if obj.respond_to?(format)
    injector.dispatch obj.method(format)
  else
    head(406) # Not acceptable
  end
end

#send_data(*args) ⇒ Object

Delegates directly to ActionController::DataStreaming#send_data.



139
140
141
# File 'lib/poniard/controller_source.rb', line 139

def send_data(*args)
  controller.send_data(*args)
end