Skip to content

Commit 5804b0e

Browse files
authored
feat: added whole batch generation (#10)
* feat: added whole batch generation * push update * remove gemfile.lock * lint
1 parent 7c54707 commit 5804b0e

7 files changed

Lines changed: 75 additions & 84 deletions

File tree

Gemfile.lock

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

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ All concept files are generated inside the `app/concepts` directory.
1818

1919
To generate files, simply run this command:
2020

21-
$ wizard [--model] [--actions] [--only] [--except] [--context]
22-
23-
| Argument | Type | Presence | Description |
24-
|----------|--------|--------------|-------------------------------------------------------|
25-
| model | String | **Required** | The model. |
26-
| actions | Array | **Required** | The files' names. |
27-
| only | Array | Optional | Only the specified *concept types*. |
28-
| except | Array | Optional | Except the specified *concept types*. |
29-
| context | String | Optional | A directory to group concept files, `nil` by default. |
21+
$ wizard [--model] [--full] [--actions] [--only] [--except] [--context]
22+
23+
| Argument | Type | Presence | Description |
24+
|----------|---------|--------------|--------------------------------------------------------------------------|
25+
| model | String | **Required** | The model. |
26+
| actions | Array | **Required** | The files' names. |
27+
| only | Array | Optional | Only the specified *concept types*. |
28+
| except | Array | Optional | Except the specified *concept types*. |
29+
| context | String | Optional | A directory to group concept files, `nil` by default. |
30+
| full | Boolean | Optional | Generate the whole batch of concepts for this model, `false` by default. |
3031

3132
Allowed concept types are: `operation | finder | form (meant for contracts) | view (meant for representables)`
3233

bin/wizard

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require "wizard"
77
options = {}
88

99
OptionParser.new do |opts|
10-
opts.banner = "Usage: TODO"
10+
opts.banner = "Usage: wizard [options]"
1111

1212
opts.on("-m", "--model MODEL", String, "Target model") do |model|
1313
options[:model] = model
@@ -25,9 +25,13 @@ OptionParser.new do |opts|
2525
options[:except] = concepts
2626
end
2727

28-
opts.on("-c", "--context CONTEXT", String, "Only these concepts") do |context|
28+
opts.on("-c", "--context CONTEXT", String, "Context of these concepts") do |context|
2929
options[:context] = context
3030
end
31+
32+
opts.on("-f", "--[no-]full", "Generate the whole batch of concepts") do |full|
33+
options[:full] = full
34+
end
3135
end.parse!
3236

3337
TrailblazerWizard.generate options[:model], **options

lib/trailblazer_wizard/concept_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def generate(model, name, context = nil)
2121
filename = materials.map { |material| ActiveSupport::Inflector.underscore(material) }.join("/")
2222
filename = "#{TrailblazerWizard.configuration.base_directory}/#{filename}.rb"
2323

24-
false if File.exist?(filename)
24+
return if File.exist?(filename)
2525

2626
content = copy(model, name, context)
2727
create_file(filename, content)

lib/trailblazer_wizard/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module TrailblazerWizard
4-
VERSION = "0.0.6"
4+
VERSION = "0.0.8"
55
end

lib/wizard.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def self.configure
2626
def self.generate(model, **args)
2727
raise StandardError, "[model] arg is required" if model.nil?
2828

29+
return generate_full(model, **args) if args.key?(:full) && args[:full]
30+
2931
raise StandardError, "[actions] arg is required" unless args.key? :actions
3032

3133
concepts = ConceptType::ALL.values
@@ -47,4 +49,21 @@ def self.generate(model, **args)
4749

4850
output
4951
end
52+
53+
def self.generate_full(model, **args)
54+
batch = {
55+
operation: %w[index show create update destroy],
56+
form: %w[create update],
57+
finder: %w[base],
58+
view: %w[index show]
59+
}
60+
61+
batch.each_key do |concept|
62+
batch[concept].each do |action|
63+
file = fetch_generator(concept.to_s).generate(model, action.to_s, args[:context]&.to_s)
64+
65+
puts file if file.length.positive?
66+
end
67+
end
68+
end
5069
end

test/test_trailblazer_wizard.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,42 @@ def test_that_it_uses_alt_types
5858

5959
assert File.exist?("test/tmp/concepts/application/representable/index.rb")
6060
end
61+
62+
def test_that_it_generates_the_full_batch
63+
TrailblazerWizard.generate("TestModel", full: true)
64+
65+
assert File.exist?("test/tmp/concepts/test_model/operation/index.rb")
66+
assert File.exist?("test/tmp/concepts/test_model/operation/show.rb")
67+
assert File.exist?("test/tmp/concepts/test_model/operation/create.rb")
68+
assert File.exist?("test/tmp/concepts/test_model/operation/update.rb")
69+
assert File.exist?("test/tmp/concepts/test_model/operation/destroy.rb")
70+
assert File.exist?("test/tmp/concepts/test_model/form/create.rb")
71+
assert File.exist?("test/tmp/concepts/test_model/form/update.rb")
72+
assert File.exist?("test/tmp/concepts/test_model/view/index.rb")
73+
assert File.exist?("test/tmp/concepts/test_model/view/show.rb")
74+
assert File.exist?("test/tmp/concepts/test_model/finder/base.rb")
75+
end
76+
77+
def test_that_it_generates_the_full_batch_with_context
78+
TrailblazerWizard.generate("TestModel", full: true, context: :admin)
79+
80+
assert File.exist?("test/tmp/concepts/test_model/admin/operation/index.rb")
81+
assert File.exist?("test/tmp/concepts/test_model/admin/operation/show.rb")
82+
assert File.exist?("test/tmp/concepts/test_model/admin/operation/create.rb")
83+
assert File.exist?("test/tmp/concepts/test_model/admin/operation/update.rb")
84+
assert File.exist?("test/tmp/concepts/test_model/admin/operation/destroy.rb")
85+
assert File.exist?("test/tmp/concepts/test_model/admin/form/create.rb")
86+
assert File.exist?("test/tmp/concepts/test_model/admin/form/update.rb")
87+
assert File.exist?("test/tmp/concepts/test_model/admin/view/index.rb")
88+
assert File.exist?("test/tmp/concepts/test_model/admin/view/show.rb")
89+
assert File.exist?("test/tmp/concepts/test_model/admin/finder/base.rb")
90+
end
91+
92+
def test_that_it_ignores_full_generation_if_arg_is_false
93+
error = assert_raises StandardError do
94+
TrailblazerWizard.generate("TestModel", full: false, context: :admin)
95+
end
96+
97+
assert_equal "[actions] arg is required", error.message
98+
end
6199
end

0 commit comments

Comments
 (0)