Class: ViewComponent::Preview
- Inherits:
-
Object
- Object
- ViewComponent::Preview
- Extended by:
- ActiveSupport::DescendantsTracker
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/view_component/preview.rb
Class Method Summary collapse
-
.all ⇒ Object
Returns all component preview classes.
-
.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_example_template_path(example) ⇒ Object
Returns the relative path (from preview_path) to the preview example template if the template exists.
-
.preview_name ⇒ Object
Returns the underscored name of the component preview without the suffix.
-
.render_args(example, params: {}) ⇒ Object
Returns the arguments for rendering of the component in its layout.
Instance Method Summary collapse
- #render(component, **args, &block) ⇒ Object
- #render_with_template(template: nil, locals: {}) ⇒ Object
Class Method Details
.all ⇒ Object
Returns all component preview classes.
29 30 31 32 |
# File 'lib/view_component/preview.rb', line 29 def all load_previews if descendants.empty? descendants end |
.component ⇒ Object
Returns the component object class associated to the preview.
46 47 48 |
# File 'lib/view_component/preview.rb', line 46 def component name.chomp("Preview").constantize end |
.example_exists?(example) ⇒ Boolean
Returns true if the example of the component preview exists.
56 57 58 |
# File 'lib/view_component/preview.rb', line 56 def example_exists?(example) examples.include?(example) end |
.examples ⇒ Object
Returns all of the available examples for the component preview.
51 52 53 |
# File 'lib/view_component/preview.rb', line 51 def examples public_instance_methods(false).map(&:to_s).sort end |
.exists?(preview) ⇒ Boolean
Returns true if the preview exists.
61 62 63 |
# File 'lib/view_component/preview.rb', line 61 def exists?(preview) all.any? { |p| p.preview_name == preview } end |
.find(preview) ⇒ Object
Find a component preview by its underscored class name.
66 67 68 |
# File 'lib/view_component/preview.rb', line 66 def find(preview) all.find { |p| p.preview_name == preview } end |
.layout(layout_name) ⇒ Object
Setter for layout name.
76 77 78 |
# File 'lib/view_component/preview.rb', line 76 def layout(layout_name) @layout = layout_name end |
.preview_example_template_path(example) ⇒ Object
Returns the relative path (from preview_path) to the preview example template if the template exists
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/view_component/preview.rb', line 81 def preview_example_template_path(example) preview_path = Array(preview_paths).detect do |preview_path| Dir["#{preview_path}/#{preview_name}_preview/#{example}.html.*"].first end if preview_path.nil? raise PreviewTemplateError, "preview template for example #{example} does not exist" end path = Dir["#{preview_path}/#{preview_name}_preview/#{example}.html.*"].first Pathname.new(path).relative_path_from(Pathname.new(preview_path)).to_s end |
.preview_name ⇒ Object
Returns the underscored name of the component preview without the suffix.
71 72 73 |
# File 'lib/view_component/preview.rb', line 71 def preview_name name.chomp("Preview").underscore end |
.render_args(example, params: {}) ⇒ Object
Returns the arguments for rendering of the component in its layout
35 36 37 38 39 40 41 42 43 |
# File 'lib/view_component/preview.rb', line 35 def render_args(example, params: {}) example_params_names = instance_method(example).parameters.map(&:last) provided_params = params.slice(*example_params_names).to_h.symbolize_keys result = provided_params.empty? ? new.public_send(example) : new.public_send(example, **provided_params) result ||= {} result[:template] = preview_example_template_path(example) if result[:template].nil? @layout = nil unless defined?(@layout) result.merge(layout: @layout) end |
Instance Method Details
#render(component, **args, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/view_component/preview.rb', line 10 def render(component, **args, &block) { args: args, block: block, component: component, locals: {}, template: "view_components/preview", } end |
#render_with_template(template: nil, locals: {}) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/view_component/preview.rb', line 20 def render_with_template(template: nil, locals: {}) { template: template, locals: locals } end |