class ViewComponent::Storybook::Stories
Public Class Methods
all()
click to toggle source
Returns all component stories classes.
# File lib/view_component/storybook/stories.rb, line 61 def all load_stories if descendants.empty? descendants end
find_story_config(name)
click to toggle source
find the story by name
# File lib/view_component/storybook/stories.rb, line 82 def find_story_config(name) story_configs.find { |config| config.name == name.to_sym } end
find_story_configs(stories_name)
click to toggle source
Find a component stories by its underscored class name.
# File lib/view_component/storybook/stories.rb, line 72 def find_story_configs(stories_name) all.find { |stories| stories.stories_name == stories_name } end
layout(layout = nil)
click to toggle source
# File lib/view_component/storybook/stories.rb, line 34 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 = nil)
click to toggle source
# File lib/view_component/storybook/stories.rb, line 28 def parameters(params = nil) # if no argument is passed act like a getter self.stories_parameters = params unless params.nil? stories_parameters end
stories_exists?(stories_name)
click to toggle source
Returns true
if the stories exist.
# File lib/view_component/storybook/stories.rb, line 67 def stories_exists?(stories_name) all.any? { |stories| stories.stories_name == stories_name } end
stories_name()
click to toggle source
# File lib/view_component/storybook/stories.rb, line 56 def stories_name name.chomp("Stories").underscore end
story(name, component = default_component, &block)
click to toggle source
# File lib/view_component/storybook/stories.rb, line 21 def story(name, component = default_component, &block) story_config = StoryConfig.new(story_id(name), name, component, layout, &block) story_config.instance_eval(&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/view_component/storybook/stories.rb, line 77 def story_exists?(name) story_configs.map(&:name).include?(name.to_sym) end
title(title = nil)
click to toggle source
# File lib/view_component/storybook/stories.rb, line 15 def title(title = nil) # if no argument is passed act like a getter self.stories_title = title unless title.nil? stories_title end
to_csf_params()
click to toggle source
# File lib/view_component/storybook/stories.rb, line 40 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/view_component/storybook/stories.rb, line 87 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/view_component/storybook/stories.rb, line 95 def validate! valid? || raise(ValidationError, @validation_instance) end
write_csf_json()
click to toggle source
# File lib/view_component/storybook/stories.rb, line 48 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_component()
click to toggle source
# File lib/view_component/storybook/stories.rb, line 108 def default_component name.chomp("Stories").constantize end
inherited(other)
click to toggle source
Calls superclass method
# File lib/view_component/storybook/stories.rb, line 101 def inherited(other) super(other) # setup class defaults other.stories_title = other.stories_name.humanize.titlecase other.story_configs = [] end
load_stories()
click to toggle source
# File lib/view_component/storybook/stories.rb, line 112 def load_stories Dir["#{stories_path}/**/*_stories.rb"].sort.each { |file| require_dependency file } if stories_path end
stories_path()
click to toggle source
# File lib/view_component/storybook/stories.rb, line 116 def stories_path Storybook.stories_path end
story_id(name)
click to toggle source
# File lib/view_component/storybook/stories.rb, line 120 def story_id(name) "#{stories_name}/#{name.to_s.parameterize}".underscore end
Protected Instance Methods
validate_story_configs()
click to toggle source
# File lib/view_component/storybook/stories.rb, line 127 def validate_story_configs story_configs.reject(&:valid?).each do |story_config| story_errors = story_config.errors.full_messages.join(', ') errors.add(:story_configs, :invalid_story, story_name: story_config.name, story_errors: story_errors) 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? duplicate_name_sentence = duplicate_names.map { |name| "'#{name}'" }.to_sentence errors.add(:story_configs, :duplicate_stories, count: duplicate_names.count, duplicate_names: duplicate_name_sentence) end