Skip to content

Commit 1023225

Browse files
committed
Add recommendation to add namespace to operations
1 parent eb391ca commit 1023225

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lib/hanami/cli/files.rb

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def chdir(path, &blk)
4242
super
4343
end
4444

45+
# @since x.x.x
46+
# @api private
47+
def recommend(message)
48+
out.puts(" Recommendation: #{message}")
49+
end
50+
4551
private
4652

4753
attr_reader :out

lib/hanami/cli/generators/app/operation.rb

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def generate_for_slice(context, slice)
4646
else
4747
fs.mkdir(directory = fs.join(slice_directory))
4848
fs.write(fs.join(directory, "#{context.name}.rb"), t("top_level_slice_operation.erb", context))
49+
fs.recommend("Add a namespace to operation names, so they go into a folder within #{directory}/.")
4950
end
5051
end
5152

@@ -56,6 +57,7 @@ def generate_for_app(context)
5657
else
5758
fs.mkdir(directory = fs.join("app"))
5859
fs.write(fs.join(directory, "#{context.name}.rb"), t("top_level_app_operation.erb", context))
60+
fs.recommend("Add a namespace to operation names, so they go into a folder within #{directory}/.")
5961
end
6062
end
6163

spec/unit/hanami/cli/commands/app/generate/operation_spec.rb

+27-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def output
1717
end
1818

1919
context "generating for app" do
20-
it "generates an operation" do
20+
it "generates an operation without a namespace, with a recommendation" do
2121
subject.call(name: "add_book")
2222

2323
operation_file = <<~EXPECTED
@@ -33,6 +33,7 @@ def call
3333

3434
expect(fs.read("app/add_book.rb")).to eq(operation_file)
3535
expect(output).to include("Created app/add_book.rb")
36+
expect(output).to include(" Recommendation: Add a namespace to operation names, so they go into a folder within app/")
3637
end
3738

3839
it "generates a operation in a deep namespace with default separator" do
@@ -81,7 +82,7 @@ def call
8182
end
8283

8384
context "generating for a slice" do
84-
it "generates a operation in a top-level namespace" do
85+
it "generates a operation in a top-level namespace, with recommendation" do
8586
fs.mkdir("slices/main")
8687
subject.call(name: "add_book", slice: "main")
8788

@@ -98,6 +99,30 @@ def call
9899

99100
expect(fs.read("slices/main/add_book.rb")).to eq(operation_file)
100101
expect(output).to include("Created slices/main/add_book.rb")
102+
expect(output).to include(" Recommendation: Add a namespace to operation names, so they go into a folder within slices/main/")
103+
end
104+
105+
it "generates a operation in a nested namespace" do
106+
fs.mkdir("slices/main")
107+
subject.call(name: "admin.books.add", slice: "main")
108+
109+
operation_file = <<~EXPECTED
110+
# frozen_string_literal: true
111+
112+
module Main
113+
module Admin
114+
module Books
115+
class Add < Main::Operation
116+
def call
117+
end
118+
end
119+
end
120+
end
121+
end
122+
EXPECTED
123+
124+
expect(fs.read("slices/main/admin/books/add.rb")).to eq(operation_file)
125+
expect(output).to include("Created slices/main/admin/books/add.rb")
101126
end
102127
end
103128
end

0 commit comments

Comments
 (0)