Module: Jets::Controller::Rendering

Includes:
Redirection
Included in:
Base
Defined in:
lib/jets/controller/rendering/rack_renderer.rb,
lib/jets/controller/rendering.rb

Overview

Special renderer. All the other renderers lead here

Defined Under Namespace

Classes: RackRenderer

Instance Method Summary collapse

Methods included from Redirection

#ensure_protocol, #redirect_back, #redirect_to

Instance Method Details

#actual_hostObject

Actual host can be headers when cloudfront is in front. Remember to set custom header “origin” header in cloudfront distribution. Can also override with Jets.config.app.domain. The actual_host value is used by redirect_to.



78
79
80
# File 'lib/jets/controller/rendering.rb', line 78

def actual_host
  Jets.config.app.domain || headers["origin"] || headers["host"]
end

#add_stage_name(url) ⇒ Object

Add API Gateway Stage Name



64
65
66
67
68
# File 'lib/jets/controller/rendering.rb', line 64

def add_stage_name(url)
  return url unless actual_host

  Jets::Controller::Stage.add(actual_host, url)
end

#adjust_content_type!(options) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/jets/controller/rendering.rb', line 35

def adjust_content_type!(options)
  if options.key?(:json)
    options[:content_type] = "application/json"
  elsif options.key?(:xml)
    options[:content_type] = "application/xml"
  end
end

#default_layoutObject



50
51
52
53
# File 'lib/jets/controller/rendering.rb', line 50

def default_layout
  application_layout_exist = !Dir.glob("#{Jets.root}/app/views/layouts/application*").empty?
  "application" if application_layout_exist
end

#ensure_renderObject



5
6
7
8
9
10
# File 'lib/jets/controller/rendering.rb', line 5

def ensure_render
  return @rendered_data if @rendered

  # defaults to rendering templates
  RackRenderer.new(self, managed_options).render
end

#managed_optionsObject



43
44
45
46
47
48
# File 'lib/jets/controller/rendering.rb', line 43

def managed_options
  layout = self.class.layout.nil? ? default_layout : self.class.layout
  options = { layout: layout }
  options[:headers] = response.headers unless response.headers.empty?
  options
end

#normalize_options(options, rest) ⇒ Object

Can normalize the options when it is a String or a Symbol Also set defaults here like the layout. Ensure options is a Hash, not a String or Symbol.



58
59
60
61
# File 'lib/jets/controller/rendering.rb', line 58

def normalize_options(options, rest)
  template = options.to_s
  rest.merge(template: template)
end

#render(options = {}, rest = {}) ⇒ Object

Many different ways to render:

render "articles/index", layout: "application"
render :new
render template: "articles/index", layout: "application"
render json: {my: "data"}
render text: "plain text"


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jets/controller/rendering.rb', line 19

def render(options={}, rest={})
  raise "DoubleRenderError" if @rendered

  if options.is_a?(Symbol) or options.is_a?(String)
    options = normalize_options(options, rest)
  end

  options.reverse_merge!(managed_options)
  adjust_content_type!(options)

  @rendered_data = RackRenderer.new(self, options).render

  @rendered = true
  @rendered_data
end

#url_for(url) ⇒ Object



70
71
72
# File 'lib/jets/controller/rendering.rb', line 70

def url_for(url)
  add_stage_name(url)
end