Skip to content

Commit 06bf0f6

Browse files
authored
Merge pull request #55 from komposable/implement-custom-destroy-on-component
Implement custom destroy for component generator
2 parents 735572b + e18b557 commit 06bf0f6

File tree

99 files changed

+1700
-309
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1700
-309
lines changed

.rubocop.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ AllCops:
55
- 'node_modules/**/*'
66
- 'vendor/**/*'
77
- 'tmp/**/*'
8+
- 'fixtures/**/*'
89

910
Gemspec/OrderedDependencies:
1011
Enabled: true

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
sudo: false
12
language: ruby
23
rvm:
34
- 2.4

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Upcoming release
44

5+
**Enhancements:**
6+
- Custom destroy for the component generator: you can now safely run `rails d component button`
7+
58
**Bug fixes:**
69
- Fix crash when `nil` is passed to a component
710

features/component_generator.feature

+312
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
Feature: Component generator
2+
3+
Background:
4+
Given I use a fixture named "my_app"
5+
When I run `bundle install`
6+
7+
Scenario: Generate component
8+
When I run `rails generate component AwesomeButton`
9+
And I cd to "frontend/components"
10+
Then the following files should exist:
11+
| awesome_button/_awesome_button.html.erb |
12+
| awesome_button/awesome_button.scss |
13+
| awesome_button/awesome_button.js |
14+
| awesome_button/awesome_button_component.rb |
15+
And the file named "index.js" should contain:
16+
"""
17+
import "components/awesome_button/awesome_button";
18+
"""
19+
20+
Scenario: Generate namespaced component
21+
When I run `rails generate component admin/sub_admin/AwesomeButton`
22+
And I cd to "frontend/components"
23+
Then the following files should exist:
24+
| admin/index.js |
25+
| admin/sub_admin/index.js |
26+
| admin/sub_admin/awesome_button/_admin_sub_admin_awesome_button.html.erb |
27+
| admin/sub_admin/awesome_button/admin_sub_admin_awesome_button.scss |
28+
| admin/sub_admin/awesome_button/admin_sub_admin_awesome_button.js |
29+
| admin/sub_admin/awesome_button/admin_sub_admin_awesome_button_component.rb |
30+
And the file named "index.js" should contain:
31+
"""
32+
import "components/admin";
33+
"""
34+
And the file named "admin/index.js" should contain:
35+
"""
36+
import "components/admin/sub_admin";
37+
"""
38+
And the file named "admin/sub_admin/index.js" should contain:
39+
"""
40+
import "components/admin/sub_admin/awesome_button/admin_sub_admin_awesome_button";
41+
"""
42+
43+
Scenario: `imports` in JavaScript files are sorted alphabetically when generating component
44+
When I cd to "frontend/components"
45+
And a file named "index.js" with:
46+
"""
47+
console.log("HELLO WORLD");
48+
"""
49+
Then the file named "index.js" should contain:
50+
"""
51+
console.log("HELLO WORLD");
52+
"""
53+
When I run `rails generate component button`
54+
Then the file named "index.js" should contain:
55+
"""
56+
import "components/button/button";
57+
"""
58+
When I run `rails generate component awesome_button`
59+
Then the file named "index.js" should contain:
60+
"""
61+
import "components/awesome_button/awesome_button";
62+
import "components/button/button";
63+
"""
64+
Scenario: Generate component with `erb` template engine
65+
When I run `rails generate component AwesomeButton`
66+
And I cd to "frontend/components/awesome_button"
67+
Then a file named "_awesome_button.html.erb" should exist
68+
69+
Scenario: Generate component with custom template engine defined to `haml`
70+
When I append to "Gemfile" with:
71+
"""
72+
73+
gem 'haml-rails'
74+
"""
75+
And I run `bundle install`
76+
And I run `rails generate component AwesomeButton`
77+
And I cd to "frontend/components/awesome_button"
78+
Then a file named "_awesome_button.html.haml" should exist
79+
80+
Scenario: Generate component with custom template engine defined to `slim`
81+
When I append to "Gemfile" with:
82+
"""
83+
84+
gem 'slim-rails'
85+
"""
86+
And I run `bundle install`
87+
And I run `rails generate component AwesomeButton`
88+
And I cd to "frontend/components/awesome_button"
89+
Then a file named "_awesome_button.html.slim" should exist
90+
91+
Scenario: Generate component with `scss` stylesheet engine
92+
When I run `rails generate component AwesomeButton`
93+
And I cd to "frontend/components/awesome_button"
94+
Then a file named "awesome_button.scss" should exist
95+
And the file named "awesome_button.js" should contain:
96+
"""
97+
import "./awesome_button.scss";
98+
"""
99+
100+
Scenario: Generate component with custom stylesheet engine defined to `scss`
101+
Given a file named "config/initializers/custom_configuration.rb" with:
102+
"""
103+
Rails.application.config.generators.stylesheet_engine = :sass
104+
"""
105+
When I run `rails generate component AwesomeButton`
106+
And I cd to "frontend/components/awesome_button"
107+
Then a file named "awesome_button.scss" should exist
108+
And the file named "awesome_button.js" should contain:
109+
"""
110+
import "./awesome_button.scss";
111+
"""
112+
113+
Scenario: Generate component with custom stylesheet engine defined to `sass`
114+
Given a file named "config/initializers/custom_configuration.rb" with:
115+
"""
116+
Rails.application.config.sass.preferred_syntax = :sass
117+
"""
118+
When I run `rails generate component AwesomeButton`
119+
And I cd to "frontend/components/awesome_button"
120+
Then a file named "awesome_button.sass" should exist
121+
And the file named "awesome_button.js" should contain:
122+
"""
123+
import "./awesome_button.sass";
124+
"""
125+
126+
Scenario: Generate component with custom stylesheet engine defined to `css`
127+
Given I remove "sass-rails" gem
128+
When I run `bundle install`
129+
And I run `rails generate component AwesomeButton`
130+
And I cd to "frontend/components/awesome_button"
131+
Then a file named "awesome_button.css" should exist
132+
And the file named "awesome_button.js" should contain:
133+
"""
134+
import "./awesome_button.css";
135+
"""
136+
137+
Scenario: Generate component with `--locale` option
138+
When I run `rails generate component AwesomeButton --locale`
139+
And I cd to "frontend/components"
140+
Then a file named "awesome_button/awesome_button.en.yml" should exist
141+
142+
Scenario: Generate component with `--locale` option and additional `fr` locale
143+
Given a file named "config/initializers/custom_configuration.rb" with:
144+
"""
145+
Rails.application.config.i18n.available_locales = [:en, :fr]
146+
"""
147+
When I run `rails generate component AwesomeButton --locale`
148+
And I cd to "frontend/components/awesome_button"
149+
Then the following files should exist:
150+
| awesome_button.en.yml |
151+
| awesome_button.fr.yml |
152+
153+
Scenario: Generate component with `--stimulus` option
154+
When I run `rails generate komponent:install --stimulus`
155+
And I cd to "frontend"
156+
Then the following files should exist:
157+
| stimulus_application.js |
158+
And I run `rails generate component AwesomeButton --stimulus`
159+
And I cd to "components"
160+
Then the following files should exist:
161+
| awesome_button/_awesome_button.html.erb |
162+
| awesome_button/awesome_button.scss |
163+
| awesome_button/awesome_button.js |
164+
| awesome_button/awesome_button_controller.js |
165+
| awesome_button/awesome_button_component.rb |
166+
And the file named "awesome_button/awesome_button_controller.js" should contain:
167+
"""
168+
import { Controller } from "stimulus";
169+
"""
170+
171+
Scenario: Component with namespaces and stimulus
172+
When I run `rails generate komponent:install --stimulus`
173+
And I cd to "frontend"
174+
Then the following files should exist:
175+
| stimulus_application.js |
176+
And I run `rails generate component admin/sub_admin/AwesomeButton --stimulus`
177+
And I cd to "components"
178+
Then the following files should exist:
179+
| admin/sub_admin/awesome_button/_admin_sub_admin_awesome_button.html.erb |
180+
| admin/sub_admin/awesome_button/admin_sub_admin_awesome_button.scss |
181+
| admin/sub_admin/awesome_button/admin_sub_admin_awesome_button.js |
182+
| admin/sub_admin/awesome_button/admin_sub_admin_awesome_button_controller.js |
183+
| admin/sub_admin/awesome_button/admin_sub_admin_awesome_button_component.rb |
184+
And the file named "admin/sub_admin/awesome_button/admin_sub_admin_awesome_button_controller.js" should contain:
185+
"""
186+
import { Controller } from "stimulus";
187+
"""
188+
189+
Scenario: Destroy component
190+
When I cd to "frontend/components"
191+
And I run `rails generate component button`
192+
Then the file named "index.js" should contain:
193+
"""
194+
import "components/button/button";
195+
"""
196+
When I run `rails destroy component button`
197+
Then the file named "index.js" should not contain:
198+
"""
199+
import "components/button/button";
200+
"""
201+
202+
Scenario: Destroy namespaced component
203+
When I cd to "frontend/components"
204+
And I run `rails generate component admin/button`
205+
Then the file named "index.js" should contain:
206+
"""
207+
import "components/admin";
208+
"""
209+
And a file named "admin/index.js" should exist
210+
And the file named "admin/index.js" should contain:
211+
"""
212+
import "components/admin/button/admin_button";
213+
"""
214+
When I run `rails destroy component admin/button`
215+
Then the file named "index.js" should not contain:
216+
"""
217+
import "components/admin";
218+
"""
219+
And a file named "admin/index.js" should not exist
220+
221+
Scenario: Destroy namespaced components
222+
When I cd to "frontend/components"
223+
And I run `rails generate component admin/button`
224+
Then the file named "index.js" should contain:
225+
"""
226+
import "components/admin";
227+
"""
228+
And a file named "admin/index.js" should exist
229+
And the file named "admin/index.js" should contain:
230+
"""
231+
import "components/admin/button/admin_button";
232+
"""
233+
When I run `rails generate component admin/tag`
234+
Then the file named "index.js" should contain:
235+
"""
236+
import "components/admin";
237+
"""
238+
And a file named "admin/index.js" should exist
239+
And the file named "admin/index.js" should contain:
240+
"""
241+
import "components/admin/button/admin_button";
242+
import "components/admin/tag/admin_tag";
243+
"""
244+
When I run `rails destroy component admin/button`
245+
Then the file named "index.js" should contain:
246+
"""
247+
import "components/admin";
248+
"""
249+
And a file named "admin/index.js" should exist
250+
Then the file named "admin/index.js" should contain:
251+
"""
252+
import "components/admin/tag/admin_tag";
253+
"""
254+
And the file named "admin/index.js" should not contain:
255+
"""
256+
import "components/admin/button/admin_button";
257+
"""
258+
259+
Scenario: Destroy complex namespaced components
260+
When I cd to "frontend/components"
261+
And I run `rails generate component admin/super/button`
262+
Then the file named "index.js" should contain:
263+
"""
264+
import "components/admin";
265+
"""
266+
And a file named "admin/index.js" should exist
267+
And the file named "admin/index.js" should contain:
268+
"""
269+
import "components/admin/super";
270+
"""
271+
And a file named "admin/super/index.js" should exist
272+
And the file named "admin/super/index.js" should contain:
273+
"""
274+
import "components/admin/super/button/admin_super_button";
275+
"""
276+
When I run `rails generate component admin/super/tag`
277+
Then the file named "index.js" should contain:
278+
"""
279+
import "components/admin";
280+
"""
281+
And the file named "admin/index.js" should contain:
282+
"""
283+
import "components/admin/super";
284+
"""
285+
And a file named "admin/super/index.js" should exist
286+
And a file named "admin/super/index.js" should contain:
287+
"""
288+
import "components/admin/super/button/admin_super_button";
289+
import "components/admin/super/tag/admin_super_tag";
290+
"""
291+
When I run `rails destroy component admin/super/button`
292+
Then the file named "index.js" should contain:
293+
"""
294+
import "components/admin";
295+
"""
296+
And a file named "admin/index.js" should exist
297+
Then the file named "admin/index.js" should contain:
298+
"""
299+
import "components/admin/super";
300+
"""
301+
And a file named "admin/super/index.js" should exist
302+
Then the file named "admin/super/index.js" should contain:
303+
"""
304+
import "components/admin/super/tag/admin_super_tag";
305+
"""
306+
When I run `rails destroy component admin/super/tag`
307+
And a file named "admin/super/index.js" should not exist
308+
And a file named "admin/index.js" should not exist
309+
Then the file named "index.js" should not contain:
310+
"""
311+
import "components/admin";
312+
"""

features/component_generator/alphabetically_sorting_imports.feature

-32
This file was deleted.

0 commit comments

Comments
 (0)