Class: RBlade::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/rblade/railtie.rb

Instance Method Summary collapse

Instance Method Details

#setup_component_view_helper(mod) ⇒ 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
# File 'lib/rblade/railtie.rb', line 25

def setup_component_view_helper(mod)
  mod.send(:define_method, RBlade.component_helper_method_name) do |component_name, current_view = nil, slot: "", **attributes, &block|
    # If this is a relative path, prepend with the previous component name's base
    if !current_view.nil? && component_name.start_with?(".")
      component_name = current_view.sub(/[^\.]++\z/, "") + component_name.delete_prefix(".")
    end

    path = RBlade::ComponentStore.find_component_file(component_name)

    # Find the relative template path without the file type
    view_paths.each do |view_path|
      break unless path.delete_prefix!(view_path.to_s).nil?
    end
    path.sub!(/(?:\.[^.]++)?\.rblade\z/, "")

    attributes = RBlade::AttributesManager.new(attributes)

    unless block.nil?
      value = nil
      slot = @output_buffer.capture do
        value = block.call(->(name, **slot_attributes, &slot_block) do
          attributes[name] = RBlade::SlotManager.new(@output_buffer.capture(&slot_block), slot_attributes)
        end)
      end

      if slot.blank?
        slot = value || ""
      end
    end

    render template: path, locals: {slot: RBlade::SlotManager.new(slot), attributes:}
  end
end