Class: ComponentParty::ActionView::ComponentRenderer

Inherits:
ActionView::TemplateRenderer
  • Object
show all
Defined in:
lib/component_party/action_view/component_renderer.rb

Defined Under Namespace

Classes: TagWrapperDecorator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup_context, component_path) ⇒ ComponentRenderer

Returns a new instance of ComponentRenderer.



8
9
10
11
# File 'lib/component_party/action_view/component_renderer.rb', line 8

def initialize(lookup_context, component_path)
  @component_path = component_path
  super(lookup_context)
end

Instance Attribute Details

#component_pathvoid (readonly)

Returns the value of attribute component_path.



6
7
8
# File 'lib/component_party/action_view/component_renderer.rb', line 6

def component_path
  @component_path
end

#lookup_contextvoid (readonly)

Returns the value of attribute lookup_context.



5
6
7
# File 'lib/component_party/action_view/component_renderer.rb', line 5

def lookup_context
  @lookup_context
end

Instance Method Details

#create_view_model(context, options) ⇒ void



33
34
35
36
37
38
39
40
# File 'lib/component_party/action_view/component_renderer.rb', line 33

def create_view_model(context, options)
  view_model_data = options[:view_model_data] || {}
  view_model_data[:view] = context

  vm_class = find_vm_class(options)

  vm_class.new(view_model_data)
end

#decorate_template(template) ⇒ void



29
30
31
# File 'lib/component_party/action_view/component_renderer.rb', line 29

def decorate_template(template)
  ComponentParty::ActionView::ComponentRenderer::TagWrapperDecorator.new(template, component_path)
end

#find_vm_class(options) ⇒ void



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/component_party/action_view/component_renderer.rb', line 42

def find_vm_class(options)
  if options[:custom_view_model]
    if options[:custom_view_model] == true
      vm_file_path = Pathname.new(options[:component]).join(ComponentParty.configuration.view_model_file_name).to_s
      ActiveSupport::Inflector.camelize(vm_file_path).constantize
    else
      options[:custom_view_model]
    end
  else
    ComponentParty::ViewModel
  end
end

#render(context, options) ⇒ void



13
14
15
16
17
# File 'lib/component_party/action_view/component_renderer.rb', line 13

def render(context, options)
  options[:template] = template_path_from_component_path(options[:component])
  options[:locals] = { vm: create_view_model(context, options) }
  super(context, options)
end

#render_template(template, layout_name = nil, locals = nil) ⇒ void

:nodoc:



20
21
22
# File 'lib/component_party/action_view/component_renderer.rb', line 20

def render_template(context, template, layout_name = nil, locals = nil) #:nodoc:
  super(context, decorate_template(template), layout_name, locals)
end

#template_path_from_component_path(component_path, template_file_name: ComponentParty.configuration.template_file_name) ⇒ void



55
56
57
# File 'lib/component_party/action_view/component_renderer.rb', line 55

def template_path_from_component_path(component_path, template_file_name: ComponentParty.configuration.template_file_name)
  Pathname.new(component_path).join(template_file_name).to_s
end