class ActionView::Storybook::Stories

Public Class Methods

all() click to toggle source

Returns all component stories classes.

# File lib/action_view/storybook/stories.rb, line 52
def all
  load_stories if descendants.empty?
  descendants
end
find_stories(stories_name) click to toggle source

Find a component stories by its underscored class name.

# File lib/action_view/storybook/stories.rb, line 63
def find_stories(stories_name)
  all.find { |stories| stories.stories_name == stories_name }
end
find_story(name) click to toggle source

find the story by name

# File lib/action_view/storybook/stories.rb, line 73
def find_story(name)
  story_configs.find { |config| config.name == name.to_sym }
end
layout(layout = nil) click to toggle source
# File lib/action_view/storybook/stories.rb, line 25
def layout(layout = nil)
  # if no argument is passed act like a getter
  self.stories_layout = layout unless layout.nil?
  stories_layout
end
parameters(**params) click to toggle source
# File lib/action_view/storybook/stories.rb, line 21
def parameters(**params)
  self.parameters = params
end
stories_exists?(stories_name) click to toggle source

Returns true if the stories exist.

# File lib/action_view/storybook/stories.rb, line 58
def stories_exists?(stories_name)
  all.any? { |stories| stories.stories_name == stories_name }
end
stories_name() click to toggle source
# File lib/action_view/storybook/stories.rb, line 47
def stories_name
  name.chomp("Stories").underscore
end
story(name, template = default_template, &block) click to toggle source
# File lib/action_view/storybook/stories.rb, line 15
def story(name, template = default_template, &block)
  story_config = StoryConfig.configure(story_id(name), name, layout, template, &block)
  story_configs << story_config
  story_config
end
story_exists?(name) click to toggle source

Returns true if the story of the component stories exists.

# File lib/action_view/storybook/stories.rb, line 68
def story_exists?(name)
  story_configs.map(&:name).include?(name.to_sym)
end
to_csf_params() click to toggle source
# File lib/action_view/storybook/stories.rb, line 31
def to_csf_params
  validate!
  csf_params = { title: title }
  csf_params[:parameters] = parameters if parameters.present?
  csf_params[:stories] = story_configs.map(&:to_csf_params)
  csf_params
end
valid?() click to toggle source

validation - ActiveModel::Validations like but on the class vs the instance

# File lib/action_view/storybook/stories.rb, line 78
def valid?
  # use an instance so we can enjoy the benefits of ActiveModel::Validations
  @validation_instance = new
  @validation_instance.valid?
end
validate!() click to toggle source
# File lib/action_view/storybook/stories.rb, line 86
def validate!
  valid? || raise(ActiveModel::ValidationError, @validation_instance)
end
write_csf_json() click to toggle source
# File lib/action_view/storybook/stories.rb, line 39
def write_csf_json
  json_path = File.join(stories_path, "#{stories_name}.stories.json")
  File.open(json_path, "w") do |f|
    f.write(JSON.pretty_generate(to_csf_params))
  end
  json_path
end

Private Class Methods

default_template() click to toggle source
# File lib/action_view/storybook/stories.rb, line 99
def default_template
  "#{name.chomp('Stories').underscore}_stories"
end
inherited(other) click to toggle source
Calls superclass method
# File lib/action_view/storybook/stories.rb, line 92
def inherited(other)
  super(other)
  # setup class defaults
  other.title = other.stories_name.humanize.titlecase
  other.story_configs = []
end
load_stories() click to toggle source
# File lib/action_view/storybook/stories.rb, line 103
def load_stories
  return unless stories_path

  Dir["#{stories_path}/**/*_stories.rb"].sort.each do |file|
    require_dependency file
  end
end
stories_path() click to toggle source
# File lib/action_view/storybook/stories.rb, line 111
def stories_path
  Storybook.stories_path
end
story_id(name) click to toggle source
# File lib/action_view/storybook/stories.rb, line 115
def story_id(name)
  "#{stories_name}/#{name.to_s.parameterize}".underscore
end

Protected Instance Methods

valid_story_configs() click to toggle source
# File lib/action_view/storybook/stories.rb, line 122
def valid_story_configs
  story_configs.reject(&:valid?).each do |story_config|
    errors.add(:story_configs, :invalid, value: story_config)
  end

  story_names = story_configs.map(&:name)
  duplicate_names = story_names.group_by(&:itself).map { |k, v| k if v.length > 1 }.compact
  return if duplicate_names.empty?

  errors.add(:story_configs, :invalid, message: "Stories #{'names'.pluralize(duplicate_names.count)} #{duplicate_names.to_sentence} are repeated")
end