-
-
Notifications
You must be signed in to change notification settings - Fork 43
Refactor and improve generators #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pascalwengerter
merged 6 commits into
matestack:develop
from
marcoroth:refactor_generators
Nov 5, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3cf4422
add indentation & class_name to templates
pascalwengerter c688fd6
refactor and improve page generator
marcoroth 0e9f042
refactor and improve component generator
marcoroth 5a2c3b0
refactor and improve app generator
marcoroth 59b4758
update generator commands in tooling docs
marcoroth d9dbbdb
add missing tests for the component generator options
marcoroth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Description: | ||
Stubs out a new matestack app. Pass the app name under_scored. | ||
|
||
If you want so, pass the option '--all_inclusive' to also create | ||
a corresponding matestack example_page, a controller, its action | ||
and the route in 'config/routes.rb'. | ||
|
||
Example 1: | ||
`rails generate matestack:app example_app` | ||
|
||
This will create: | ||
app/matestack/apps/example_app.rb | ||
|
||
Example 2: | ||
`rails generate matestack:app example_app --all_inclusive` | ||
|
||
This will create: | ||
app/matestack/apps/example_app.rb | ||
app/matestack/pages/example_app/example_page.rb | ||
app/controllers/example_app_controller.rb | ||
route get 'example_app/example_page', to: 'example_app#example_page' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Matestack | ||
module Generators | ||
class AppGenerator < Rails::Generators::NamedBase | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
class_option :all_inclusive, type: :boolean, default: false | ||
|
||
def create_app | ||
template 'app/matestack/apps/%file_name%.rb.tt' | ||
|
||
if options[:all_inclusive] == true | ||
template 'app/controllers/%file_name%_controller.rb' | ||
|
||
route %{get '#{file_name}/example_page', to: '#{file_name}\#example_page'} | ||
|
||
generate "matestack:page example_page --app_name #{file_name} --called_by_app_generator=true" | ||
|
||
puts "You can visit your new matestack apps' example page under http://localhost:3000/#{file_name}/example_page" | ||
end | ||
end | ||
end | ||
end | ||
end |
2 changes: 0 additions & 2 deletions
2
...pp/templates/matestack_app_controller.erb → .../controllers/%file_name%_controller.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
class <%= class_name %>Controller < ApplicationController | ||
|
||
def example_page | ||
responder_for(Pages::<%= class_name %>::ExamplePage) | ||
end | ||
|
||
end |
28 changes: 28 additions & 0 deletions
28
lib/generators/matestack/app/templates/app/matestack/apps/%file_name%.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Apps::<%= class_name %> < Matestack::Ui::App | ||
# OPTIONAL: Prepare data that gets used on every page of this app | ||
# def prepare | ||
# API calls and db queries go here | ||
# end | ||
|
||
# MANDATORY: This is where your matestack pages get yielded | ||
def response | ||
components { | ||
heading text: 'Welcome to your <%= class_name %> app' | ||
page_content | ||
} | ||
end | ||
|
||
# OPTIONAL: Use partials to render stuff like layout components | ||
# def partial_example | ||
# partial { | ||
# # your components go here | ||
# } | ||
# end | ||
|
||
# OPTIONAL: Slots come after partials | ||
# def slot_example | ||
# slot { | ||
# # your components go here | ||
# } | ||
# end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Description: | ||
Stubs out a new matestack component. Pass the component name, under_scored. | ||
|
||
To create a dynamic component, specify it using the --dynamic option. | ||
|
||
To place this component within a namespace, specify the namespace name under_scored and it | ||
will get generated into 'app/matestack/components/namespace/*'. | ||
|
||
Example 1: | ||
`rails generate matestack:component NAME` | ||
|
||
This will create: | ||
app/matestack/components/NAME.rb | ||
|
||
Example 2: | ||
`rails generate matestack:component NAME --dynamic --namespace sample_namespace` | ||
|
||
This will create: | ||
app/matestack/components/sample_namespace/NAME.rb | ||
app/matestack/components/sample_namespace/NAME.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Matestack | ||
module Generators | ||
class ComponentGenerator < Rails::Generators::NamedBase | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
class_option :dynamic, type: :boolean, default: false | ||
class_option :scss, type: :boolean, default: false | ||
class_option :haml, type: :boolean, default: false | ||
class_option :namespace, type: :string | ||
|
||
def namespace | ||
options[:namespace] | ||
end | ||
|
||
def dynamic | ||
options[:dynamic] | ||
end | ||
|
||
def create_component | ||
# Future: Check for matestack-compatible namespacing! | ||
|
||
template 'app/matestack/components/%namespace%/%file_name%.rb.tt' | ||
template 'app/matestack/components/%namespace%/%file_name%.js.tt' if options[:dynamic] | ||
template 'app/matestack/components/%namespace%/%file_name%.scss.tt' if options[:scss] | ||
template 'app/matestack/components/%namespace%/%file_name%.haml.tt' if options[:haml] | ||
pascalwengerter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
end | ||
end |
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...nent/templates/matestack_component.js.erb → .../components/%namespace%/%file_name%.js.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...tors/matestack/component/templates/app/matestack/components/%namespace%/%file_name%.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Components::<% unless namespace.nil? %><%= namespace.camelize %>::<% end %><%= class_name %> < Matestack::Ui::<% if dynamic == true %>DynamicComponent<% else %>StaticComponent<% end %> | ||
def prepare | ||
# DB queries and API calls go here | ||
end | ||
|
||
def response | ||
components { | ||
# orchestrate existing matestack components here | ||
} | ||
end | ||
end |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Description: | ||
Stubs out a new matestack page and its route. Pass the page name and --app-name, both under_scored. | ||
|
||
To place the page within a namespace, specify the namespace name under_scored and it | ||
will get generated into 'app/matestack/pages/namespace/*'. | ||
|
||
To create a custom controller#action to use in the created route, specify it as | ||
under_scored_controller#under_scored_action | ||
|
||
This generates a matestack page class in app/matestack/pages/ and adds a route to config/routes.rb | ||
|
||
Example 1: | ||
`rails generate matestack:page example_page --app_name example_app` | ||
|
||
This will create: | ||
app/matestack/pages/example_app/example_page.rb | ||
|
||
This will add to routes: | ||
get 'example_app/example_page', to: 'example_app#example_page' | ||
|
||
Example 2: | ||
`rails generate matestack:page example_page --app_name example_app --controller_action=static#second` | ||
|
||
This will create: | ||
app/matestack/pages/example_app/example_page.rb | ||
|
||
This will add to routes: | ||
get 'example_app/example_page', to: 'static#second' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Matestack | ||
module Generators | ||
class PageGenerator < Rails::Generators::NamedBase | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
class_option :app_name, type: :string, required: true | ||
class_option :namespace, type: :string | ||
class_option :controller_action, type: :string | ||
class_option :called_by_app_generator, type: :boolean, default: false | ||
|
||
def app_name | ||
options[:app_name] | ||
end | ||
|
||
def namespace | ||
options[:namespace] | ||
end | ||
|
||
def controller_action | ||
if options[:controller_action].nil? | ||
"#{app_name}\##{file_name}" | ||
else | ||
options[:controller_action] | ||
end | ||
end | ||
|
||
def create_page | ||
template "app/matestack/pages/%app_name%/%namespace%/%file_name%.rb" | ||
|
||
unless options[:called_by_app_generator] | ||
if namespace | ||
route %{get '#{app_name}/#{namespace}/#{file_name}', to: '#{controller_action}'} | ||
else | ||
route %{get '#{app_name}/#{file_name}', to: '#{controller_action}'} | ||
end | ||
|
||
puts "Page created! Make sure to add" | ||
puts "" | ||
puts "def #{file_name}" | ||
|
||
if namespace | ||
puts " responder_for(Pages::#{app_name.camelize}::#{namespace.camelize}::#{file_name.camelize})" | ||
else | ||
puts " responder_for(Pages::#{app_name.camelize}::#{file_name.camelize})" | ||
end | ||
|
||
puts "end" | ||
puts "" | ||
puts "to the desired controller!" | ||
end | ||
end | ||
end | ||
end | ||
end |
28 changes: 28 additions & 0 deletions
28
...ors/matestack/page/templates/app/matestack/pages/%app_name%/%namespace%/%file_name%.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Pages::<% unless namespace.nil? %><%= namespace.camelize %>::<% end %><% unless app_name.nil? %><%= app_name.camelize %>::<% end %><%= class_name %> < Matestack::Ui::Page | ||
# OPTIONAL: Prepare data for the response method | ||
# def prepare | ||
# API calls and db queries go here | ||
# end | ||
|
||
# MANDATORY: This is where the matestack magic happens | ||
def response | ||
components { | ||
heading text: 'Welcome to your <%= class_name %> page' | ||
page_content | ||
} | ||
end | ||
|
||
# OPTIONAL: Use partials to render stuff like layout components | ||
# def partial_example | ||
# partial { | ||
# # your components go here | ||
# } | ||
# end | ||
|
||
# OPTIONAL: Slots come after partials | ||
# def slot_example | ||
# slot { | ||
# # your components go here | ||
# } | ||
# end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.