Class: ViewComponentLiquid::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component_liquid/template_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ TemplateHandler

Returns a new instance of TemplateHandler.



12
13
14
15
# File 'lib/view_component_liquid/template_handler.rb', line 12

def initialize(view)
  @view = view
  @controller = @view.controller
end

Class Method Details

.call(template, source) ⇒ Object



7
8
9
# File 'lib/view_component_liquid/template_handler.rb', line 7

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

Instance Method Details

#compilable?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/view_component_liquid/template_handler.rb', line 51

def compilable?
  false
end

#filtersObject



38
39
40
41
42
# File 'lib/view_component_liquid/template_handler.rb', line 38

def filters
  if @view.respond_to?(:liquid_filters, true)
    @view.send(:liquid_filters)
  end
end

#registersObject



44
45
46
47
48
49
# File 'lib/view_component_liquid/template_handler.rb', line 44

def registers
  {
    view: @view,
    file_system: ViewComponentLiquid::FileSystem.new(@view)
  }
end

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/view_component_liquid/template_handler.rb', line 17

def render(template, local_assigns={})
  component = LiquidComponent.parse(template)
  template = component.content

  assigns = local_assigns.deep_stringify_keys
  assigns["component"] = component.to_h.deep_stringify_keys
  assigns["controller"] = {
    "controller_name" => @controller.controller_name,
    "action_name" => @controller.action_name
  }
  if @view.respond_to?(:assigns)
    assigns["controller"].merge! @view.assigns.to_h
    @view.instance_variable_set(:@liquid_page, component)
  end

  p "ASSIGNS", assigns

  liquid = Liquid::Template.parse(template)
  liquid.send(render_method, assigns, filters: filters, registers: registers).html_safe
end

#render_methodObject



55
56
57
# File 'lib/view_component_liquid/template_handler.rb', line 55

def render_method
  (Rails.env.development? || Rails.env.test?) ? :render! : :render
end