Class: ActionView::Storybook::Stories

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

Class Method Summary collapse

Class Method Details

.allObject

Returns all component stories classes.



52
53
54
55
# File 'lib/action_view/storybook/stories.rb', line 52

def all
  load_stories if descendants.empty?
  descendants
end

.find_stories(stories_name) ⇒ Object

Find a component stories by its underscored class name.



63
64
65
# 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) ⇒ Object

find the story by name



73
74
75
# 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) ⇒ Object



25
26
27
28
29
# 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) ⇒ Object



21
22
23
# File 'lib/action_view/storybook/stories.rb', line 21

def parameters(**params)
  self.parameters = params
end

.stories_exists?(stories_name) ⇒ Boolean

Returns true if the stories exist.

Returns:

  • (Boolean)


58
59
60
# File 'lib/action_view/storybook/stories.rb', line 58

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

.stories_nameObject



47
48
49
# File 'lib/action_view/storybook/stories.rb', line 47

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

.story(name, template = default_template, &block) ⇒ Object



15
16
17
18
19
# 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) ⇒ Boolean

Returns true if the story of the component stories exists.

Returns:

  • (Boolean)


68
69
70
# File 'lib/action_view/storybook/stories.rb', line 68

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

.to_csf_paramsObject



31
32
33
34
35
36
37
# 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?Boolean

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

Returns:

  • (Boolean)


78
79
80
81
82
# 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!Object



86
87
88
# File 'lib/action_view/storybook/stories.rb', line 86

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

.write_csf_jsonObject



39
40
41
42
43
44
45
# 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