Sha256: d7dee7d13256c3112b83f98eea95d619f371ed29922e27f656accf671b278b68

Contents?: true

Size: 1.92 KB

Versions: 12

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "active_support/descendants_tracker"

module ViewComponent # :nodoc:
  class Preview
    include ActionView::Helpers::TagHelper
    extend ActiveSupport::DescendantsTracker

    def render(component, **args, &block)
      { component: component, args: args, block: block }
    end

    class << self
      # Returns all component preview classes.
      def all
        load_previews if descendants.empty?
        descendants
      end

      # Returns the arguments for rendering of the component in its layout
      def render_args(example)
        new.public_send(example).merge(layout: @layout)
      end

      # Returns the component object class associated to the preview.
      def component
        name.chomp("Preview").constantize
      end

      # Returns all of the available examples for the component preview.
      def examples
        public_instance_methods(false).map(&:to_s).sort
      end

      # Returns +true+ if the example of the component preview exists.
      def example_exists?(example)
        examples.include?(example)
      end

      # Returns +true+ if the preview exists.
      def exists?(preview)
        all.any? { |p| p.preview_name == preview }
      end

      # Find a component preview by its underscored class name.
      def find(preview)
        all.find { |p| p.preview_name == preview }
      end

      # Returns the underscored name of the component preview without the suffix.
      def preview_name
        name.chomp("Preview").underscore
      end

      # Setter for layout name.
      def layout(layout_name)
        @layout = layout_name
      end

      private

      def load_previews
        if preview_path
          Dir["#{preview_path}/**/*_preview.rb"].sort.each { |file| require_dependency file }
        end
      end

      def preview_path
        Base.preview_path
      end

      def show_previews
        Base.show_previews
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
view_component-2.4.0 lib/view_component/preview.rb
view_component-2.3.0 lib/view_component/preview.rb
view_component-2.2.2 lib/view_component/preview.rb
view_component-2.2.1 lib/view_component/preview.rb
view_component-2.2.0 lib/view_component/preview.rb
view_component-2.1.0 lib/view_component/preview.rb
view_component-2.0.0 lib/view_component/preview.rb
view_component-1.17.0 lib/view_component/preview.rb
actionview-component-1.17.0 lib/view_component/preview.rb
view_component-1.16.0 lib/view_component/preview.rb
actionview-component-1.16.0 lib/view_component/preview.rb
actionview-component-1.15.0 lib/view_component/preview.rb