Skip to content

Commit ced699e

Browse files
authored
Merge pull request #1958 from jekuta/fix-1944
Respect long namespaces for rails g scaffold
2 parents 0127716 + 4ae2293 commit ced699e

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/generators/rspec/scaffold/scaffold_generator.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,31 @@ def copy_view(view)
7373

7474
# support for namespaced-resources
7575
def ns_file_name
76-
ns_parts.empty? ? file_name : "#{ns_parts[0].underscore}_#{ns_parts[1].singularize.underscore}"
76+
return file_name if ns_parts.empty?
77+
"#{ns_prefix.map(&:underscore).join('/')}_#{ns_suffix.singularize.underscore}"
7778
end
7879

7980
# support for namespaced-resources
8081
def ns_table_name
81-
ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}"
82+
return table_name if ns_parts.empty?
83+
"#{ns_prefix.map(&:underscore).join('/')}/#{ns_suffix.tableize}"
8284
end
8385

8486
def ns_parts
8587
@ns_parts ||= begin
86-
matches = generator_args[0].to_s.match(/\A(\w+)(?:\/|::)(\w+)/)
87-
matches ? [matches[1], matches[2]] : []
88+
parts = generator_args[0].split(/\/|::/)
89+
parts.size > 1 ? parts : []
8890
end
8991
end
9092

93+
def ns_prefix
94+
@ns_prefix ||= ns_parts[0..-2]
95+
end
96+
97+
def ns_suffix
98+
@ns_suffix ||= ns_parts[-1]
99+
end
100+
91101
def value_for(attribute)
92102
raw_value_for(attribute).inspect
93103
end

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
it { is_expected.to contain(/"renders a JSON response with errors for the \w+"/) }
4949

5050
it { is_expected.not_to contain(/"redirects to the \w+ list"/) }
51-
5251
end
5352
end
5453

@@ -226,6 +225,7 @@
226225
it { is_expected.to contain(/describe "routing"/) }
227226
it { is_expected.to contain(/routes to #new/) }
228227
it { is_expected.to contain(/routes to #edit/) }
228+
it { is_expected.to contain('route_to("posts#new")') }
229229
end
230230

231231
describe 'with --no-routing-specs' do
@@ -238,5 +238,15 @@
238238
it { is_expected.not_to contain(/routes to #new/) }
239239
it { is_expected.not_to contain(/routes to #edit/) }
240240
end
241+
242+
context 'with a namespaced name' do
243+
subject { file('spec/routing/api/v1/posts_routing_spec.rb') }
244+
245+
describe 'with default options' do
246+
before { run_generator %w(api/v1/posts) }
247+
it { is_expected.to contain(/^RSpec.describe Api::V1::PostsController, #{type_metatag(:routing)}/) }
248+
it { is_expected.to contain('route_to("api/v1/posts#new")') }
249+
end
250+
end
241251
end
242252
end

0 commit comments

Comments
 (0)