diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cbc10aa..2039b0b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,7 +49,7 @@ jobs: - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} - - name: Build and test with Rake + - name: Build and test with Rspec run: | gem install bundler:1.17.3 bundle update diff --git a/README.md b/README.md index b5193d7..7ca2492 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,13 @@ And a story template to render individual stories: It's up to you how handle rendering your partials in Storybook, but `storybook_rails` will look for a view template that matches the story name (`buttons/button_stories.html.erb` in the example above. In addition, `storybook_rails` provides a `story_params` helper which provides quick access to the params and args specified in the story config. You can use these parameters in your view template to render each story dynamically. Or not. It's up to you. +### Generating Storybook Files +`storybook_rails` includes a Rails generator to make it easy to generate the files outlined in the section above. + +To generate the files above, we could have done this: `bin/rails generate storybook_rails:stories Button primary secondary outline`. + +For more detail, `bin/rails storybook_rails:stories --help`. + ### Generating Storybook Stories JSON Generate the Storybook JSON stories by running the rake task: diff --git a/lib/action_view/storybook/engine.rb b/lib/action_view/storybook/engine.rb index 8b3c69d..17aa7e8 100644 --- a/lib/action_view/storybook/engine.rb +++ b/lib/action_view/storybook/engine.rb @@ -39,7 +39,9 @@ class Engine < Rails::Engine end rake_tasks do + # :nocov: load File.join(__dir__, "tasks/write_stories_json.rake") + # :nocov: end end end diff --git a/lib/generators/storybook_rails/USAGE b/lib/generators/storybook_rails/USAGE new file mode 100644 index 0000000..c15436c --- /dev/null +++ b/lib/generators/storybook_rails/USAGE @@ -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 diff --git a/lib/generators/storybook_rails/stories_generator.rb b/lib/generators/storybook_rails/stories_generator.rb new file mode 100644 index 0000000..8634804 --- /dev/null +++ b/lib/generators/storybook_rails/stories_generator.rb @@ -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 diff --git a/lib/generators/storybook_rails/templates/stories.html.erb.tt b/lib/generators/storybook_rails/templates/stories.html.erb.tt new file mode 100644 index 0000000..d8eb56d --- /dev/null +++ b/lib/generators/storybook_rails/templates/stories.html.erb.tt @@ -0,0 +1 @@ +