Skip to content

Commit c688fd6

Browse files
committed
refactor and improve page generator
- change name: matestack_page => matestack:page - simplify the genration of a page - change structure of templates - add tests for the options --namespace and --controller_action
1 parent 3cf4422 commit c688fd6

File tree

7 files changed

+133
-100
lines changed

7 files changed

+133
-100
lines changed

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_page/USAGE

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

lib/generators/matestack_page/matestack_page_generator.rb

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'generator_spec'
2+
require 'generators/matestack/page/page_generator'
3+
4+
describe Matestack::Generators::PageGenerator, 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')) }
7+
8+
before :each do
9+
FileUtils.cp_r dummy, dummy_copy
10+
end
11+
12+
after :each do
13+
FileUtils.rm_rf dummy
14+
FileUtils.cp_r dummy_copy, dummy
15+
FileUtils.rm_rf dummy_copy
16+
end
17+
18+
destination Rails.root
19+
20+
it "creates example page" do
21+
run_generator %w(my_example_page --app_name my_app)
22+
23+
assert_file "app/matestack/pages/my_app/my_example_page.rb", /class Pages::MyApp::MyExamplePage < Matestack::Ui::Page\b/
24+
assert_file "config/routes.rb", /my_app#my_example_page/
25+
end
26+
27+
it 'create example page with namespace' do
28+
run_generator %w(my_example_page --app_name my_app --namespace sample_namespace)
29+
30+
assert_file "app/matestack/pages/my_app/sample_namespace/my_example_page.rb", /class Pages::SampleNamespace::MyApp::MyExamplePage < Matestack::Ui::Page\b/
31+
assert_file "config/routes.rb", %r{my_app/sample_namespace/my_example_page}
32+
assert_file "config/routes.rb", /my_app#my_example_page/
33+
end
34+
35+
it 'create example page with controller_action' do
36+
run_generator %w(my_example_page --app_name my_app --controller_action custom#action)
37+
38+
assert_file "app/matestack/pages/my_app/my_example_page.rb", /class Pages::MyApp::MyExamplePage < Matestack::Ui::Page\b/
39+
assert_file "config/routes.rb", /custom#action/
40+
end
41+
42+
it 'create example page with namespace and controller_action' do
43+
run_generator %w(my_example_page --app_name my_app --namespace sample_namespace --controller_action custom#action)
44+
45+
assert_file "app/matestack/pages/my_app/sample_namespace/my_example_page.rb", /class Pages::SampleNamespace::MyApp::MyExamplePage < Matestack::Ui::Page\b/
46+
assert_file "config/routes.rb", %r{my_app/sample_namespace/my_example_page}
47+
assert_file "config/routes.rb", /custom#action/
48+
end
49+
50+
end

spec/lib/generators/matestack_page_generator_spec.rb

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

0 commit comments

Comments
 (0)