Class: Landable::RenderService

Inherits:
Object
  • Object
show all
Defined in:
app/services/landable/render_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, theme, options = nil) ⇒ RenderService

Returns a new instance of RenderService.



11
12
13
14
15
# File 'app/services/landable/render_service.rb', line 11

def initialize(page, theme, options = nil)
  @page  = page
  @theme = theme
  @options = options || {}
end

Class Method Details

.call(page, options = nil) ⇒ Object



7
8
9
# File 'app/services/landable/render_service.rb', line 7

def self.call(page, options = nil)
  new(page, page.theme, options).render!
end

Instance Method Details

#render!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/landable/render_service.rb', line 17

def render!
  variables = options[:responder].try(:controller).try(:fetch_landable_variables)
  variables ||= {}

  content = render_template(page.body, variables, registers: {
                              page: page,
                              assets: assets_for_page,
                              responder: options[:responder]
                            })

  if layout?
    content = render_template(theme.body, { 'body' => content }, registers: {
                                page: page,
                                assets: assets_for_theme
                              })
  end

  # not completely happy about this
  if options[:preview]
    if page.html?
      # fancy!
      preview_template = File.open(Landable::Engine.root.join('app', 'views', 'templates', 'preview.liquid')).read

      content = render_template(preview_template, 'content' => content,
                                                  'is_redirect' => page.redirect?,
                                                  'is_html' => page.html?,
                                                  'status_code' => page.status_code,
                                                  'redirect_url' => page.redirect_url)
    else
      # non-html stuff just gets rendered as plaintext for a preview
      content = '<pre>' + CGI.escapeHTML(content) + '</pre>'
    end
  end

  content
end