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
-
#actual_host ⇒ Object
Actual host can be headers when cloudfront is in front.
-
#add_stage(url) ⇒ Object
Example usage:.
- #adjust_content_type!(options) ⇒ Object
- #default_layout ⇒ Object
- #ensure_render ⇒ Object
- #managed_options ⇒ Object
-
#normalize_options(options, rest) ⇒ Object
Can normalize the options when it is a String or a Symbol Also set defaults here like the layout.
-
#render(options = {}, rest = {}) ⇒ Object
Many different ways to render:.
- #url_for(url) ⇒ Object
Methods included from Redirection
#ensure_protocol, #redirect_back, #redirect_to
Instance Method Details
#actual_host ⇒ Object
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!() return if .key?(:content_type) if .key?(:json) [:content_type] = "application/json" elsif .key?(:xml) [:content_type] = "application/xml" end end |
#default_layout ⇒ Object
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_render ⇒ Object
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, ).render end |
#managed_options ⇒ Object
45 46 47 48 49 50 |
# File 'lib/jets/controller/rendering.rb', line 45 def layout = self.class.layout.nil? ? default_layout : self.class.layout = { layout: layout } [:headers] = response.headers unless response.headers.empty? 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 (, rest) template = .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(={}, rest={}) raise "DoubleRenderError" if @rendered if .is_a?(Symbol) or .is_a?(String) = (, rest) end .reverse_merge!() adjust_content_type!() @rendered_data = RackRenderer.new(self, ).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 |