Class: ActionView::Component::Preview
- Inherits:
-
Object
- Object
- ActionView::Component::Preview
- Extended by:
- ActiveSupport::DescendantsTracker
- Includes:
- TestHelpers
- Defined in:
- lib/action_view/component/preview.rb
Class Method Summary collapse
-
.all ⇒ Object
Returns all component preview classes.
-
.call(example, layout: nil) ⇒ Object
Returns the html of the component in its layout.
-
.component ⇒ Object
Returns the component object class associated to the preview.
-
.example_exists?(example) ⇒ Boolean
Returns
trueif the example of the component preview exists. -
.examples ⇒ Object
Returns all of the available examples for the component preview.
-
.exists?(preview) ⇒ Boolean
Returns
trueif the preview exists. -
.find(preview) ⇒ Object
Find a component preview by its underscored class name.
-
.layout(layout_name) ⇒ Object
Setter for layout name.
-
.preview_name ⇒ Object
Returns the underscored name of the component preview without the suffix.
Instance Method Summary collapse
Methods included from TestHelpers
#controller, #render_component, #render_inline, #request, #with_variant
Class Method Details
.all ⇒ Object
Returns all component preview classes.
17 18 19 20 |
# File 'lib/action_view/component/preview.rb', line 17 def all load_previews if descendants.empty? descendants end |
.call(example, layout: nil) ⇒ Object
Returns the html of the component in its layout
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/action_view/component/preview.rb', line 23 def call(example, layout: nil) example_html = new.public_send(example) if layout.nil? layout = @layout.nil? ? "layouts/application" : @layout end Rails::ComponentExamplesController.render(template: "examples/show", layout: layout, assigns: { example: example_html }) end |
.component ⇒ Object
Returns the component object class associated to the preview.
35 36 37 |
# File 'lib/action_view/component/preview.rb', line 35 def component self.name.sub(%r{Preview$}, "").constantize end |
.example_exists?(example) ⇒ Boolean
Returns true if the example of the component preview exists.
45 46 47 |
# File 'lib/action_view/component/preview.rb', line 45 def example_exists?(example) examples.include?(example) end |
.examples ⇒ Object
Returns all of the available examples for the component preview.
40 41 42 |
# File 'lib/action_view/component/preview.rb', line 40 def examples public_instance_methods(false).map(&:to_s).sort end |
.exists?(preview) ⇒ Boolean
Returns true if the preview exists.
50 51 52 |
# File 'lib/action_view/component/preview.rb', line 50 def exists?(preview) all.any? { |p| p.preview_name == preview } end |
.find(preview) ⇒ Object
Find a component preview by its underscored class name.
55 56 57 |
# File 'lib/action_view/component/preview.rb', line 55 def find(preview) all.find { |p| p.preview_name == preview } end |
.layout(layout_name) ⇒ Object
Setter for layout name.
65 66 67 |
# File 'lib/action_view/component/preview.rb', line 65 def layout(layout_name) @layout = layout_name end |
.preview_name ⇒ Object
Returns the underscored name of the component preview without the suffix.
60 61 62 |
# File 'lib/action_view/component/preview.rb', line 60 def preview_name name.sub(/Preview$/, "").underscore end |
Instance Method Details
#render(component, *locals, &block) ⇒ Object
11 12 13 |
# File 'lib/action_view/component/preview.rb', line 11 def render(component, *locals, &block) render_inline(component, *locals, &block) end |