Module: Jets::Controller::Rendering

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

Instance Method Summary collapse

Methods included from Redirection

#ensure_protocol, #redirect_to

Instance Method Details

#actual_hostObject



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

def actual_host
  headers["host"]
end

#add_stage_name(url) ⇒ Object

Add API Gateway Stage Name



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

def add_stage_name(url)
  return url unless actual_host

  if actual_host.include?("amazonaws.com") && url.starts_with?('/')
    stage_name = Jets::Resource::ApiGateway::Deployment.stage_name
    url = "/#{stage_name}#{url}"
  end
  url
end

#adjust_content_type!(options) ⇒ Object



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

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

#default_layoutObject



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

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

#ensure_renderObject



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

def ensure_render
  return @rendered_data if @rendered

  # defaults to rendering templates
  Renderers::TemplateRenderer.new(self, managed_options).render
end

#managed_optionsObject



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

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.



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

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"


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

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 = Renderers::TemplateRenderer.new(self, options).render

  @rendered = true
  @rendered_data
end

#url_for(url) ⇒ Object



73
74
75
# File 'lib/jets/controller/rendering.rb', line 73

def url_for(url)
  add_stage_name(url)
end