Class: ViewComponent::Storybook::Stories

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker
Includes:
ActiveModel::Validations
Defined in:
lib/view_component/storybook/stories.rb

Defined Under Namespace

Classes: ValidationError

Class Method Summary collapse

Class Method Details

.allObject

Returns all component stories classes.



59
60
61
62
# File 'lib/view_component/storybook/stories.rb', line 59

def all
  load_stories if descendants.empty?
  descendants
end

.find_story_config(name) ⇒ Object

find the story by name



80
81
82
# File 'lib/view_component/storybook/stories.rb', line 80

def find_story_config(name)
  story_configs.find { |config| config.name == name.to_sym }
end

.find_story_configs(stories_name) ⇒ Object

Find a component stories by its underscored class name.



70
71
72
# File 'lib/view_component/storybook/stories.rb', line 70

def find_story_configs(stories_name)
  all.find { |stories| stories.stories_name == stories_name }
end

.layout(layout = nil) ⇒ Object



34
35
36
37
38
# 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) ⇒ Object



28
29
30
31
32
# 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) ⇒ Boolean

Returns true if the stories exist.

Returns:

  • (Boolean)


65
66
67
# File 'lib/view_component/storybook/stories.rb', line 65

def stories_exists?(stories_name)
  all.any? { |stories| stories.stories_name == stories_name }
end

.stories_nameObject



54
55
56
# File 'lib/view_component/storybook/stories.rb', line 54

def stories_name
  name.chomp("Stories").underscore
end

.story(name, component = default_component, &block) ⇒ Object



21
22
23
24
25
26
# 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) ⇒ Boolean

Returns true if the story of the component stories exists.

Returns:

  • (Boolean)


75
76
77
# File 'lib/view_component/storybook/stories.rb', line 75

def story_exists?(name)
  story_configs.map(&:name).include?(name.to_sym)
end

.title(title = nil) ⇒ Object



15
16
17
18
19
# 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_paramsObject



40
41
42
43
44
45
46
# 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?Boolean

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

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/view_component/storybook/stories.rb', line 85

def valid?
  # use an instance so we can enjoy the benefits of ActiveModel::Validations
  @validation_instance = new
  @validation_instance.valid?
end

.validate!Object



93
94
95
# File 'lib/view_component/storybook/stories.rb', line 93

def validate!
  valid? || raise(ValidationError, @validation_instance)
end

.write_csf_jsonObject



48
49
50
51
52
# File 'lib/view_component/storybook/stories.rb', line 48

def write_csf_json
  json_path = File.join(stories_path, "#{stories_name}.stories.json")
  File.write(json_path, JSON.pretty_generate(to_csf_params))
  json_path
end