storybook_rails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionView
4
+ module Storybook
5
+ class StoryConfig
6
+ include ActiveModel::Validations
7
+
8
+ attr_reader :id, :name, :template
9
+ attr_accessor :controls, :parameters, :layout, :content_block
10
+
11
+ validate :valid_controls
12
+
13
+ def initialize(id, name, layout, template)
14
+ @id = id
15
+ @name = name
16
+ @layout = layout
17
+ @template = template
18
+ @controls = []
19
+ end
20
+
21
+ def to_csf_params
22
+ validate!
23
+ csf_params = {
24
+ name: pretty_name(name),
25
+ parameters: {
26
+ server: {
27
+ id: id,
28
+ params: {
29
+ story_name: name
30
+ }
31
+ }
32
+ }
33
+ }
34
+ csf_params.deep_merge!(parameters: parameters) if parameters.present?
35
+ controls.each do |control|
36
+ csf_params.deep_merge!(control.to_csf_params)
37
+ end
38
+ csf_params
39
+ end
40
+
41
+ def values_from_params(params)
42
+ controls.map do |control|
43
+ value = control.value_from_param(params[control.param])
44
+ value = control.value if value.nil? # nil only not falsey
45
+ [control.param, value]
46
+ end.to_h
47
+ end
48
+
49
+ def self.configure(id, name, layout, template, &configuration)
50
+ config = new(id, name, layout, template)
51
+ ActionView::Storybook::Dsl::StoryDsl.evaluate!(config, &configuration)
52
+ config
53
+ end
54
+
55
+ def pretty_name(name)
56
+ name.to_s.humanize.titlecase
57
+ end
58
+
59
+ protected
60
+
61
+ def valid_controls
62
+ controls.reject(&:valid?).each do |control|
63
+ errors.add(:controls, :invalid, value: control)
64
+ end
65
+
66
+ control_names = controls.map(&:name)
67
+ duplicate_names = control_names.group_by(&:itself).map { |k, v| k if v.length > 1 }.compact
68
+ return if duplicate_names.empty?
69
+
70
+ errors.add(:controls, :invalid, message: "Control #{'names'.pluralize(duplicate_names.count)} #{duplicate_names.to_sentence} are repeated")
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :storybook_rails do
4
+ desc "Write CSF JSON stories for all Stories"
5
+ task write_stories_json: :environment do
6
+ puts "Writing Stories JSON"
7
+ ActionView::Storybook::Stories.all.each do |stories|
8
+ json_path = stories.write_csf_json
9
+ puts "#{stories.name} => #{json_path}"
10
+ end
11
+ puts "Done"
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionView
4
+ module Storybook
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,225 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: storybook_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Pence
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: relaxed-rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.9'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.9.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.9.1
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.18.5
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.18.5
153
+ - !ruby/object:Gem::Dependency
154
+ name: simplecov-console
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.9'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.9'
167
+ description: Generate Storybook CSF JSON for rendering Rails View Partials in Storybook
168
+ email:
169
+ - danieldpence@users.noreply.github.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - LICENSE.txt
175
+ - README.md
176
+ - app/controllers/action_view/storybook/stories_controller.rb
177
+ - app/views/action_view/storybook/stories/show.html.erb
178
+ - lib/action_view/storybook.rb
179
+ - lib/action_view/storybook/controls.rb
180
+ - lib/action_view/storybook/controls/array_config.rb
181
+ - lib/action_view/storybook/controls/boolean_config.rb
182
+ - lib/action_view/storybook/controls/color_config.rb
183
+ - lib/action_view/storybook/controls/control_config.rb
184
+ - lib/action_view/storybook/controls/date_config.rb
185
+ - lib/action_view/storybook/controls/number_config.rb
186
+ - lib/action_view/storybook/controls/object_config.rb
187
+ - lib/action_view/storybook/controls/options_config.rb
188
+ - lib/action_view/storybook/controls/text_config.rb
189
+ - lib/action_view/storybook/dsl.rb
190
+ - lib/action_view/storybook/dsl/controls_dsl.rb
191
+ - lib/action_view/storybook/dsl/story_dsl.rb
192
+ - lib/action_view/storybook/engine.rb
193
+ - lib/action_view/storybook/helpers.rb
194
+ - lib/action_view/storybook/helpers/story_params.rb
195
+ - lib/action_view/storybook/stories.rb
196
+ - lib/action_view/storybook/story_config.rb
197
+ - lib/action_view/storybook/tasks/write_stories_json.rake
198
+ - lib/action_view/storybook/version.rb
199
+ homepage: https://github.com/danieldpence/storybook_rails
200
+ licenses:
201
+ - MIT
202
+ metadata:
203
+ allowed_push_host: https://rubygems.org
204
+ homepage_uri: https://github.com/danieldpence/storybook_rails
205
+ source_code_uri: https://github.com/danieldpence/storybook_rails
206
+ post_install_message:
207
+ rdoc_options: []
208
+ require_paths:
209
+ - lib
210
+ required_ruby_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: 2.5.0
215
+ required_rubygems_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ requirements: []
221
+ rubygems_version: 3.1.4
222
+ signing_key:
223
+ specification_version: 4
224
+ summary: Storybook for Rails Partials
225
+ test_files: []