Class: ActionView::Component::Preview

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker
Includes:
TestHelpers
Defined in:
lib/action_view/component/preview.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TestHelpers

#controller, #render_component, #render_inline, #request, #with_variant

Class Method Details

.allObject

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

.componentObject

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.

Returns:

  • (Boolean)


45
46
47
# File 'lib/action_view/component/preview.rb', line 45

def example_exists?(example)
  examples.include?(example)
end

.examplesObject

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.

Returns:

  • (Boolean)


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_nameObject

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