Skip to content

Commit 1409ad9

Browse files
cllnstimriley
andauthoredJul 12, 2024··
Generate db files during hanami generate slice (#197)
* Add .keep file for app/relations, reorder expectations * Fix comment * Create db/.keep * Alphabetize DB generations * Add base Repo and repos folder * Add base Relation and relations folder * Add base Struct and structs folder * Only generate db/.keep if sqlite * Add --skip-db to generate slice * Add --app-db to generate slice * Add --slice-db to generate slice * Disallow incompatible options * Simplify comment * Separate default value and conflicting options logic * Remove db options * Remove spurious requires Our Zeitwerk autoloading will take care of making these constants available for us. --------- Co-authored-by: Tim Riley <[email protected]>
1 parent 504a060 commit 1409ad9

File tree

9 files changed

+122
-34
lines changed

9 files changed

+122
-34
lines changed
 

‎lib/hanami/cli/commands/app/generate/action.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def call(
8585
skip_view: DEFAULT_SKIP_VIEW,
8686
skip_tests: DEFAULT_SKIP_TESTS, # rubocop:disable Lint/UnusedMethodArgument
8787
slice: nil,
88-
context: nil, **
88+
context: nil,
89+
**
8990
)
9091
slice = inflector.underscore(Shellwords.shellescape(slice)) if slice
9192
name = naming.action_name(name)

‎lib/hanami/cli/commands/app/generate/slice.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ class Slice < Command
1616
argument :name, required: true, desc: "The slice name"
1717
option :url, required: false, type: :string, desc: "The slice URL prefix"
1818

19+
# @since 2.2.0
20+
# @api private
21+
SKIP_DB_DEFAULT = false
22+
private_constant :SKIP_DB_DEFAULT
23+
24+
# @since 2.2.0
25+
# @api private
26+
option :skip_db,
27+
type: :boolean,
28+
required: false,
29+
default: SKIP_DB_DEFAULT,
30+
desc: "Skip database"
31+
1932
example [
2033
"admin # Admin slice (/admin URL prefix)",
2134
"users --url=/u # Users slice (/u URL prefix)",
@@ -34,14 +47,18 @@ def initialize(
3447

3548
# @since 2.0.0
3649
# @api private
37-
def call(name:, url: nil, **)
50+
def call(
51+
name:,
52+
url: nil,
53+
skip_db: SKIP_DB_DEFAULT
54+
)
3855
require "hanami/setup"
3956

4057
app = inflector.underscore(Hanami.app.namespace)
4158
name = inflector.underscore(Shellwords.shellescape(name))
4259
url = sanitize_url_prefix(name, url)
4360

44-
generator.call(app, name, url)
61+
generator.call(app, name, url, skip_db: skip_db)
4562
end
4663

4764
private

‎lib/hanami/cli/generators/app/slice.rb

+13-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ def initialize(fs:, inflector:)
1919

2020
# @since 2.0.0
2121
# @api private
22-
def call(app, slice, url, context: SliceContext.new(inflector, app, slice, url)) # rubocop:disable Metrics/AbcSize
22+
def call(app, slice, url, context: nil, **opts)
23+
context ||= SliceContext.new(inflector, app, slice, url, **opts)
24+
2325
fs.inject_line_at_class_bottom(
2426
fs.join("config", "routes.rb"), "class Routes", t("routes.erb", context).chomp
2527
)
2628

2729
fs.mkdir(directory = "slices/#{slice}")
2830

29-
# fs.write("#{directory}/config/slice.rb", t("slice.erb", context))
3031
fs.write(fs.join(directory, "action.rb"), t("action.erb", context))
3132
fs.write(fs.join(directory, "view.rb"), t("view.erb", context))
3233
fs.write(fs.join(directory, "views", "helpers.rb"), t("helpers.erb", context))
@@ -39,15 +40,21 @@ def call(app, slice, url, context: SliceContext.new(inflector, app, slice, url))
3940
fs.write(fs.join(directory, "assets", "images", "favicon.ico"), file("favicon.ico"))
4041
end
4142

42-
# fs.write(fs.join(directory, "/entities.rb"), t("entities.erb", context))
43-
# fs.write(fs.join(directory, "/repository.rb"), t("repository.erb", context))
43+
if context.generate_db?
44+
fs.write(fs.join(directory, "db", "relation.rb"), t("relation.erb", context))
45+
fs.write(fs.join(directory, "relations", ".keep"), t("keep.erb", context))
46+
47+
fs.write(fs.join(directory, "db", "repo.rb"), t("repo.erb", context))
48+
fs.write(fs.join(directory, "repos", ".keep"), t("keep.erb", context))
49+
50+
fs.write(fs.join(directory, "db", "struct.rb"), t("struct.erb", context))
51+
fs.write(fs.join(directory, "structs", ".keep"), t("keep.erb", context))
52+
end
4453

4554
fs.write(fs.join(directory, "actions/.keep"), t("keep.erb", context))
4655
fs.write(fs.join(directory, "views/.keep"), t("keep.erb", context))
4756
fs.write(fs.join(directory, "templates/.keep"), t("keep.erb", context))
4857
fs.write(fs.join(directory, "templates/layouts/.keep"), t("keep.erb", context))
49-
# fs.write(fs.join(directory, entities/.keep"), t("keep.erb", context))
50-
# fs.write(fs.join(directory, repositories/.keep"), t("keep.erb", context))
5158
end
5259

5360
private
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 DB
5+
class Relation < <%= camelized_app_name %>::DB::Relation
6+
end
7+
end
8+
end
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
module <%= camelized_slice_name %>
4-
class Slice < Hanami::Slice
4+
module DB
5+
class Repo < <%= camelized_app_name %>::DB::Repo
6+
end
57
end
68
end

‎lib/hanami/cli/generators/app/slice/repository.erb

-10
This file was deleted.
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 DB
5+
class Struct < <%= camelized_app_name %>::DB::Struct
6+
end
7+
end
8+
end

‎lib/hanami/cli/generators/app/slice_context.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ module App
1212
class SliceContext < Generators::Context
1313
# @since 2.0.0
1414
# @api private
15-
def initialize(inflector, app, slice, url)
15+
def initialize(inflector, app, slice, url, **options)
1616
@slice = slice
1717
@url = url
18-
super(inflector, app)
18+
super(inflector, app, **options)
1919
end
2020

2121
# @since 2.0.0
@@ -48,6 +48,12 @@ def javascript_erb_tag
4848
%(<%= javascript_tag "app" %>)
4949
end
5050

51+
# @since 2.2.0
52+
# @api private
53+
def generate_db?
54+
!options.fetch(:skip_db, false)
55+
end
56+
5157
private
5258

5359
attr_reader :slice

‎spec/unit/hanami/cli/commands/app/generate/slice_spec.rb

+61-12
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@ class Routes < Hanami::Routes
5353
expect(fs.directory?("slices/#{slice}")).to be(true)
5454
expect(output).to include("Created slices/#{slice}/")
5555

56-
# # Slice
57-
# slice_class = <<~CODE
58-
# # frozen_string_literal: true
59-
#
60-
# module Admin
61-
# class Slice < Hanami::Slice
62-
# end
63-
# end
64-
# CODE
65-
# expect(fs.read("slices/#{slice}/config/slice.rb")).to eq(slice_class)
66-
6756
# Action
6857
action = <<~CODE
6958
# auto_register: false
@@ -77,10 +66,57 @@ class Action < #{app}::Action
7766

7867
expect(fs.read("slices/#{slice}/action.rb")).to eq(action)
7968
expect(output).to include("Created slices/#{slice}/action.rb")
80-
8169
expect(fs.read("slices/#{slice}/actions/.keep")).to eq("")
8270
expect(output).to include("Created slices/#{slice}/actions/.keep")
8371

72+
# Relation
73+
relation = <<~EXPECTED
74+
# frozen_string_literal: true
75+
76+
module Admin
77+
module DB
78+
class Relation < Bookshelf::DB::Relation
79+
end
80+
end
81+
end
82+
EXPECTED
83+
expect(fs.read("slices/admin/db/relation.rb")).to eq(relation)
84+
expect(output).to include("Created slices/admin/db/relation.rb")
85+
expect(fs.read("slices/admin/relations/.keep")).to eq("")
86+
expect(output).to include("Created slices/admin/relations/.keep")
87+
88+
# Repo
89+
repo = <<~EXPECTED
90+
# frozen_string_literal: true
91+
92+
module Admin
93+
module DB
94+
class Repo < Bookshelf::DB::Repo
95+
end
96+
end
97+
end
98+
EXPECTED
99+
expect(fs.read("slices/admin/db/repo.rb")).to eq(repo)
100+
expect(output).to include("Created slices/admin/db/repo.rb")
101+
expect(fs.read("slices/admin/repos/.keep")).to eq("")
102+
expect(output).to include("Created slices/admin/repos/.keep")
103+
104+
# Struct
105+
struct = <<~EXPECTED
106+
# frozen_string_literal: true
107+
108+
module Admin
109+
module DB
110+
class Struct < Bookshelf::DB::Struct
111+
end
112+
end
113+
end
114+
EXPECTED
115+
expect(fs.read("slices/admin/db/struct.rb")).to eq(struct)
116+
expect(output).to include("Created slices/admin/db/struct.rb")
117+
expect(fs.read("slices/admin/structs/.keep")).to eq("")
118+
expect(output).to include("Created slices/admin/structs/.keep")
119+
84120
view = <<~RUBY
85121
# auto_register: false
86122
# frozen_string_literal: true
@@ -267,6 +303,19 @@ class Action < #{app}::Action
267303
end
268304
end
269305

306+
context "with --skip-db" do
307+
it "generates a slice without hanami-db files" do
308+
within_application_directory do
309+
subject.call(name: slice, skip_db: true)
310+
311+
expect(fs.exist?("slices/admin/db")).to be(false)
312+
expect(fs.exist?("slices/admin/repos")).to be(false)
313+
expect(fs.exist?("slices/admin/relations")).to be(false)
314+
expect(fs.exist?("slices/admin/structs")).to be(false)
315+
end
316+
end
317+
end
318+
270319
private
271320

272321
def within_application_directory

0 commit comments

Comments
 (0)
Please sign in to comment.