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.



39
40
41
42
# File 'lib/action_view/component/preview.rb', line 39

def all
  load_previews if descendants.empty?
  descendants
end

.call(example) ⇒ Object

Returns the html of the component in its layout



45
46
47
48
49
50
51
# File 'lib/action_view/component/preview.rb', line 45

def call(example)
  example_html = new.public_send(example)

  Rails::ComponentExamplesController.render(template: "examples/show",
                                            layout: @layout || "layouts/application",
                                            assigns: { example: example_html })
end

.componentObject

Returns the component object class associated to the preview.



54
55
56
# File 'lib/action_view/component/preview.rb', line 54

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)


64
65
66
# File 'lib/action_view/component/preview.rb', line 64

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

.examplesObject

Returns all of the available examples for the component preview.



59
60
61
# File 'lib/action_view/component/preview.rb', line 59

def examples
  public_instance_methods(false).map(&:to_s).sort
end

.exists?(preview) ⇒ Boolean

Returns true if the preview exists.

Returns:

  • (Boolean)


69
70
71
# File 'lib/action_view/component/preview.rb', line 69

def exists?(preview)
  all.any? { |p| p.preview_name == preview }
end

.find(preview) ⇒ Object

Find a component preview by its underscored class name.



74
75
76
# File 'lib/action_view/component/preview.rb', line 74

def find(preview)
  all.find { |p| p.preview_name == preview }
end

.layout(layout_name) ⇒ Object

Setter for layout name.



84
85
86
# File 'lib/action_view/component/preview.rb', line 84

def layout(layout_name)
  @layout = layout_name
end

.preview_nameObject

Returns the underscored name of the component preview without the suffix.



79
80
81
# File 'lib/action_view/component/preview.rb', line 79

def preview_name
  name.sub(/Preview$/, "").underscore
end

Instance Method Details

#render(component, *locals) ⇒ Object



33
34
35
# File 'lib/action_view/component/preview.rb', line 33

def render(component, *locals)
  render_inline(component, *locals)
end