Class: ViewComponent::Storybook::Stories
- Inherits:
-
Preview
- Object
- Preview
- ViewComponent::Storybook::Stories
- Defined in:
- lib/view_component/storybook/stories.rb
Class Attribute Summary collapse
-
.code_object ⇒ Object
Returns the value of attribute code_object.
-
.stories_json_path ⇒ Object
readonly
Returns the value of attribute stories_json_path.
Class Method Summary collapse
- .control(param, as:, **opts) ⇒ Object
-
.find_story(name) ⇒ Object
find the story by name.
- .layout(layout, only: nil, except: nil) ⇒ Object
- .parameters(params, only: nil, except: nil) ⇒ Object
- .preview_name ⇒ Object
-
.render_args(story_name, params: {}) ⇒ Object
Returns the arguments for rendering of the component in its layout.
- .stories ⇒ Object
- .stories_name ⇒ Object
- .title(title = nil) ⇒ Object
- .to_csf_params ⇒ Object
- .write_csf_json ⇒ Object
Class Attribute Details
.code_object ⇒ Object
Returns the value of attribute code_object.
74 75 76 |
# File 'lib/view_component/storybook/stories.rb', line 74 def code_object @code_object end |
.stories_json_path ⇒ Object (readonly)
Returns the value of attribute stories_json_path.
74 75 76 |
# File 'lib/view_component/storybook/stories.rb', line 74 def stories_json_path @stories_json_path end |
Class Method Details
.control(param, as:, **opts) ⇒ Object
19 20 21 |
# File 'lib/view_component/storybook/stories.rb', line 19 def control(param, as:, **opts) controls.add(param, as: as, **opts) end |
.find_story(name) ⇒ Object
find the story by name
48 49 50 |
# File 'lib/view_component/storybook/stories.rb', line 48 def find_story(name) stories.find { |story| story.name == name.to_sym } end |
.layout(layout, only: nil, except: nil) ⇒ Object
15 16 17 |
# File 'lib/view_component/storybook/stories.rb', line 15 def layout(layout, only: nil, except: nil) layout_collection.add(layout, only: only, except: except) end |
.parameters(params, only: nil, except: nil) ⇒ Object
11 12 13 |
# File 'lib/view_component/storybook/stories.rb', line 11 def parameters(params, only: nil, except: nil) parameters_collection.add(params, only: only, except: except) end |
.preview_name ⇒ Object
27 28 29 |
# File 'lib/view_component/storybook/stories.rb', line 27 def preview_name stories_name end |
.render_args(story_name, params: {}) ⇒ Object
Returns the arguments for rendering of the component in its layout
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/view_component/storybook/stories.rb', line 53 def render_args(story_name, params: {}) # mostly reimplementing the super method but adding logic to parse the params through the controls and find the layout story_params_names = instance_method(story_name).parameters.map(&:last) provided_params = params.slice(*story_params_names).to_h.symbolize_keys story = find_story(story_name) control_parsed_params = provided_params.to_h do |param, value| control = story.controls.find { |c| c.param == param } value = control.parse_param_value(value) if control [param, value] end result = control_parsed_params.empty? ? new.public_send(story_name) : new.public_send(story_name, **control_parsed_params) result ||= {} result[:template] = preview_example_template_path(story_name) if result[:template].nil? @layout = layout_collection.for_story(story_name.to_sym) result.merge(layout: @layout) end |
.stories ⇒ Object
43 44 45 |
# File 'lib/view_component/storybook/stories.rb', line 43 def stories @stories ||= story_names.map { |name| Story.new(story_id(name), name, parameters_collection.for_story(name), controls.for_story(name)) } end |
.stories_name ⇒ Object
23 24 25 |
# File 'lib/view_component/storybook/stories.rb', line 23 def stories_name name.chomp("Stories").underscore end |
.title(title = nil) ⇒ Object
7 8 9 |
# File 'lib/view_component/storybook/stories.rb', line 7 def title(title = nil) @stories_title = title end |
.to_csf_params ⇒ Object
31 32 33 34 35 36 |
# File 'lib/view_component/storybook/stories.rb', line 31 def to_csf_params csf_params = { title: stories_title } csf_params[:parameters] = parameters_collection.for_all if parameters_collection.for_all.present? csf_params[:stories] = stories.map(&:to_csf_params) csf_params end |
.write_csf_json ⇒ Object
38 39 40 41 |
# File 'lib/view_component/storybook/stories.rb', line 38 def write_csf_json File.write(stories_json_path, JSON.pretty_generate(to_csf_params)) stories_json_path end |