Skip to content

Commit ff1b07b

Browse files
author
Nicholas Barone
committed
Generators for installation and example view component
1 parent 422afea commit ff1b07b

File tree

8 files changed

+284
-15
lines changed

8 files changed

+284
-15
lines changed

Gemfile.lock

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,76 @@ PATH
22
remote: .
33
specs:
44
rails-storybook (0.1.0)
5+
rails (~> 7.0)
6+
slim (~> 5.2)
57

68
GEM
79
remote: https://rubygems.org/
810
specs:
11+
actioncable (7.1.3.4)
12+
actionpack (= 7.1.3.4)
13+
activesupport (= 7.1.3.4)
14+
nio4r (~> 2.0)
15+
websocket-driver (>= 0.6.1)
16+
zeitwerk (~> 2.6)
17+
actionmailbox (7.1.3.4)
18+
actionpack (= 7.1.3.4)
19+
activejob (= 7.1.3.4)
20+
activerecord (= 7.1.3.4)
21+
activestorage (= 7.1.3.4)
22+
activesupport (= 7.1.3.4)
23+
mail (>= 2.7.1)
24+
net-imap
25+
net-pop
26+
net-smtp
27+
actionmailer (7.1.3.4)
28+
actionpack (= 7.1.3.4)
29+
actionview (= 7.1.3.4)
30+
activejob (= 7.1.3.4)
31+
activesupport (= 7.1.3.4)
32+
mail (~> 2.5, >= 2.5.4)
33+
net-imap
34+
net-pop
35+
net-smtp
36+
rails-dom-testing (~> 2.2)
37+
actionpack (7.1.3.4)
38+
actionview (= 7.1.3.4)
39+
activesupport (= 7.1.3.4)
40+
nokogiri (>= 1.8.5)
41+
racc
42+
rack (>= 2.2.4)
43+
rack-session (>= 1.0.1)
44+
rack-test (>= 0.6.3)
45+
rails-dom-testing (~> 2.2)
46+
rails-html-sanitizer (~> 1.6)
47+
actiontext (7.1.3.4)
48+
actionpack (= 7.1.3.4)
49+
activerecord (= 7.1.3.4)
50+
activestorage (= 7.1.3.4)
51+
activesupport (= 7.1.3.4)
52+
globalid (>= 0.6.0)
53+
nokogiri (>= 1.8.5)
54+
actionview (7.1.3.4)
55+
activesupport (= 7.1.3.4)
56+
builder (~> 3.1)
57+
erubi (~> 1.11)
58+
rails-dom-testing (~> 2.2)
59+
rails-html-sanitizer (~> 1.6)
60+
activejob (7.1.3.4)
61+
activesupport (= 7.1.3.4)
62+
globalid (>= 0.3.6)
63+
activemodel (7.1.3.4)
64+
activesupport (= 7.1.3.4)
65+
activerecord (7.1.3.4)
66+
activemodel (= 7.1.3.4)
67+
activesupport (= 7.1.3.4)
68+
timeout (>= 0.4.0)
69+
activestorage (7.1.3.4)
70+
actionpack (= 7.1.3.4)
71+
activejob (= 7.1.3.4)
72+
activerecord (= 7.1.3.4)
73+
activesupport (= 7.1.3.4)
74+
marcel (~> 1.0)
975
activesupport (7.1.3.4)
1076
base64
1177
bigdecimal
@@ -19,13 +85,19 @@ GEM
1985
ast (2.4.2)
2086
base64 (0.2.0)
2187
bigdecimal (3.1.8)
88+
builder (3.3.0)
2289
concurrent-ruby (1.3.3)
2390
connection_pool (2.4.1)
91+
crass (1.0.6)
92+
date (3.3.4)
2493
debug (1.9.2)
2594
irb (~> 1.10)
2695
reline (>= 0.3.8)
2796
diff-lcs (1.5.1)
2897
drb (2.2.1)
98+
erubi (1.13.0)
99+
globalid (1.2.1)
100+
activesupport (>= 6.1)
29101
i18n (1.14.5)
30102
concurrent-ruby (~> 1.0)
31103
io-console (0.7.2)
@@ -34,8 +106,34 @@ GEM
34106
reline (>= 0.4.2)
35107
json (2.7.2)
36108
language_server-protocol (3.17.0.3)
109+
loofah (2.22.0)
110+
crass (~> 1.0.2)
111+
nokogiri (>= 1.12.0)
112+
mail (2.8.1)
113+
mini_mime (>= 0.1.1)
114+
net-imap
115+
net-pop
116+
net-smtp
117+
marcel (1.0.4)
118+
mini_mime (1.1.5)
119+
mini_portile2 (2.8.7)
37120
minitest (5.24.1)
38121
mutex_m (0.2.0)
122+
net-imap (0.4.14)
123+
date
124+
net-protocol
125+
net-pop (0.1.2)
126+
net-protocol
127+
net-protocol (0.2.2)
128+
timeout
129+
net-smtp (0.5.0)
130+
net-protocol
131+
nio4r (2.7.3)
132+
nokogiri (1.16.7)
133+
mini_portile2 (~> 2.8.2)
134+
racc (~> 1.4)
135+
nokogiri (1.16.7-x86_64-darwin)
136+
racc (~> 1.4)
39137
parallel (1.25.1)
40138
parser (3.3.4.0)
41139
ast (~> 2.4.1)
@@ -44,6 +142,42 @@ GEM
44142
stringio
45143
racc (1.8.1)
46144
rack (3.1.7)
145+
rack-session (2.0.0)
146+
rack (>= 3.0.0)
147+
rack-test (2.1.0)
148+
rack (>= 1.3)
149+
rackup (2.1.0)
150+
rack (>= 3)
151+
webrick (~> 1.8)
152+
rails (7.1.3.4)
153+
actioncable (= 7.1.3.4)
154+
actionmailbox (= 7.1.3.4)
155+
actionmailer (= 7.1.3.4)
156+
actionpack (= 7.1.3.4)
157+
actiontext (= 7.1.3.4)
158+
actionview (= 7.1.3.4)
159+
activejob (= 7.1.3.4)
160+
activemodel (= 7.1.3.4)
161+
activerecord (= 7.1.3.4)
162+
activestorage (= 7.1.3.4)
163+
activesupport (= 7.1.3.4)
164+
bundler (>= 1.15.0)
165+
railties (= 7.1.3.4)
166+
rails-dom-testing (2.2.0)
167+
activesupport (>= 5.0.0)
168+
minitest
169+
nokogiri (>= 1.6)
170+
rails-html-sanitizer (1.6.0)
171+
loofah (~> 2.21)
172+
nokogiri (~> 1.14)
173+
railties (7.1.3.4)
174+
actionpack (= 7.1.3.4)
175+
activesupport (= 7.1.3.4)
176+
irb
177+
rackup (>= 1.0.0)
178+
rake (>= 12.2)
179+
thor (~> 1.0, >= 1.2.2)
180+
zeitwerk (~> 2.6)
47181
rainbow (3.1.1)
48182
rake (13.2.1)
49183
rdoc (6.7.0)
@@ -96,11 +230,23 @@ GEM
96230
rubocop-performance
97231
rubocop-rails
98232
ruby-progressbar (1.13.0)
233+
slim (5.2.1)
234+
temple (~> 0.10.0)
235+
tilt (>= 2.1.0)
99236
stringio (3.1.1)
100237
strscan (3.1.0)
238+
temple (0.10.3)
239+
thor (1.3.1)
240+
tilt (2.4.0)
241+
timeout (0.4.1)
101242
tzinfo (2.0.6)
102243
concurrent-ruby (~> 1.0)
103244
unicode-display_width (2.5.0)
245+
webrick (1.8.1)
246+
websocket-driver (0.7.6)
247+
websocket-extensions (>= 0.1.0)
248+
websocket-extensions (0.1.5)
249+
zeitwerk (2.6.17)
104250

105251
PLATFORMS
106252
ruby
@@ -109,6 +255,7 @@ PLATFORMS
109255
DEPENDENCIES
110256
bundler (~> 2.5)
111257
debug (~> 1.6)
258+
rails (~> 7.0)
112259
rails-storybook!
113260
rake (~> 13.2)
114261
rspec (~> 3.13)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'rails/generators'
2+
3+
class Storybook::ExampleGenerator < Rails::Generators::Base
4+
def install_view_components
5+
insert_into_file "Gemfile", "gem 'view_component'"
6+
system "bundle install"
7+
end
8+
9+
def create_example_component
10+
create_file "app/components/example_component.rb", <<~RUBY
11+
# frozen_string_literal: true
12+
13+
class ExampleComponent < ViewComponent::Base
14+
slim_template <<~SLIM
15+
span
16+
= @title
17+
SLIM
18+
19+
def initialize(title:)
20+
@title = title
21+
end
22+
end
23+
RUBY
24+
end
25+
26+
def create_example_component_preview
27+
create_file "test/components/previews/example_component_preview.rb", <<~RUBY
28+
# frozen_string_literal: true
29+
30+
class ExampleComponentPreview < ViewComponent::Preview
31+
layout "storybook"
32+
def default
33+
render(ExampleComponent.new(title: "title"))
34+
end
35+
36+
def with_content_block
37+
render(ExampleComponent.new(title: "This component accepts a block of content")) do
38+
tag.div do
39+
content_tag(:span, "Hello")
40+
end
41+
end
42+
end
43+
end
44+
RUBY
45+
end
46+
47+
def create_example_story
48+
create_file "stories/example_component.stories.json", <<~JSON
49+
{
50+
"title": "ExampleComponent",
51+
"stories": [
52+
{
53+
"name": "default",
54+
"parameters": {
55+
"server": { "id": "example_component/default" }
56+
}
57+
}
58+
]
59+
}
60+
JSON
61+
end
62+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'rails/generators'
2+
3+
class Storybook::InstallGenerator < Rails::Generators::Base
4+
def install_storybook
5+
system "npx storybook@latest init --yes --dev --no-dev --builder webpack5"
6+
system "yarn add @storybook/server --dev"
7+
system "yarn add @storybook/server-webpack5 --dev"
8+
system "yarn install"
9+
insert_into_file "Procfile.dev", "storybook: yarn storybook -p 6006"
10+
end
11+
12+
def install_rack_cors
13+
insert_into_file "Gemfile", " gem 'rack-cors'\n", after: "group :development do\n"
14+
system "bundle install"
15+
create_file "config/initializers/cors.rb", <<~RUBY
16+
Rails.application.config.middleware.insert_before 0, Rack::Cors do
17+
allow do
18+
origins '*'
19+
resource '*', headers: :any, methods: [:get, :post, :patch, :put]
20+
end
21+
end
22+
RUBY
23+
end
24+
25+
def create_storybook_layout_slim
26+
create_file "app/views/layouts/storybook.html.slim", <<~SLIM
27+
doctype html
28+
html lang='en'
29+
head
30+
title Trainyard
31+
meta charset="UTF-8"
32+
meta[name="viewport" content="width=device-width,initial-scale=1"]
33+
= csrf_meta_tags
34+
= csp_meta_tag
35+
= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload"
36+
= stylesheet_link_tag "application"
37+
= javascript_pack_tag "application"
38+
body
39+
div
40+
main.ml-64.p-8
41+
== yield
42+
SLIM
43+
end
44+
45+
def update_storybook
46+
# Main.js
47+
insert_into_file ".storybook/main.js", "\n \"@storybook/server\",", after: "addons: ["
48+
gsub_file '.storybook/main.js', "*.stories.@(js|jsx|mjs|ts|tsx)", "*.stories.@(js|jsx|mjs|ts|tsx|json)"
49+
gsub_file '.storybook/main.js', "@storybook/react-webpack5", "@storybook/server-webpack5"
50+
51+
# Preview.js
52+
base_url = "http://localhost:3000/rails/view_components"
53+
insert_into_file ".storybook/preview.js", "\n server: { url: \"#{base_url}\", },", after: "parameters: {"
54+
55+
# insert_into_file ".storybook/main.js", "@storybook/addon-controls", after: "addons: ["
56+
end
57+
end

lib/rails/storybook.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/rails/storybook/version.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/storybook.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "storybook/version"
2+
3+
module Storybook
4+
class Error < StandardError; end
5+
# Your code goes here...
6+
end

lib/storybook/version.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
module Storybook
3+
VERSION = "0.1.0"
4+
end
5+

rails-storybook.gemspec

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
lib = File.expand_path("../lib", __FILE__)
33
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require "rails/storybook/version"
4+
require "storybook/version"
55

66
Gem::Specification.new do |spec|
77
spec.name = "rails-storybook"
8-
spec.version = Rails::Storybook::VERSION
8+
spec.version = Storybook::VERSION
99
spec.authors = [ "Nicholas Barone" ]
1010
spec.email = [ "[email protected]" ]
1111

@@ -23,6 +23,11 @@ Gem::Specification.new do |spec|
2323
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2424
spec.require_paths = [ "lib" ]
2525

26+
27+
spec.add_dependency "rails", "~> 7.0"
28+
spec.add_dependency "slim", "~> 5.2"
29+
spec.add_development_dependency "rails", "~> 7.0"
30+
2631
spec.add_development_dependency "bundler", "~> 2.5"
2732
spec.add_development_dependency "rake", "~> 13.2"
2833
spec.add_development_dependency "rspec", "~> 3.13"

0 commit comments

Comments
 (0)