Skip to content

Commit 0e9f042

Browse files
committed
refactor and improve component generator
- change name: matestack_component => matestack:component - simplify the genration of a component - change structure of templates
1 parent c688fd6 commit 0e9f042

File tree

9 files changed

+51
-56
lines changed

9 files changed

+51
-56
lines changed
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 create_component
16+
@dynamic = options[:dynamic]
17+
18+
# Future: Check for matestack-compatible namespacing!
19+
20+
template 'app/matestack/components/%namespace%/%file_name%.rb.tt'
21+
template 'app/matestack/components/%namespace%/%file_name%.js.tt' if options[:dynamic]
22+
template 'app/matestack/components/%namespace%/%file_name%.scss.tt' if options[:scss]
23+
template 'app/matestack/components/%namespace%/%file_name%.haml.tt' if options[:haml]
24+
end
25+
end
26+
end
27+
end
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Components::<% unless @namespace.nil? %><%= @namespace.camelize %>::<% end %><%= class_name %> < Matestack::Ui::<% if @dynamic == true %>DynamicComponent<% else %>StaticComponent<% end %>
2-
32
def prepare
43
# DB queries and API calls go here
54
end
@@ -9,5 +8,4 @@ class Components::<% unless @namespace.nil? %><%= @namespace.camelize %>::<% end
98
# orchestrate existing matestack components here
109
}
1110
end
12-
1311
end

lib/generators/matestack_component/USAGE

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

lib/generators/matestack_component/matestack_component_generator.rb

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

spec/lib/generators/matestack_component_generator_spec.rb renamed to spec/lib/generators/matestack/component/component_generator_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require "generator_spec"
2-
require 'generators/matestack_component/matestack_component_generator'
2+
require 'generators/matestack/component/component_generator'
33

4-
describe MatestackComponentGenerator, 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::ComponentGenerator, 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

0 commit comments

Comments
 (0)