class ViewComponent::Preview
:nodoc:
def all
def all load_previews descendants end
def examples
def examples public_instance_methods(false).map(&:to_s).sort end
def exists?(preview)
def exists?(preview) all.any? { |p| p.preview_name == preview } end
def find(preview)
def find(preview) all.find { |p| p.preview_name == preview } end
def layout(layout_name)
rubocop:disable Style/TrivialAccessors
def layout(layout_name) @layout = layout_name end
def load_previews
def load_previews Array(preview_paths).each do |preview_path| Dir["#{preview_path}/**/*preview.rb"].sort.each { |file| require_dependency file } end end
def preview_example_template_path(example)
def preview_example_template_path(example) preview_path = Array(preview_paths).detect do |path| Dir["#{path}/#{preview_name}_preview/#{example}.html.*"].first end raise MissingPreviewTemplateError.new(example) if preview_path.nil? path = Dir["#{preview_path}/#{preview_name}_preview/#{example}.html.*"].first Pathname.new(path) .relative_path_from(Pathname.new(preview_path)) .to_s .sub(/\..*$/, "") end
def preview_name
def preview_name name.chomp("Preview").underscore end
def preview_paths
def preview_paths Base.preview_paths end
def preview_source(example)
def preview_source(example) source = instance_method(example.to_sym).source.split("\n") source[1...(source.size - 1)].join("\n") end
def render(component, **args, &block)
def render(component, **args, &block) { args: args, block: block, component: component, locals: {}, template: "view_components/preview" } end
def render_args(example, params: {})
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
def render_with_template(template: nil, locals: {})
def render_with_template(template: nil, locals: {}) { template: template, locals: locals } end