Class: ViewComponentContrib::Preview::Base
- Inherits:
-
ViewComponent::Preview
- Object
- ViewComponent::Preview
- ViewComponentContrib::Preview::Base
- Includes:
- DefaultTemplate
- Defined in:
- lib/view_component_contrib/preview/base.rb
Overview
Base view component class with extensions already included
Constant Summary collapse
- DEFAULT_CONTAINER_CLASS =
""
Constants included from DefaultTemplate
DefaultTemplate::DEFAULT_TEMPLATE, DefaultTemplate::MISSING_TEMPLATE_ERROR
Class Attribute Summary collapse
-
.component_class_name ⇒ Object
Infer component class name from preview class name: - Namespace::ButtonPreview => Namespace::Button::Component | Namespace::ButtonComponent | Namespace::Button - Button::Preview => Button::Component | ButtonComponent | Button.
- .container_class ⇒ Object
Class Method Summary collapse
-
.inherited(child) ⇒ Object
Support layout inheritance.
- .render_args ⇒ Object
Instance Method Summary collapse
-
#render_component(component_or_props = nil, &block) ⇒ Object
Shortcut for render_with_template(locals: …).
-
#render_with(**locals) ⇒ Object
Shortcut for render_with_template(locals: …).
Methods included from DefaultTemplate
Class Attribute Details
.component_class_name ⇒ Object
Infer component class name from preview class name:
-
Namespace::ButtonPreview => Namespace::Button::Component | Namespace::ButtonComponent | Namespace::Button
-
Button::Preview => Button::Component | ButtonComponent | Button
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/view_component_contrib/preview/base.rb', line 49 def component_class_name @component_class_name ||= begin component_name = name.sub(/(::Preview|Preview)$/, "") [ "#{component_name}::Component", "#{component_name}Component", component_name ].find do _1.safe_constantize end end end |
.container_class ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/view_component_contrib/preview/base.rb', line 27 def container_class return @container_class if defined?(@container_class) @container_class = if superclass.respond_to?(:container_class) superclass.container_class else DEFAULT_CONTAINER_CLASS end end |
Class Method Details
.inherited(child) ⇒ Object
Support layout inheritance
20 21 22 23 |
# File 'lib/view_component_contrib/preview/base.rb', line 20 def inherited(child) child.layout(@layout) if defined?(@layout) super end |
.render_args ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/view_component_contrib/preview/base.rb', line 38 def render_args(...) super.tap do |res| res[:locals] ||= {} build_component_instance(res[:locals]) res[:locals][:container_class] ||= container_class end end |
Instance Method Details
#render_component(component_or_props = nil, &block) ⇒ Object
Shortcut for render_with_template(locals: …)
81 82 83 84 85 86 87 88 89 |
# File 'lib/view_component_contrib/preview/base.rb', line 81 def render_component(component_or_props = nil, &block) component = if component_or_props.is_a?(::ViewComponent::Base) component_or_props else self.class.component_class_name.constantize.new(**(component_or_props || {})) end render_with(component: component, content_block: block) end |
#render_with(**locals) ⇒ Object
Shortcut for render_with_template(locals: …)
76 77 78 |
# File 'lib/view_component_contrib/preview/base.rb', line 76 def render_with(**locals) render_with_template(locals: locals) end |