class Lookbook::PreviewEntity

@api public
Represents a preview class

def after_render_method

Other tags:
    Api: - private
def after_render_method
  fetch_config(:after_render)
end

def default_scenario

Returns:
  • (ScenarioEntity) - The default scenario for this preview

Other tags:
    Example: :ruby -
def default_scenario
  visible_scenarios.first
end

def display_options

Other tags:
    Api: - private
def display_options
  global_options = Lookbook.config.preview_display_options
  global_options.deep_merge(fetch_config(:display_options, {}))
end

def file_name_base

Other tags:
    Api: - private
def file_name_base
  @_file_name_slug ||= file_name(true).gsub(/(_component_preview|component_preview|_preview|preview|component)$/, "")
end

def grouped_scenario_entities

def grouped_scenario_entities
  scenarios = []
  scenario_entities.each.with_index(1) do |entity, i|
    if entity.group.nil?
      entity.default_priority = i
      scenarios << entity
    else
      group_name = entity.group.presence || entity.parent.name.pluralize
      group = scenarios.find do |s|
        s.is_a?(ScenarioGroupEntity) && s.name == Utils.name(group_name)
      end
      if group
        group.add_scenario(entity)
      else
        group = ScenarioGroupEntity.new(group_name, [entity], self)
        group.default_priority = i
        scenarios << group
      end
    end
  end
  scenarios
end

def guess_render_targets

Other tags:
    Api: - private
def guess_render_targets
  [preview_class.name.chomp("Preview").constantize&.name]
rescue
  []
end

def hidden_scenarios

Returns:
  • (Array) - All hidden scenarios for the preview

Other tags:
    Example: :ruby -
def hidden_scenarios
  @_hidden_scenarios ||= ScenarioCollection.new(scenarios.select(&:hidden?))
end

def initialize(code_object)

Other tags:
    Api: - private
def initialize(code_object)
  @code_object = code_object
  @preview_class = code_object.path.constantize
  @file_path = Pathname(code_object.file)
  @base_directories = Engine.preview_paths
  cleaned_path = preview_class.name.underscore.strip
    .gsub(/(_component_preview|_preview)(\..+)?$/, "")
    .gsub(/\/(component_preview|preview|component)(\..+)?$/, "")
  @lookup_path = PathUtils.to_lookup_path(cleaned_path)
end

def inspect_path

Returns:
  • (String) - URL path
def inspect_path
  lookbook_inspect_path(lookup_path)
end

def layout

Other tags:
    Api: - private
def layout
  preview_class.instance_variable_get(:@layout)
end

def load_scenarios

def load_scenarios
  code_object.groups.any? ? grouped_scenario_entities : scenario_entities
end

def preview_class_name

Returns:
  • (String) - Class name
def preview_class_name
  preview_class.name
end

def preview_path

Returns:
  • (String) - URL path
def preview_path
  lookbook_preview_path(lookup_path)
end

def render_target

Other tags:
    Api: - private
def render_target
  render_targets.first
end

def render_targets

Returns:
  • (Array) - All known render targets used in the preview

Other tags:
    Example: :ruby -
def render_targets
  @_render_targets ||= RenderTargetCollection.new(scenarios.flat_map(&:render_targets).uniq(&:lookup_path))
end

def scenario(scenario_name)

Returns:
  • (nil) - if no matching scenario was found
  • (ScenarioEntity) - The matching scenario, if found

Parameters:
  • scenario_name (Symbol, String) -- Name of the scenario

Other tags:
    Example: :ruby -
def scenario(scenario_name)
  scenarios.find { |m| m.name == scenario_name.to_s }
end

def scenario_entities

def scenario_entities
  public_methods = preview_class.public_instance_methods(false)
  method_objects = code_object.meths.select { |m| public_methods.include?(m.name) }
  method_objects.map.with_index { |code_object, i| ScenarioEntity.new(code_object, self, priority: i) }
end

def scenarios

Returns:
  • (Array) - All scenarios for the preview

Other tags:
    Example: :ruby -
def scenarios
  @_scenarios ||= ScenarioCollection.new(load_scenarios)
end

def visible_scenarios

Returns:
  • (Array) - All visible scenarios for the preview

Other tags:
    Example: :ruby -
def visible_scenarios
  @_visible_scenarios ||= ScenarioCollection.new(scenarios.select(&:visible?))
end