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



75
76
77
# File 'lib/jets/controller/rendering.rb', line 75

def actual_host
  headers["host"]
end

#add_stage_name(url) ⇒ Object

Add API Gateway Stage Name



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

def add_stage_name(url)
  return url unless actual_host

  if actual_host.include?("amazonaws.com") && url.starts_with?('/')
    stage_name = [Jets.config.short_env, Jets.config.env_extra].compact.join('_').gsub('-','_') # Stage name only allows a-zA-Z0-9_
    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



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

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

#default_optionsObject



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

def default_options
  layout = self.class.layout.nil? ? default_layout : self.class.layout
  { layout: layout }
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, default_options).render
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.



55
56
57
58
# File 'lib/jets/controller/rendering.rb', line 55

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

  @rendered = true
  @rendered_data
end

#url_for(url) ⇒ Object



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

def url_for(url)
  add_stage_name(url)
end