Skip to content

Commit

Permalink
Add stories_generator (#3)
Browse files Browse the repository at this point in the history
* 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
danieldpence authored Jun 2, 2021
1 parent cca4d9f commit 2ede47f
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions lib/action_view/storybook/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/generators/storybook_rails/USAGE
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
22 changes: 22 additions & 0 deletions lib/generators/storybook_rails/stories_generator.rb
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Render the <%= class_name %> partial here.</div>
11 changes: 11 additions & 0 deletions lib/generators/storybook_rails/templates/stories.rb.tt
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
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
18 changes: 10 additions & 8 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# frozen_string_literal: true

require 'simplecov'
SimpleCov.start do
command_name "rails#{ENV['RAILS_VERSION']}-ruby#{ENV['RUBY_VERSION']}" if ENV["RUBY_VERSION"]
add_filter 'spec'
add_group 'generators', 'lib/generators'
add_group 'action_view', 'lib/action_view'
end

require "bundler/setup"
require "action_view"
require "action_view/storybook"
require "action_controller"

# Configure Rails Envinronment
# we need to do this before including capybara
Expand All @@ -12,12 +17,9 @@

require "rspec/expectations"
require "rspec/rails"
require 'simplecov'
require 'minitest/autorun'
require 'minitest/spec'
require 'pry'
SimpleCov.start do
command_name "rails#{ENV['RAILS_VERSION']}-ruby#{ENV['RUBY_VERSION']}" if ENV["RUBY_VERSION"]
add_filter 'spec'
end

Dir[File.expand_path(File.join(File.dirname(__FILE__), "support", "**", "*.rb"))].sort.each { |f| require f }

Expand Down

0 comments on commit 2ede47f

Please sign in to comment.