Preview Components that require helpers #955
-
Hey Thanks for the work on this gem. Works great and we are starting to test it out. Some of our old view code uses gems such as This gives us a problem during previews, as we can't call the method a controller would look like this class AwesomeController < ApplicationController
def index
add_breadcrumb "Super awesome title", root_path
# .. and so on
end
end The component would be a simple wrapper component that delegates to the helper <div class="breadcrumbs_wrapper">
<%= render_breadcrumbs separator: ' > ' %>
</div> The preview would be: class BreadcrumbsComponentPreview < ViewComponent::Preview
include Rails.application.routes.url_helpers
def default
add_breadcrumb "Awesome Title", root_path
render BreadcrumbsComponent.new
end
end Any idea on how to get the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Gonna answer my own questions by throwing code at it: # frozen_string_literal: true
class Settings::BreadcrumbsComponent < ApplicationComponent
def initialize(breadcrumbs: [], **options)
@breadcrumbs = breadcrumbs.map { |b| wrap_element(b) }
@options = options.merge(
separator: ' <span class=\'seperator\'>></span> '
)
end
def breadcrumbs
builder.render.html_safe
end
protected
def builder
@builder ||= BreadcrumbsOnRails::Breadcrumbs::SimpleBuilder.new(self, @breadcrumbs || helpers.breadcrumbs, @options)
end
def wrap_element(elm)
return elm unless elm.is_a? Array
BreadcrumbsOnRails::Breadcrumbs::Element.new(b.first, b.last)
end
end |
Beta Was this translation helpful? Give feedback.
Gonna answer my own questions by throwing code at it: