Skip to content

Commit 4e005a0

Browse files
Merge pull request #251 from marcoroth/refactor_generators
Refactor and improve generators
2 parents 0b80217 + d9dbbdb commit 4e005a0

File tree

28 files changed

+310
-262
lines changed

28 files changed

+310
-262
lines changed

docs/tooling/generators/matestack_app_generator.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Generates matestack apps to `app/matestack/apps`.
1313
## Example 1
1414

1515
```bash
16-
rails generate matestack_app example_app
16+
rails generate matestack:app example_app
1717
```
1818

1919
Creates an ExampleApp in `app/matestack/apps/example_app.rb`.
2020

2121
## Example 2
2222

2323
```bash
24-
rails generate matestack_app simple_app --all_inclusive
24+
rails generate matestack:app simple_app --all_inclusive
2525
```
2626

2727
Creates:
@@ -32,5 +32,5 @@ Creates:
3232

3333
To see all options, run
3434
```bash
35-
rails generate matestack_app -h
35+
rails generate matestack:app -h
3636
```

docs/tooling/generators/matestack_component_generator.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ Generates matestack components to `app/matestack/components`.
1717
## Example
1818

1919
```bash
20-
rails generate matestack_component simple_component
20+
rails generate matestack:component simple_component
2121
```
2222

2323
Creates a SimpleComponent in `app/matestack/components/simple_component.rb`.
2424

2525
To see all options, run
2626
```bash
27-
rails generate matestack_component -h
27+
rails generate matestack:component -h
2828
```

docs/tooling/generators/matestack_page_generator.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Generates matestack pages to `app/matestack/pages`.
1717
## Example 1
1818

1919
```bash
20-
rails generate matestack_page simple_page --app_name example_app
20+
rails generate matestack:page simple_page --app_name example_app
2121
```
2222

2323
Creates a SimplePage in `app/matestack/pages/example_app/simple_page.rb`.
@@ -33,7 +33,7 @@ in the terminal to use in your controller.
3333
## Example 2
3434

3535
```bash
36-
rails generate matestack_page second_page --app_name example_app --namespace sample_namespace
36+
rails generate matestack:page second_page --app_name example_app --namespace sample_namespace
3737
```
3838

3939
Creates a SimplePage in `app/matestack/pages/example_app/sample_namespace/second_page.rb`.
@@ -48,5 +48,5 @@ in the terminal to use in your, e.g., `example_app_controller.rb`.
4848

4949
To see all options, run
5050
```bash
51-
rails generate matestack_page -h
51+
rails generate matestack:page -h
5252
```

lib/generators/matestack/app/USAGE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Description:
2+
Stubs out a new matestack app. Pass the app name under_scored.
3+
4+
If you want so, pass the option '--all_inclusive' to also create
5+
a corresponding matestack example_page, a controller, its action
6+
and the route in 'config/routes.rb'.
7+
8+
Example 1:
9+
`rails generate matestack:app example_app`
10+
11+
This will create:
12+
app/matestack/apps/example_app.rb
13+
14+
Example 2:
15+
`rails generate matestack:app example_app --all_inclusive`
16+
17+
This will create:
18+
app/matestack/apps/example_app.rb
19+
app/matestack/pages/example_app/example_page.rb
20+
app/controllers/example_app_controller.rb
21+
route get 'example_app/example_page', to: 'example_app#example_page'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Matestack
4+
module Generators
5+
class AppGenerator < Rails::Generators::NamedBase
6+
source_root File.expand_path('templates', __dir__)
7+
8+
class_option :all_inclusive, type: :boolean, default: false
9+
10+
def create_app
11+
template 'app/matestack/apps/%file_name%.rb.tt'
12+
13+
if options[:all_inclusive] == true
14+
template 'app/controllers/%file_name%_controller.rb'
15+
16+
route %{get '#{file_name}/example_page', to: '#{file_name}\#example_page'}
17+
18+
generate "matestack:page example_page --app_name #{file_name} --called_by_app_generator=true"
19+
20+
puts "You can visit your new matestack apps' example page under http://localhost:3000/#{file_name}/example_page"
21+
end
22+
end
23+
end
24+
end
25+
end
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class <%= class_name %>Controller < ApplicationController
2-
32
def example_page
43
responder_for(Pages::<%= class_name %>::ExamplePage)
54
end
6-
75
end

lib/generators/matestack_app/templates/matestack_app.erb renamed to lib/generators/matestack/app/templates/app/matestack/apps/%file_name%.rb.tt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Apps::<%= class_name %> < Matestack::Ui::App
2-
32
# OPTIONAL: Prepare data that gets used on every page of this app
43
# def prepare
54
# API calls and db queries go here
@@ -26,5 +25,4 @@ class Apps::<%= class_name %> < Matestack::Ui::App
2625
# # your components go here
2726
# }
2827
# end
29-
3028
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Description:
2+
Stubs out a new matestack component. Pass the component name, under_scored.
3+
4+
To create a dynamic component, specify it using the --dynamic option.
5+
6+
To place this component within a namespace, specify the namespace name under_scored and it
7+
will get generated into 'app/matestack/components/namespace/*'.
8+
9+
Example 1:
10+
`rails generate matestack:component NAME`
11+
12+
This will create:
13+
app/matestack/components/NAME.rb
14+
15+
Example 2:
16+
`rails generate matestack:component NAME --dynamic --namespace sample_namespace`
17+
18+
This will create:
19+
app/matestack/components/sample_namespace/NAME.rb
20+
app/matestack/components/sample_namespace/NAME.js
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Matestack
2+
module Generators
3+
class ComponentGenerator < Rails::Generators::NamedBase
4+
source_root File.expand_path('templates', __dir__)
5+
6+
class_option :dynamic, type: :boolean, default: false
7+
class_option :scss, type: :boolean, default: false
8+
class_option :haml, type: :boolean, default: false
9+
class_option :namespace, type: :string
10+
11+
def namespace
12+
options[:namespace]
13+
end
14+
15+
def dynamic
16+
options[:dynamic]
17+
end
18+
19+
def create_component
20+
# Future: Check for matestack-compatible namespacing!
21+
22+
template 'app/matestack/components/%namespace%/%file_name%.rb.tt'
23+
template 'app/matestack/components/%namespace%/%file_name%.js.tt' if options[:dynamic]
24+
template 'app/matestack/components/%namespace%/%file_name%.scss.tt' if options[:scss]
25+
template 'app/matestack/components/%namespace%/%file_name%.haml.tt' if options[:haml]
26+
end
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)