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.



91
92
93
# File 'lib/jets/controller/rendering.rb', line 91

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

#add_stage(url) ⇒ Object

Example usage:

render json: {success: true, location: add_stage(posts_path)}


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jets/controller/rendering.rb', line 69

def add_stage(url)
  return url unless actual_host

  uri = URI.parse(url)
  # if no location.host, we been provided a relative host
  if !uri.host && actual_host
    url = "/#{url}" unless url.starts_with?('/')
    url = Jets::Controller::Stage.add(actual_host, url)
    actual_host + url
  else
    url
  end
end

#adjust_content_type!(options) ⇒ Object



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

def adjust_content_type!(options)
  return if options.key?(:content_type)

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

#default_layoutObject



52
53
54
55
# File 'lib/jets/controller/rendering.rb', line 52

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



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

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.



60
61
62
63
# File 'lib/jets/controller/rendering.rb', line 60

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



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

def url_for(url)
  add_stage(url)
end