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

lib/generators/matestack_component/templates/matestack_component.js.erb renamed to lib/generators/matestack/component/templates/app/matestack/components/%namespace%/%file_name%.js.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Require your custom component in your application.js before usage
22
// Place it below "//= require matestack-ui-core" using the following line:
3-
//= require <% unless @namespace.nil? %><%= @namespace %>/<% end %><%= file_name %>
3+
//= require <% unless namespace.nil? %><%= namespace %>/<% end %><%= file_name %>
44

5-
MatestackUiCore.Vue.component('custom<% unless @namespace.nil? %>-<%= @namespace %><% end %>-<%= file_name %>', {
5+
MatestackUiCore.Vue.component('custom<% unless namespace.nil? %>-<%= namespace %><% end %>-<%= file_name %>', {
66
mixins: [MatestackUiCore.componentMixin],
77
data: function() {
88
return {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Components::<% unless namespace.nil? %><%= namespace.camelize %>::<% end %><%= class_name %> < Matestack::Ui::<% if dynamic == true %>DynamicComponent<% else %>StaticComponent<% end %>
2+
def prepare
3+
# DB queries and API calls go here
4+
end
5+
6+
def response
7+
components {
8+
# orchestrate existing matestack components here
9+
}
10+
end
11+
end

lib/generators/matestack/page/USAGE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Description:
2+
Stubs out a new matestack page and its route. Pass the page name and --app-name, both under_scored.
3+
4+
To place the page within a namespace, specify the namespace name under_scored and it
5+
will get generated into 'app/matestack/pages/namespace/*'.
6+
7+
To create a custom controller#action to use in the created route, specify it as
8+
under_scored_controller#under_scored_action
9+
10+
This generates a matestack page class in app/matestack/pages/ and adds a route to config/routes.rb
11+
12+
Example 1:
13+
`rails generate matestack:page example_page --app_name example_app`
14+
15+
This will create:
16+
app/matestack/pages/example_app/example_page.rb
17+
18+
This will add to routes:
19+
get 'example_app/example_page', to: 'example_app#example_page'
20+
21+
Example 2:
22+
`rails generate matestack:page example_page --app_name example_app --controller_action=static#second`
23+
24+
This will create:
25+
app/matestack/pages/example_app/example_page.rb
26+
27+
This will add to routes:
28+
get 'example_app/example_page', to: 'static#second'
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module Matestack
2+
module Generators
3+
class PageGenerator < Rails::Generators::NamedBase
4+
source_root File.expand_path('templates', __dir__)
5+
6+
class_option :app_name, type: :string, required: true
7+
class_option :namespace, type: :string
8+
class_option :controller_action, type: :string
9+
class_option :called_by_app_generator, type: :boolean, default: false
10+
11+
def app_name
12+
options[:app_name]
13+
end
14+
15+
def namespace
16+
options[:namespace]
17+
end
18+
19+
def controller_action
20+
if options[:controller_action].nil?
21+
"#{app_name}\##{file_name}"
22+
else
23+
options[:controller_action]
24+
end
25+
end
26+
27+
def create_page
28+
template "app/matestack/pages/%app_name%/%namespace%/%file_name%.rb"
29+
30+
unless options[:called_by_app_generator]
31+
if namespace
32+
route %{get '#{app_name}/#{namespace}/#{file_name}', to: '#{controller_action}'}
33+
else
34+
route %{get '#{app_name}/#{file_name}', to: '#{controller_action}'}
35+
end
36+
37+
puts "Page created! Make sure to add"
38+
puts ""
39+
puts "def #{file_name}"
40+
41+
if namespace
42+
puts " responder_for(Pages::#{app_name.camelize}::#{namespace.camelize}::#{file_name.camelize})"
43+
else
44+
puts " responder_for(Pages::#{app_name.camelize}::#{file_name.camelize})"
45+
end
46+
47+
puts "end"
48+
puts ""
49+
puts "to the desired controller!"
50+
end
51+
end
52+
end
53+
end
54+
end

lib/generators/matestack_page/templates/matestack_page.erb renamed to lib/generators/matestack/page/templates/app/matestack/pages/%app_name%/%namespace%/%file_name%.rb.tt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
class Pages::<% unless @namespace.nil? %><%= @namespace.camelize %>::<% end %><% unless @app_name.nil? %><%= @app_name.camelize %>::<% end %><%= class_name %> < Matestack::Ui::Page
2-
1+
class Pages::<% unless namespace.nil? %><%= namespace.camelize %>::<% end %><% unless app_name.nil? %><%= app_name.camelize %>::<% end %><%= class_name %> < Matestack::Ui::Page
32
# OPTIONAL: Prepare data for the response method
43
# def prepare
54
# API calls and db queries go here
@@ -26,5 +25,4 @@ class Pages::<% unless @namespace.nil? %><%= @namespace.camelize %>::<% end %><%
2625
# # your components go here
2726
# }
2827
# end
29-
3028
end

lib/generators/matestack_app/USAGE

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

lib/generators/matestack_app/matestack_app_generator.rb

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

lib/generators/matestack_component/USAGE

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

0 commit comments

Comments
 (0)