roda-project 0.1.18 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0b645d449bd52931506a9e9ee0ef203d14f8b6606cee831d1570bae7e8b8e66
4
- data.tar.gz: ca8c1717d95f3bb0aebe8f698df9dee7306eb687b2c7d763a2f6d3d4ea46a746
3
+ metadata.gz: 84a58b65b141c3d2088c07052ff66b90a020e4eec2318ae616152e0a180faad9
4
+ data.tar.gz: 8d0e6f6081add8077fcdbdadc7a03224073b1b846c685d0cd33238e899f0577f
5
5
  SHA512:
6
- metadata.gz: f60f531f4683da41ba01d19e67ded642ffb4cf8e828044287354d11362bb31a07cdb6ff4affd75483ebc384d11140b4c9014173af24116b95c4efaaba93173c5
7
- data.tar.gz: 0d87b9e35efedb3f34dd250edfbdf12bae629d1c92562d9a0652aff2c6dd741a67515119ec08ac79855a7626f5f65766c5a59c13a2399cdeef126b3a58a42717
6
+ metadata.gz: 395ba486f899a3bae89797950575c5fd814c23c15ebe7764f4dafb8bfaf487c47893b4a97fcfcfb5870ea1eb7d5b8f472b23752831d7392826940e9f5f4f8c07
7
+ data.tar.gz: 0ffaf1e80285e5a95ebeac8a90d5b700a0ce147d80f2d524e08d66aa7699726f37b82e580c8e1622598c2e53ff252580cde5f9fba2f4e942e0af9f626b6e6ed1
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module Project
5
+ module Bin
6
+ class Generators
7
+ class Routes
8
+ class InertiaPath
9
+ attr_reader :view_path, :component_name, :display_path, :layout_import_path
10
+
11
+ def initialize(branch_name, name)
12
+ @view_path = build_view_path(branch_name, name)
13
+ @component_name = build_component_name(name)
14
+ @display_path = build_display_path(branch_name, name)
15
+ @layout_import_path = build_layout_import_path
16
+ end
17
+
18
+ private
19
+
20
+ def build_layout_import_path
21
+ prefix = "../" * (view_path.count("/") + 1)
22
+ "#{prefix}components/Layout"
23
+ end
24
+
25
+ def build_view_path(branch_name, name)
26
+ segments = branch_name.split("/")
27
+ segments << (name || DEFAULT_VIEW_NAME)
28
+ (segments[0..-2] + [segments.last.capitalize]).join("/")
29
+ end
30
+
31
+ def build_component_name(name)
32
+ (name || DEFAULT_VIEW_NAME).capitalize
33
+ end
34
+
35
+ def build_display_path(branch_name, name)
36
+ segments = branch_name.split("/")
37
+ segments << (name || DEFAULT_VIEW_NAME)
38
+ segments.join("/")
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module Project
5
+ module Bin
6
+ class Generators
7
+ class Routes
8
+ class InertiaViewBuilder
9
+ def initialize(path)
10
+ @path = path
11
+ end
12
+
13
+ def component_content
14
+ <<~JSX
15
+ import React from 'react'
16
+ import { Link } from '@inertiajs/react'
17
+ import Layout from '#{@path.layout_import_path}'
18
+
19
+ const #{@path.component_name} = props => {
20
+ return (<Layout>
21
+ <div>
22
+ #{@path.display_path}
23
+ </div>
24
+ </Layout>)
25
+ }
26
+
27
+ export default #{@path.component_name}
28
+ JSX
29
+ end
30
+
31
+ def test_content
32
+ <<~JS
33
+ import React from 'react'
34
+ import { render, screen } from '@testing-library/react'
35
+ import #{@path.component_name} from './#{@path.component_name}'
36
+
37
+ describe('#{@path.component_name}', () => {
38
+ it('renders', () => {
39
+ render(<#{@path.component_name} />)
40
+
41
+ expect(
42
+ screen.getByText('#{@path.display_path}')
43
+ ).toBeInTheDocument()
44
+ })
45
+ })
46
+ JS
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module Project
5
+ module Bin
6
+ class Generators
7
+ class Routes
8
+ module Namespace
9
+ module_function
10
+
11
+ def for_path(sub_path)
12
+ sub_path.include?("/") ? ":\"#{sub_path}\"" : ":#{sub_path}"
13
+ end
14
+
15
+ def hash_branches_line(sub_path)
16
+ "r.hash_branches(#{for_path(sub_path)})"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module Project
5
+ module Bin
6
+ class Generators
7
+ class Routes
8
+ class NestedBranchFileBuilder
9
+ def initialize(project_name:, prefix:)
10
+ @project_name = project_name
11
+ @prefix = prefix
12
+ end
13
+
14
+ def content
15
+ <<~RUBY
16
+ class #{@project_name}
17
+ #{header} # #{@prefix} branch
18
+ r.hash_branches(#{Namespace.for_path(@prefix)}) # #{@prefix}/ +1 routes
19
+ end
20
+ end
21
+ RUBY
22
+ end
23
+
24
+ private
25
+
26
+ def header
27
+ return "hash_branch \"#{@prefix}\" do |r|" unless @prefix.include?("/")
28
+
29
+ parts = @prefix.split("/")
30
+ parent_path = parts[0..-2].join("/")
31
+ "hash_branch #{Namespace.for_path(parent_path)}, \"#{parts.last}\" do |r|"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module Project
5
+ module Bin
6
+ class Generators
7
+ class Routes
8
+ class Route
9
+ VALID_METHODS = %w[get post delete put patch].freeze
10
+
11
+ attr_reader :method, :name
12
+
13
+ def self.parse(route_str)
14
+ method, name = route_str.split(":", 2)
15
+
16
+ if name && VALID_METHODS.include?(method)
17
+ new(method, name)
18
+ elsif name.nil? && VALID_METHODS.include?(route_str)
19
+ new(route_str, nil)
20
+ else
21
+ new("get", route_str)
22
+ end
23
+ end
24
+
25
+ def initialize(method, name)
26
+ @method = method
27
+ @name = name
28
+ end
29
+
30
+ def get?
31
+ method == "get"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module Project
5
+ module Bin
6
+ class Generators
7
+ class Routes
8
+ class RouteSpecBuilder
9
+ def initialize(branch_name:, routes:)
10
+ @branch_name = branch_name
11
+ @routes = routes
12
+ end
13
+
14
+ def content
15
+ [
16
+ "require_relative \"#{relative_spec_helper_path}\"",
17
+ "",
18
+ "describe \"Routes for #{@branch_name}\" do",
19
+ test_definitions,
20
+ "end",
21
+ ""
22
+ ].join("\n")
23
+ end
24
+
25
+ private
26
+
27
+ def test_definitions
28
+ @routes.map { |route| build_test_definition(route) }.join("\n")
29
+ end
30
+
31
+ def build_test_definition(route)
32
+ test_name = route.name ? " /#{@branch_name}/#{route.name}" : " /#{@branch_name}"
33
+ test_path = route.name ? " \"/#{@branch_name}/#{route.name}\"" : " \"/#{@branch_name}\""
34
+
35
+ " it \"responds to #{route.method.upcase}#{test_name}\" do\n" \
36
+ " #{route.method}#{test_path}\n" \
37
+ " expect(last_response.status).to eq(200)\n" \
38
+ " end"
39
+ end
40
+
41
+ def relative_spec_helper_path
42
+ prefix = "../" * (2 + @branch_name.count("/"))
43
+ "#{prefix}spec_helper"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module Project
5
+ module Bin
6
+ class Generators
7
+ class Routes
8
+ class RoutesFileBuilder
9
+ def initialize(project_name:, branch_name:, routes:, view_mode:)
10
+ @project_name = project_name
11
+ @branch_name = branch_name
12
+ @routes = routes
13
+ @view_mode = view_mode
14
+ end
15
+
16
+ def content
17
+ <<~RUBY
18
+ class #{@project_name}
19
+ #{branch_header}
20
+ #{route_definitions}
21
+ end
22
+ end
23
+ RUBY
24
+ end
25
+
26
+ private
27
+
28
+ def route_definitions
29
+ @routes.map { |route| build_route_definition(route) }.join("\n\n")
30
+ end
31
+
32
+ def build_route_definition(route)
33
+ view_line = build_view_line(route)
34
+ route_block(route, view_line)
35
+ end
36
+
37
+ def build_view_line(route)
38
+ return "" unless route.get? && @view_mode != :none
39
+
40
+ if @view_mode == :inertia
41
+ "\n inertia('#{InertiaPath.new(@branch_name, route.name).view_path}')"
42
+ else
43
+ "\n view('#{route.name || DEFAULT_VIEW_NAME}')"
44
+ end
45
+ end
46
+
47
+ def route_block(route, view_line)
48
+ route_name = route.name ? " \"#{route.name}\"" : ""
49
+ " r.#{route.method}#{route_name} do#{view_line}\n end"
50
+ end
51
+
52
+ def branch_header
53
+ return "hash_branch \"#{@branch_name}\" do |r|" unless @branch_name.include?("/")
54
+
55
+ parts = @branch_name.split("/")
56
+ sub_path = parts[0..-2].join("/")
57
+ "hash_branch #{Namespace.for_path(sub_path)}, \"#{parts.last}\" do |r|"
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -1,11 +1,11 @@
1
- # rubocop:disable Layout/HeredocIndentation
1
+ # frozen_string_literal: true
2
+
2
3
  class Roda
3
4
  module Project
4
5
  module Bin
5
6
  class Generators < ::Thor
6
7
  class Routes < Roda::Project::Bin::Generator
7
8
  DEFAULT_VIEW_NAME = "index"
8
- VALID_METHODS = %w[get post delete put patch].freeze
9
9
 
10
10
  def call
11
11
  if branch_name.nil? || branch_name.empty? || routes_list.nil? || routes_list.empty?
@@ -18,249 +18,112 @@ class Roda
18
18
  generate_nested_branch_files
19
19
  generate_views
20
20
  generate_tests
21
+ generate_inertia_view_tests
21
22
  end
22
23
 
23
24
  private
24
25
 
25
26
  def generate_routes
26
27
  filename = File.join(ensure_and_get_path("app/routes", branch_name), "#{branch_name}.rb")
27
- route_definitions = routes_list.map { |route_str| build_route_definition(route_str) }.join("\n\n")
28
-
29
- write_file(filename, routes_file_content(route_definitions))
30
- end
31
-
32
- def generate_views
33
- return unless must_generate_views?
34
-
35
- @context.inertia? ? generate_inertia_views : generate_erb_views
36
- end
37
-
38
- def generate_erb_views
39
- branch_views_dir = File.join("app/views", branch_name)
40
- FileUtils.mkdir_p(branch_views_dir)
41
- routes_list.each do |route_str|
42
- method, name = parse_route_string(route_str)
43
- next unless method == "get"
44
-
45
- name = DEFAULT_VIEW_NAME if name.nil?
46
- write_file(File.join(branch_views_dir, "#{name}.erb"), "")
47
- end
48
- end
49
-
50
- def generate_inertia_views
51
- views_dir = "app/assets/js/inertia/views"
52
- FileUtils.mkdir_p(views_dir)
53
- routes_list.each do |route_str|
54
- method, name = parse_route_string(route_str)
55
- next unless method == "get"
56
-
57
- path = InertiaPath.new(branch_name, name)
58
- view_filename = File.join(views_dir, "#{path.view_path}.jsx")
59
- FileUtils.mkdir_p(File.dirname(view_filename))
60
- write_file(view_filename, inertia_component_content(path))
61
- end
62
- end
63
-
64
- def generate_tests
65
- test_filename = File.join(ensure_and_get_path("spec/app/routes", branch_name), "#{branch_name}_spec.rb")
66
- nesting_level = branch_name.count("/")
67
- relative_spec_helper_path = "../" * (2 + nesting_level) + "spec_helper"
68
-
69
- test_route_definitions = routes_list.map { |route_str| build_test_definition(route_str) }.join("\n")
70
-
71
- test_content = <<~RUBY
72
- require_relative "#{relative_spec_helper_path}"
73
-
74
- describe "Routes for #{branch_name}" do
75
- #{test_route_definitions}
76
- end
77
- RUBY
78
- write_file(test_filename, test_content)
28
+ write_file(filename, routes_file_content)
79
29
  end
80
30
 
81
31
  def generate_nested_branch_files
82
32
  return unless branch_name.include?("/")
83
33
 
84
- parts = branch_name.split("/")
85
- parts[0..-2].each_with_index do |_segment, index|
86
- prefix = parts[0..index].join("/")
87
- filename = File.join("app/routes", "#{prefix}.rb")
88
-
89
- if File.exist?(filename)
90
- ensure_hash_branches_line(filename, namespace_for(prefix))
91
- next
92
- end
93
-
94
- dir = File.dirname(filename)
95
- FileUtils.mkdir_p(dir) unless File.directory?(dir)
96
- write_file(filename, nested_branch_file_content(prefix, index))
97
- end
98
- end
99
-
100
- def build_route_definition(route_str)
101
- method, name = parse_route_string(route_str)
102
- view_line = build_view_line(method, name)
103
- route_block(method, name, view_line)
34
+ branch_prefixes.each { |prefix| generate_nested_branch_file(prefix) }
104
35
  end
105
36
 
106
- def build_view_line(method, name)
107
- return "" unless method == "get" && must_generate_views?
37
+ def generate_nested_branch_file(prefix)
38
+ filename = File.join("app/routes", "#{prefix}.rb")
108
39
 
109
- if @context.inertia?
110
- "\n inertia('#{InertiaPath.new(branch_name, name).view_path}')"
40
+ if File.exist?(filename)
41
+ ensure_hash_branches_line(filename, prefix)
111
42
  else
112
- "\n view('#{name || DEFAULT_VIEW_NAME}')"
43
+ FileUtils.mkdir_p(File.dirname(filename))
44
+ write_file(filename, NestedBranchFileBuilder.new(project_name:, prefix:).content)
113
45
  end
114
46
  end
115
47
 
116
- def route_block(method, name, view_line)
117
- route_name = name ? " \"#{name}\"" : ""
118
- " r.#{method}#{route_name} do#{view_line}\n end"
48
+ def generate_views
49
+ generate_inertia_views if view_mode == :inertia
50
+ generate_erb_views if view_mode == :erb
119
51
  end
120
52
 
121
- def build_test_definition(route_str)
122
- method, name = parse_route_string(route_str)
123
- test_name = name ? " /#{branch_name}/#{name}" : " /#{branch_name}"
124
- test_path = name ? " \"/#{branch_name}/#{name}\"" : " \"/#{branch_name}\""
125
-
126
- " it \"responds to #{method.upcase}#{test_name}\" do\n" \
127
- " #{method}#{test_path}\n" \
128
- " expect(last_response.status).to eq(200)\n" \
129
- " end"
53
+ def generate_erb_views
54
+ dir = File.join("app/views", branch_name)
55
+ FileUtils.mkdir_p(dir)
56
+ view_routes.each { |route| write_file(File.join(dir, "#{route.name || DEFAULT_VIEW_NAME}.erb"), "") }
130
57
  end
131
58
 
132
- def routes_file_content(route_definitions)
133
- <<~RUBY
134
- class #{@context.const_project_name}
135
- #{build_hash_branch_header}
136
- #{route_definitions}
137
- end
138
- end
139
- RUBY
59
+ def generate_inertia_views
60
+ write_inertia_files(".jsx", &:component_content)
140
61
  end
141
62
 
142
- def build_hash_branch_header
143
- return "hash_branch \"#{branch_name}\" do |r|" unless branch_name.include?("/")
63
+ def generate_tests
64
+ test_filename = File.join(ensure_and_get_path("spec/app/routes", branch_name), "#{branch_name}_spec.rb")
65
+ write_file(test_filename, RouteSpecBuilder.new(branch_name:, routes:).content)
66
+ end
144
67
 
145
- parts = branch_name.split("/")
146
- sub_path = parts[0..-2].join("/")
147
- branch_segment = parts.last
148
- "hash_branch #{namespace_for(sub_path)}, \"#{branch_segment}\" do |r|"
68
+ def generate_inertia_view_tests
69
+ write_inertia_files(".test.js", &:test_content) if view_mode == :inertia
149
70
  end
150
71
 
151
- def nested_branch_file_content(prefix, index)
152
- parts = prefix.split("/")
153
- segment = parts.last
154
- header = if index.zero?
155
- "hash_branch \"#{segment}\" do |r|"
156
- else
157
- parent_path = parts[0..-2].join("/")
158
- "hash_branch #{namespace_for(parent_path)}, \"#{segment}\" do |r|"
72
+ def write_inertia_files(extension)
73
+ views_dir = "app/assets/js/inertia/views"
74
+ FileUtils.mkdir_p(views_dir)
75
+ view_routes.each do |route|
76
+ path = InertiaPath.new(branch_name, route.name)
77
+ filename = File.join(views_dir, "#{path.view_path}#{extension}")
78
+ FileUtils.mkdir_p(File.dirname(filename))
79
+ write_file(filename, yield(InertiaViewBuilder.new(path)))
159
80
  end
81
+ end
160
82
 
161
- <<~RUBY
162
- class #{@context.const_project_name}
163
- #{header} # #{prefix} branch
164
- r.hash_branches(#{namespace_for(prefix)}) # #{prefix}/ +1 routes
165
- end
166
- end
167
- RUBY
83
+ def routes_file_content
84
+ RoutesFileBuilder.new(project_name:, branch_name:, routes:, view_mode:).content
168
85
  end
169
86
 
170
- def ensure_hash_branches_line(filename, namespace)
171
- expected_line = "r.hash_branches(#{namespace})"
87
+ def ensure_hash_branches_line(filename, prefix)
88
+ expected_line = Namespace.hash_branches_line(prefix)
172
89
  return if File.read(filename).include?(expected_line)
173
90
 
174
91
  puts " warning: #{filename} already exists — please add:"
175
92
  puts " #{expected_line}"
176
93
  end
177
94
 
178
- def namespace_for(sub_path)
179
- sub_path.include?("/") ? ":\"#{sub_path}\"" : ":#{sub_path}"
95
+ def branch_prefixes
96
+ parts = branch_name.split("/")
97
+ (0...parts.length - 1).map { |index| parts[0..index].join("/") }
180
98
  end
181
99
 
182
- def must_generate_views?
183
- return false unless @options[:views]
100
+ def view_mode = @view_mode ||= compute_view_mode
184
101
 
185
- if @context.inertia?
186
- Dir.exist?("app/assets/js/inertia/views")
187
- else
188
- Dir.exist?("app/views")
189
- end
190
- end
102
+ def compute_view_mode
103
+ return :none unless @options[:views]
191
104
 
192
- def inertia_component_content(path)
193
- <<~JSX
194
- import React from 'react'
195
- import { Link } from '@inertiajs/react'
196
- import Layout from '../components/Layout'
105
+ views_exist = @context.inertia? ? Dir.exist?("app/assets/js/inertia/views") : Dir.exist?("app/views")
106
+ return :none unless views_exist
197
107
 
198
- const #{path.component_name} = props => {
199
- return (<Layout>
200
- <div>
201
- #{path.display_path}
202
- </div>
203
- </Layout>)
204
- }
205
-
206
- export default #{path.component_name}
207
- JSX
108
+ @context.inertia? ? :inertia : :erb
208
109
  end
209
110
 
210
- def parse_route_string(route_str)
211
- method, name = route_str.split(":", 2)
212
- if name && VALID_METHODS.include?(method)
213
- [method, name]
214
- elsif name.nil? && VALID_METHODS.include?(route_str)
215
- [route_str, nil]
216
- else
217
- ["get", route_str]
218
- end
219
- end
111
+ def routes = @routes ||= routes_list.map { |route_str| Route.parse(route_str) }
112
+
113
+ def view_routes = @view_routes ||= routes.select(&:get?)
114
+
115
+ def project_name = @context.const_project_name
220
116
 
221
117
  def write_file(filename, content)
222
118
  File.write(filename, content)
223
119
  action_success_message(filename)
224
120
  end
225
121
 
226
- def routes_list
227
- @routes_list ||= @args[1..]
228
- end
122
+ def routes_list = @routes_list ||= @args[1..]
229
123
 
230
- def branch_name
231
- @branch_name ||= @args[0]
232
- end
233
-
234
- class InertiaPath
235
- attr_reader :view_path, :component_name, :display_path
236
-
237
- def initialize(branch_name, name)
238
- @view_path = build_view_path(branch_name, name)
239
- @component_name = build_component_name(name)
240
- @display_path = build_display_path(branch_name, name)
241
- end
242
-
243
- private
244
-
245
- def build_view_path(branch_name, name)
246
- segments = branch_name.split("/")
247
- segments << (name || "index")
248
- (segments[0..-2] + [segments.last.capitalize]).join("/")
249
- end
250
-
251
- def build_component_name(name)
252
- (name || "index").capitalize
253
- end
254
-
255
- def build_display_path(branch_name, name)
256
- segments = branch_name.split("/")
257
- segments << (name || "index")
258
- segments.join("/")
259
- end
260
- end
124
+ def branch_name = @branch_name ||= @args[0]
261
125
  end
262
126
  end
263
127
  end
264
128
  end
265
129
  end
266
- # rubocop:enable Layout/HeredocIndentation
@@ -50,7 +50,7 @@ class Roda
50
50
 
51
51
  retry_on_error { @context.base = read_line("(#{fullstack_id}) Fullstack (#{api_id}) API (#{minimal_id}) Minimal › ", fullstack_id).to_i }
52
52
  if @context.fullstack?
53
- retry_on_error { @context.frontend = read_line("(#{erb_id}) only ERB (#{inertia_id}) Inertia.js › ", erb_id).to_i }
53
+ retry_on_error { @context.frontend = read_line("(#{erb_id}) Only ERB (#{inertia_id}) Inertia.js › ", erb_id).to_i }
54
54
  end
55
55
 
56
56
  retry_on_error { @context.tests = read_line("(#{rspec_id}) RSpec (#{minitest_id}) Minitest › ", rspec_id).to_i }
@@ -111,6 +111,12 @@ class Roda
111
111
  erb_cp_dir(from_template, "app/assets")
112
112
  erb_cp_file(from_template, "esbuild.js")
113
113
  erb_cp_file(from_template, "package.json")
114
+ if @context.inertia?
115
+ erb_cp_file(from_template, "babel.config.js")
116
+ erb_cp_file(from_template, "jest.config.js")
117
+ erb_cp_file(from_template, "jest.setup.js")
118
+ erb_cp_file(from_template, "jest.inertia-mock.js")
119
+ end
114
120
  cp_dir(from_template, "app/config/locales")
115
121
  action_success_message("app/config/locales")
116
122
  cp_dir(from_template, "app/views")
@@ -7,9 +7,9 @@ class Roda
7
7
  module_function
8
8
 
9
9
  def camelize(str)
10
- return str if /[A-Z]/.match?(str)
10
+ return str if /\A[A-Z][a-zA-Z0-9]*\z/.match?(str)
11
11
 
12
- str.split("_").map(&:capitalize).join
12
+ underscore(str).split("_").reject(&:empty?).map(&:capitalize).join
13
13
  end
14
14
 
15
15
  def underscore(str)
@@ -5,7 +5,7 @@ class Roda
5
5
  include Helpers::Ids
6
6
 
7
7
  class InvalidValue < Roda::Project::Error; end
8
- VALID_PROJECT_NAME_REGEX = /^[a-zA-Z][a-zA-Z0-9_]*$/
8
+ VALID_PROJECT_NAME_REGEX = /\A[a-zA-Z][a-zA-Z0-9_]*\z/
9
9
 
10
10
  attr_reader(
11
11
  :tests,
@@ -95,11 +95,12 @@ class Roda
95
95
  # rubocop:enable Lint/InterpolationCheck
96
96
 
97
97
  def project_name=(val)
98
- if !(val =~ VALID_PROJECT_NAME_REGEX)
98
+ unless VALID_PROJECT_NAME_REGEX.match?(val.to_s)
99
99
  raise InvalidValue, "Project name must start with a letter and contains only letters, numbers and _"
100
100
  end
101
101
 
102
- @project_name = val
102
+ @project_name = Helpers::Inflections.underscore(val.to_s).squeeze("_").sub(/_+\z/, "")
103
+ @const_project_name = nil
103
104
  end
104
105
 
105
106
  def rspec?
@@ -172,7 +173,7 @@ class Roda
172
173
  end
173
174
 
174
175
  def const_project_name
175
- @const_project_name ||= project_name.split("_").map(&:capitalize).join
176
+ @const_project_name ||= Helpers::Inflections.camelize(project_name)
176
177
  end
177
178
  end
178
179
  end
@@ -9,7 +9,7 @@
9
9
  - **Frontend**: esbuild + ERB (Fullstack mode).<% else %>
10
10
  - **Frontend**: esbuild + Inertia.js + React.js (Fullstack mode).<% end %>
11
11
 
12
- ## Code Style & Conventions<%= i = 0 %>
12
+ ## Code Style & Conventions<% i = 0 %>
13
13
 
14
14
  ### <%= i=i+1 %>. Types
15
15
 
@@ -26,6 +26,7 @@
26
26
  - Calling `inertia('Something')` will search an inertia view file in "app/assets/js/inertia/views/Something.jsx".
27
27
  - Calling `inertia('Zoo', { name: "foo", age: 3 })` will search an inertia view file in "app/assets/js/inertia/views/Zoo.jsx" and set the props data in the React function component.
28
28
  - Calling `inertia('Foo/Bar')` will search an inertia view file in "app/assets/js/inertia/views/Foo/Bar.jsx".<% end %>
29
+ - **STRICT:** When creating routes with `bin/roda g routes` always use the generated views files (when necessary) instead of creating new ones
29
30
  <% end %>
30
31
  ### <%= i=i+1 %>. Routing
31
32
 
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ presets: [
3
+ ['@babel/preset-env', { targets: { node: 'current' } }],
4
+ ['@babel/preset-react', { runtime: 'automatic' }],
5
+ ],
6
+ }
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ testEnvironment: 'jsdom',
3
+ roots: ['<rootDir>/app/assets/js/inertia'],
4
+ testMatch: ['**/*.test.js'],
5
+ moduleFileExtensions: ['js', 'jsx', 'json'],
6
+ setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
7
+ moduleNameMapper: {
8
+ '^@inertiajs/react$': '<rootDir>/jest.inertia-mock.js',
9
+ },
10
+ }
@@ -0,0 +1,11 @@
1
+ const React = require('react')
2
+
3
+ const Link = ({ children, href, ...props }) => React.createElement('a', { href, ...props }, children)
4
+
5
+ module.exports = {
6
+ Link,
7
+ Head: ({ children }) => React.createElement(React.Fragment, null, children),
8
+ router: { visit: () => {}, get: () => {}, post: () => {}, reload: () => {} },
9
+ usePage: () => ({ props: {}, url: '/', component: '' }),
10
+ useForm: () => ({ data: {}, setData: () => {}, submit: () => {}, processing: false, errors: {} }),
11
+ }
@@ -0,0 +1 @@
1
+ require('@testing-library/jest-dom')
@@ -4,7 +4,7 @@
4
4
  "description": "",
5
5
  "main": "esbuild.config.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
7
+ "test": "jest",
8
8
  "build": "node esbuild.js",
9
9
  "build:watch": "node esbuild.js --watch"
10
10
  },
@@ -16,8 +16,17 @@
16
16
  "react-dom": "^19.2.7"
17
17
  },
18
18
  "devDependencies": {
19
+ "@babel/core": "^7.29.7",
20
+ "@babel/preset-env": "^7.29.7",
21
+ "@babel/preset-react": "^7.29.7",
22
+ "@testing-library/dom": "^10.4.2",
23
+ "@testing-library/jest-dom": "^6.10.0",
24
+ "@testing-library/react": "^16.3.3",
25
+ "babel-jest": "^30.5.2",
19
26
  "esbuild": "^0.19.0",
20
27
  "esbuild-plugin-manifest": "^1.0.5",
28
+ "jest": "^30.5.2",
29
+ "jest-environment-jsdom": "^30.5.2",
21
30
  "path": "^0.12.7"
22
31
  }
23
32
  }
@@ -4,7 +4,7 @@ describe "hello" do
4
4
  it "test requests" do
5
5
  get "/"
6
6
  expect(last_response.status).to eq(200)<% if context.fullstack? %>
7
- expect(last_response.body).to include("Hello world")<% end %>
7
+ expect(last_response.body).to include(<%= context.inertia? ? "\"public/assets/app.js\"" : "\"Hello world\"" %>)<% end %>
8
8
  end
9
9
  <% if context.rodauth? %>
10
10
  # run "RACK_ENV=test bin/roda db migrate" to make this spec pass
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Roda
4
4
  module Project
5
- VERSION = "0.1.18"
5
+ VERSION = "0.1.19"
6
6
  end
7
7
  end
data/lib/roda/project.rb CHANGED
@@ -74,6 +74,13 @@ require_relative "project/bin/generators/migration/code_builder"
74
74
  require_relative "project/bin/generators/model"
75
75
  # Generators/routes
76
76
  require_relative "project/bin/generators/routes"
77
+ require_relative "project/bin/generators/routes/route"
78
+ require_relative "project/bin/generators/routes/inertia_path"
79
+ require_relative "project/bin/generators/routes/namespace"
80
+ require_relative "project/bin/generators/routes/routes_file_builder"
81
+ require_relative "project/bin/generators/routes/nested_branch_file_builder"
82
+ require_relative "project/bin/generators/routes/route_spec_builder"
83
+ require_relative "project/bin/generators/routes/inertia_view_builder"
77
84
  # Generators/service
78
85
  require_relative "project/bin/generators/service"
79
86
  # Generators/base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-project
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique F. Teixeira
@@ -85,6 +85,13 @@ files:
85
85
  - lib/roda/project/bin/generators/migration/field_parser.rb
86
86
  - lib/roda/project/bin/generators/model.rb
87
87
  - lib/roda/project/bin/generators/routes.rb
88
+ - lib/roda/project/bin/generators/routes/inertia_path.rb
89
+ - lib/roda/project/bin/generators/routes/inertia_view_builder.rb
90
+ - lib/roda/project/bin/generators/routes/namespace.rb
91
+ - lib/roda/project/bin/generators/routes/nested_branch_file_builder.rb
92
+ - lib/roda/project/bin/generators/routes/route.rb
93
+ - lib/roda/project/bin/generators/routes/route_spec_builder.rb
94
+ - lib/roda/project/bin/generators/routes/routes_file_builder.rb
88
95
  - lib/roda/project/bin/generators/service.rb
89
96
  - lib/roda/project/cli.rb
90
97
  - lib/roda/project/generator.rb
@@ -130,7 +137,11 @@ files:
130
137
  - lib/roda/project/templates/front-end-inertia/app/views/foo/bar.erb
131
138
  - lib/roda/project/templates/front-end-inertia/app/views/index.erb
132
139
  - lib/roda/project/templates/front-end-inertia/app/views/layout.erb
140
+ - lib/roda/project/templates/front-end-inertia/babel.config.js
133
141
  - lib/roda/project/templates/front-end-inertia/esbuild.js
142
+ - lib/roda/project/templates/front-end-inertia/jest.config.js
143
+ - lib/roda/project/templates/front-end-inertia/jest.inertia-mock.js
144
+ - lib/roda/project/templates/front-end-inertia/jest.setup.js
134
145
  - lib/roda/project/templates/front-end-inertia/package.json
135
146
  - lib/roda/project/templates/front-end-inertia/public/assets/app.css
136
147
  - lib/roda/project/templates/front-end-inertia/public/assets/app.js