Skip to content

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
merged 6 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/tooling/generators/matestack_app_generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ Generates matestack apps to `app/matestack/apps`.
## Example 1

```bash
rails generate matestack_app example_app
rails generate matestack:app example_app
```

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

## Example 2

```bash
rails generate matestack_app simple_app --all_inclusive
rails generate matestack:app simple_app --all_inclusive
```

Creates:
Expand All @@ -32,5 +32,5 @@ Creates:

To see all options, run
```bash
rails generate matestack_app -h
rails generate matestack:app -h
```
4 changes: 2 additions & 2 deletions docs/tooling/generators/matestack_component_generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Generates matestack components to `app/matestack/components`.
## Example

```bash
rails generate matestack_component simple_component
rails generate matestack:component simple_component
```

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

To see all options, run
```bash
rails generate matestack_component -h
rails generate matestack:component -h
```
6 changes: 3 additions & 3 deletions docs/tooling/generators/matestack_page_generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Generates matestack pages to `app/matestack/pages`.
## Example 1

```bash
rails generate matestack_page simple_page --app_name example_app
rails generate matestack:page simple_page --app_name example_app
```

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

```bash
rails generate matestack_page second_page --app_name example_app --namespace sample_namespace
rails generate matestack:page second_page --app_name example_app --namespace sample_namespace
```

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

To see all options, run
```bash
rails generate matestack_page -h
rails generate matestack:page -h
```
21 changes: 21 additions & 0 deletions lib/generators/matestack/app/USAGE
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'
25 changes: 25 additions & 0 deletions lib/generators/matestack/app/app_generator.rb
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
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
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
20 changes: 20 additions & 0 deletions lib/generators/matestack/component/USAGE
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
29 changes: 29 additions & 0 deletions lib/generators/matestack/component/component_generator.rb
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]
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Require your custom component in your application.js before usage
// Place it below "//= require matestack-ui-core" using the following line:
//= require <% unless @namespace.nil? %><%= @namespace %>/<% end %><%= file_name %>
//= require <% unless namespace.nil? %><%= namespace %>/<% end %><%= file_name %>

MatestackUiCore.Vue.component('custom<% unless @namespace.nil? %>-<%= @namespace %><% end %>-<%= file_name %>', {
MatestackUiCore.Vue.component('custom<% unless namespace.nil? %>-<%= namespace %><% end %>-<%= file_name %>', {
mixins: [MatestackUiCore.componentMixin],
data: function() {
return {
Expand Down
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
28 changes: 28 additions & 0 deletions lib/generators/matestack/page/USAGE
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'
54 changes: 54 additions & 0 deletions lib/generators/matestack/page/page_generator.rb
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
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
21 changes: 0 additions & 21 deletions lib/generators/matestack_app/USAGE

This file was deleted.

26 changes: 0 additions & 26 deletions lib/generators/matestack_app/matestack_app_generator.rb

This file was deleted.

Loading