Class: Lesmok::Railing::ActionViewHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/lesmok/railing/action_view_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ ActionViewHandler

Returns a new instance of ActionViewHandler.



21
22
23
# File 'lib/lesmok/railing/action_view_handler.rb', line 21

def initialize(view)
  @view = view
end

Class Method Details

.call(template) ⇒ Object



17
18
19
# File 'lib/lesmok/railing/action_view_handler.rb', line 17

def self.call(template)
  "#{self}.new(self).render(#{template.source.inspect}, local_assigns)"
end

.lesmok_optionsObject



13
14
15
# File 'lib/lesmok/railing/action_view_handler.rb', line 13

def self.lesmok_options
  @lesmok_options ||= {}
end

Instance Method Details

#compilable?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/lesmok/railing/action_view_handler.rb', line 69

def compilable?
  false
end

#log_any_liquid_errors(errors, type_str = 'error') ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/lesmok/railing/action_view_handler.rb', line 60

def log_any_liquid_errors(errors, type_str = 'error')
  return if errors.blank?
  log = ::Rails.logger
  log.warn "[Lesmok] Template #{type_str}s (#{errors.size}) detected!"
  errors.each do |err|
    log.debug "  -- Liquid #{type_str}: #{err}"
  end
end

#render(template, local_assigns = {}) ⇒ Object



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
53
54
55
56
57
58
# File 'lib/lesmok/railing/action_view_handler.rb', line 25

def render(template, local_assigns = {})
  @view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'

  assigns = @view.assigns

  if @view.content_for?(:layout)
    assigns["content_for_layout"] = @view.content_for(:layout)
  end
  assigns.merge!(local_assigns.stringify_keys)

  controller = @view.controller
  filters = if controller.respond_to?(:liquid_filters, true)
              controller.send(:liquid_filters)
            elsif controller.respond_to?(:master_helper_module)
              [controller.master_helper_module]
            else
              [controller._helpers]
            end

  liquid = ::Liquid::Template.parse(template)
  render_assigns = assigns.with_indifferent_access
  render_opts = {:filters => filters, :registers => {:action_view => @view, :controller => @view.controller}}
  if self.class.lesmok_options[:rethrow_errors]
    text = liquid.render!(render_assigns, render_opts)
  else
    text = liquid.render(render_assigns, render_opts)
  end
  if ::Lesmok.config.debugging?
    log_any_liquid_errors(liquid.errors)
    log_any_liquid_errors(liquid.warnings, 'warning')
  end

  text.html_safe
end