-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic generator * Adds basic stories files generator * Rubocop * Add desc to generator * Add specs for stories generator * Make Rubocop happy * Coverage * Rework coverage config * Lint * Move usage to its own file * Rename storybook -> storybook_rails * Add section to README * Update README.md
- Loading branch information
1 parent
cca4d9f
commit 2ede47f
Showing
9 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Description: | ||
============ | ||
Generates a [NAME]_stories.rb file and matching view template with | ||
the given NAME (if one does not exist) and optional [story_names]. | ||
|
||
Example: | ||
======== | ||
bin/rails generate storybook_rails:stories Button default primary | ||
|
||
creates a Button story file and view template: | ||
Story File: test/components/button_stories.rb | ||
Template: test/components/button_component.html.erb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails/generators/named_base" | ||
|
||
module StorybookRails | ||
class StoriesGenerator < Rails::Generators::NamedBase | ||
source_root File.expand_path("templates", __dir__) | ||
argument :stories, type: :array, default: [], banner: "stories" | ||
check_class_collision suffix: "Stories" | ||
|
||
def generate_stories_files | ||
template "stories.rb", File.join(stories_path.to_s, "#{file_path}_stories.rb") | ||
template "stories.html.erb", File.join(stories_path.to_s, "#{file_path}_stories.html.erb") | ||
end | ||
|
||
private | ||
|
||
def stories_path | ||
Rails.application.config.storybook_rails.stories_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div>Render the <%= class_name %> partial here.</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class <%= class_name %>Stories < ActionView::Storybook::Stories | ||
<%- stories.each do |story_name| -%> | ||
story(:<%= story_name %>) do | ||
controls do | ||
end | ||
end | ||
|
||
<%- end -%> | ||
end |
62 changes: 62 additions & 0 deletions
62
spec/action_view/generators/storybook_rails/stories_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'fileutils' | ||
require "rails/generators/test_case" | ||
require "generators/storybook_rails/stories_generator" | ||
|
||
Rails.application.load_generators | ||
|
||
class StoriesGeneratorTest < Rails::Generators::TestCase | ||
tests StorybookRails::StoriesGenerator | ||
destination File.expand_path("../tmp", __dir__) | ||
setup :prepare_destination | ||
|
||
arguments %w[Button primary] | ||
|
||
setup do | ||
Rails.application.config.storybook_rails.stories_path = "./" | ||
end | ||
|
||
teardown do | ||
FileUtils.rm_rf(File.expand_path("../tmp", __dir__)) | ||
end | ||
|
||
def test_stories_generator | ||
run_generator | ||
|
||
assert_file "button_stories.rb" do |file| | ||
assert_match(/class ButtonStories < /, file) | ||
assert_match(/story\(:primary\)/, file) | ||
end | ||
|
||
assert_file "button_stories.html.erb" do |file| | ||
assert_match("<div>Render the Button partial here.</div>", file) | ||
end | ||
end | ||
|
||
def test_stories_generator_with_namespace | ||
run_generator %w[buttons/button primary] | ||
|
||
assert_file "buttons/button_stories.rb" do |file| | ||
assert_match(/class Buttons::ButtonStories < /, file) | ||
assert_match(/story\(:primary\)/, file) | ||
end | ||
|
||
assert_file "buttons/button_stories.html.erb" do |file| | ||
assert_match("<div>Render the Buttons::Button partial here.</div>", file) | ||
end | ||
end | ||
|
||
def test_stories_generator_with_namespaced_classname | ||
run_generator %w[Buttons::Button primary] | ||
|
||
assert_file "buttons/button_stories.rb" do |file| | ||
assert_match(/class Buttons::ButtonStories < /, file) | ||
assert_match(/story\(:primary\)/, file) | ||
end | ||
|
||
assert_file "buttons/button_stories.html.erb" do |file| | ||
assert_match("<div>Render the Buttons::Button partial here.</div>", file) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters