Skip to content

Commit eb391ca

Browse files
committed
Revert "Prevent generating operation without namespace"
This reverts commit a5bd2f3.
1 parent a5bd2f3 commit eb391ca

File tree

7 files changed

+34
-84
lines changed

7 files changed

+34
-84
lines changed

lib/hanami/cli/errors.rb

-4
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,5 @@ def initialize(scheme)
9191
super("`#{scheme}' is not a supported db scheme")
9292
end
9393
end
94-
95-
# @since x.x.x
96-
# @api public
97-
class NameNeedsNamespaceError < Error; end
9894
end
9995
end

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

+6-14
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ def generate_for_slice(context, slice)
4242

4343
if context.namespaces.any?
4444
fs.mkdir(directory = fs.join(slice_directory, context.namespaces))
45-
fs.write(fs.join(directory, "#{context.name}.rb"), t("slice_operation.erb", context))
45+
fs.write(fs.join(directory, "#{context.name}.rb"), t("nested_slice_operation.erb", context))
4646
else
47-
print_error_message_about_naming(context.name, slice_directory)
47+
fs.mkdir(directory = fs.join(slice_directory))
48+
fs.write(fs.join(directory, "#{context.name}.rb"), t("top_level_slice_operation.erb", context))
4849
end
4950
end
5051

5152
def generate_for_app(context)
5253
if context.namespaces.any?
5354
fs.mkdir(directory = fs.join("app", context.namespaces))
54-
fs.write(fs.join(directory, "#{context.name}.rb"), t("app_operation.erb", context))
55+
fs.write(fs.join(directory, "#{context.name}.rb"), t("nested_app_operation.erb", context))
5556
else
56-
print_error_message_about_naming(context.name, "app")
57+
fs.mkdir(directory = fs.join("app"))
58+
fs.write(fs.join(directory, "#{context.name}.rb"), t("top_level_app_operation.erb", context))
5759
end
5860
end
5961

@@ -65,16 +67,6 @@ def template(path, context)
6567
).result(context.ctx)
6668
end
6769

68-
def print_error_message_about_naming(provided_name, base_location)
69-
raise NameNeedsNamespaceError.new(
70-
"Failed to create operation `#{provided_name}'. " \
71-
"This would create the operation directly in the `#{base_location}/' folder. " \
72-
"Instead, you should provide a namespace for the folder where this operation will live. " \
73-
"NOTE: We recommend giving it a name that's specific to your domain, " \
74-
"but you can also use `operations.#{provided_name}' in the meantime if you're unsure."
75-
)
76-
end
77-
7870
alias_method :t, :template
7971
end
8072
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module <%= camelized_app_name %>
4+
<%= module_namespace_offset %>class <%= camelized_name %> < <%= camelized_app_name %>::Operation
5+
<%= module_namespace_offset %> def call
6+
<%= module_namespace_offset %> end
7+
<%= module_namespace_offset %>end
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module <%= camelized_slice_name %>
4+
<%= module_namespace_offset %>class <%= camelized_name %> < <%= camelized_slice_name %>::Operation
5+
<%= module_namespace_offset %> def call
6+
<%= module_namespace_offset %> end
7+
<%= module_namespace_offset %>end
8+
end

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

+12-66
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,21 @@ def output
1818

1919
context "generating for app" do
2020
it "generates an operation" do
21-
subject.call(name: "operations/add_book")
21+
subject.call(name: "add_book")
2222

2323
operation_file = <<~EXPECTED
2424
# frozen_string_literal: true
2525
2626
module Test
27-
module Operations
28-
class AddBook < Test::Operation
29-
def call
30-
end
27+
class AddBook < Test::Operation
28+
def call
3129
end
3230
end
3331
end
3432
EXPECTED
3533

36-
expect(fs.read("app/operations/add_book.rb")).to eq(operation_file)
37-
expect(output).to include("Created app/operations/add_book.rb")
34+
expect(fs.read("app/add_book.rb")).to eq(operation_file)
35+
expect(output).to include("Created app/add_book.rb")
3836
end
3937

4038
it "generates a operation in a deep namespace with default separator" do
@@ -59,7 +57,7 @@ def call
5957
expect(output).to include("Created app/admin/books/add.rb")
6058
end
6159

62-
it "generates an operation in a deep namespace with slash separator" do
60+
it "generates an operation in a deep namespace with slash separators" do
6361
subject.call(name: "admin/books/add")
6462

6563
operation_file = <<~EXPECTED
@@ -80,78 +78,26 @@ def call
8078
expect(fs.read("app/admin/books/add.rb")).to eq(operation_file)
8179
expect(output).to include("Created app/admin/books/add.rb")
8280
end
83-
84-
it "outputs an error if trying to generate an operation without a separator" do
85-
expect {
86-
subject.call(name: "add_book")
87-
}.to raise_error(Hanami::CLI::NameNeedsNamespaceError).with_message(
88-
"Failed to create operation `add_book'. " \
89-
"This would create the operation directly in the `app/' folder. " \
90-
"Instead, you should provide a namespace for the folder where this operation will live. " \
91-
"NOTE: We recommend giving it a name that's specific to your domain, " \
92-
"but you can also use `operations.add_book' in the meantime if you're unsure."
93-
)
94-
expect(fs.exist?("app/add_book.rb")).to be(false)
95-
end
9681
end
9782

9883
context "generating for a slice" do
99-
it "generates a operation" do
100-
fs.mkdir("slices/main")
101-
subject.call(name: "operations.add_book", slice: "main")
102-
103-
operation_file = <<~EXPECTED
104-
# frozen_string_literal: true
105-
106-
module Main
107-
module Operations
108-
class AddBook < Main::Operation
109-
def call
110-
end
111-
end
112-
end
113-
end
114-
EXPECTED
115-
116-
expect(fs.read("slices/main/operations/add_book.rb")).to eq(operation_file)
117-
expect(output).to include("Created slices/main/operations/add_book.rb")
118-
end
119-
120-
it "generates a operation in a deep namespace with default separator" do
84+
it "generates a operation in a top-level namespace" do
12185
fs.mkdir("slices/main")
122-
subject.call(name: "admin.books.add", slice: "main")
86+
subject.call(name: "add_book", slice: "main")
12387

12488
operation_file = <<~EXPECTED
12589
# frozen_string_literal: true
12690
12791
module Main
128-
module Admin
129-
module Books
130-
class Add < Main::Operation
131-
def call
132-
end
133-
end
92+
class AddBook < Main::Operation
93+
def call
13494
end
13595
end
13696
end
13797
EXPECTED
13898

139-
expect(fs.read("slices/main/admin/books/add.rb")).to eq(operation_file)
140-
expect(output).to include("Created slices/main/admin/books/add.rb")
141-
end
142-
143-
it "outputs an error if trying to generate an operation without a separator" do
144-
fs.mkdir("slices/main")
145-
expect {
146-
subject.call(name: "add_book", slice: "main")
147-
}.to raise_error(Hanami::CLI::NameNeedsNamespaceError).with_message(
148-
"Failed to create operation `add_book'. " \
149-
"This would create the operation directly in the `slices/main/' folder. " \
150-
"Instead, you should provide a namespace for the folder where this operation will live. " \
151-
"NOTE: We recommend giving it a name that's specific to your domain, " \
152-
"but you can also use `operations.add_book' in the meantime if you're unsure."
153-
)
154-
expect(fs.exist?("app/add_book.rb")).to be(false)
99+
expect(fs.read("slices/main/add_book.rb")).to eq(operation_file)
100+
expect(output).to include("Created slices/main/add_book.rb")
155101
end
156102
end
157103
end

0 commit comments

Comments
 (0)