Class: Cuprum::Rails::Responses::Html::RenderResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/rails/responses/html/render_response.rb

Overview

Encapsulates an HTML response that renders a given template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, assigns: {}, flash: {}, layout: nil, status: 200) ⇒ RenderResponse

Returns a new instance of RenderResponse.

Parameters:

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

    variables to assign when rendering the template.

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

    the flash messages to set.

  • layout (String) (defaults to: nil)

    the layout to render.

  • status (Integer) (defaults to: 200)

    the HTTP status of the response.

  • template (String, Symbol)

    the template to render.



13
14
15
16
17
18
19
# File 'lib/cuprum/rails/responses/html/render_response.rb', line 13

def initialize(template, assigns: {}, flash: {}, layout: nil, status: 200)
  @assigns  = assigns
  @flash    = flash
  @layout   = layout
  @status   = status
  @template = template
end

Instance Attribute Details

#assignsHash (readonly)

Returns variables to assign when rendering the template.

Returns:

  • (Hash)

    variables to assign when rendering the template.



22
23
24
# File 'lib/cuprum/rails/responses/html/render_response.rb', line 22

def assigns
  @assigns
end

#flashHash (readonly)

Returns the flash messages to set.

Returns:

  • (Hash)

    the flash messages to set.



25
26
27
# File 'lib/cuprum/rails/responses/html/render_response.rb', line 25

def flash
  @flash
end

#layoutString (readonly)

Returns the layout to render.

Returns:

  • (String)

    the layout to render.



28
29
30
# File 'lib/cuprum/rails/responses/html/render_response.rb', line 28

def layout
  @layout
end

#statusInteger (readonly)

Returns the HTTP status of the response.

Returns:

  • (Integer)

    the HTTP status of the response.



31
32
33
# File 'lib/cuprum/rails/responses/html/render_response.rb', line 31

def status
  @status
end

#templateString, Symbol (readonly)

Returns the template to render.

Returns:

  • (String, Symbol)

    the template to render.



34
35
36
# File 'lib/cuprum/rails/responses/html/render_response.rb', line 34

def template
  @template
end

Instance Method Details

#call(renderer) ⇒ Object

Calls the renderer’s #render method with the template and parameters.

Parameters:

  • renderer (#render)

    The context for executing the response, such as a Rails controller.



40
41
42
43
44
45
46
47
48
# File 'lib/cuprum/rails/responses/html/render_response.rb', line 40

def call(renderer)
  assign_flash(renderer)
  assign_variables(renderer)

  options = { status: status }
  options[:layout] = layout if layout

  renderer.render(template, **options)
end