Skip to content

Commit 5a2c3b0

Browse files
committed
refactor and improve app generator
- change name: matestack_app => matestack:app - simplify the generation of an app - change structure of templates
1 parent 0e9f042 commit 5a2c3b0

File tree

7 files changed

+51
-56
lines changed

7 files changed

+51
-56
lines changed

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

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.

spec/lib/generators/matestack_app_generator_spec.rb renamed to spec/lib/generators/matestack/app/app_generator_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require 'generator_spec'
2-
require 'generators/matestack_app/matestack_app_generator'
2+
require 'generators/matestack/app/app_generator'
33

4-
describe MatestackAppGenerator, type: :generator do
5-
let(:dummy) {File.expand_path(File.join(__FILE__, '..', '..', '..', 'dummy'))}
6-
let(:dummy_copy) {File.expand_path(File.join(__FILE__, '..', '..', '..', 'dummy_copy'))}
4+
describe Matestack::Generators::AppGenerator, type: :generator do
5+
let(:dummy) { File.expand_path(File.join(__FILE__, '..', '..', '..', '..', '..', 'dummy')) }
6+
let(:dummy_copy) { File.expand_path(File.join(__FILE__, '..', '..', '..', '..', '..', 'dummy_copy')) }
77

88
before :each do
99
FileUtils.cp_r dummy, dummy_copy
@@ -27,9 +27,9 @@
2727
run_generator %w(my_example_app --all_inclusive)
2828

2929
assert_file "app/matestack/apps/my_example_app.rb", /class Apps::MyExampleApp < Matestack::Ui::App\b/
30-
assert_file "app/matestack/pages/my_example_app/example_page.rb", /class Pages::MyExampleApp::ExamplePage < Matestack::Ui::Page\b/
3130
assert_file "app/controllers/my_example_app_controller.rb", /class MyExampleAppController < ApplicationController\b/
3231
assert_file "config/routes.rb", /my_example_app\b/
32+
assert_file "app/matestack/pages/my_example_app/example_page.rb", /class Pages::MyExampleApp::ExamplePage < Matestack::Ui::Page\b/
3333
end
3434

3535
end

0 commit comments

Comments
 (0)