Skip to content

Commit cab19b2

Browse files
Merge pull request #22 from SwitchDreams/bug/fixes_minor_bugs
Bug/fixes minor bugs
2 parents 2e045f2 + cef752c commit cab19b2

File tree

5 files changed

+70
-11
lines changed

5 files changed

+70
-11
lines changed

lib/generators/rest_api_generator/helpers.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,24 @@ def scope_route_path
7171
def nested_routes
7272
return "" if options["father"].blank?
7373

74-
"#{options["father"].downcase.pluralize}/\#{#{options["father"].singularize.downcase}.id}/#{plural_name}"
74+
"#{options["father"].downcase.pluralize}/\#{#{options["father"].singularize.downcase}.id}"
7575
end
7676

7777
def initial_route
78-
return "/#{plural_name}" if options["father"].blank? && options["scope"].blank?
79-
80-
scope_route_path + "/" + nested_routes
78+
route = ""
79+
route += scope_route_path if options["scope"].present?
80+
route += options["father"].present? && route.present? ? "/#{nested_routes}" : nested_routes
81+
route += "/#{plural_name}"
82+
route[0] == "/" ? route : "/#{route}"
8183
end
8284

8385
def spec_routes
8486
{
8587
index: initial_route,
86-
show: initial_route + "\#{#{singular_name}.id}",
88+
show: initial_route + "/\#{#{singular_name}.id}",
8789
create: initial_route,
88-
update: initial_route + "\#{#{singular_name}.id}",
89-
delete: initial_route + "\#{#{singular_name}.id}",
90+
update: initial_route + "/\#{#{singular_name}.id}",
91+
delete: initial_route + "/\#{#{singular_name}.id}",
9092
}
9193
end
9294
end

lib/generators/rest_api_generator/spec/rspec_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def controller_test_path
2626

2727
def spec_controller_template
2828
if options["father"].present?
29-
"rspec/resource_controller_spec.rb"
30-
else
3129
"rspec/nested_resource_controller_spec.rb"
30+
else
31+
"rspec/resource_controller_spec.rb"
3232
end
3333
end
3434
end

lib/generators/rest_api_generator/spec/rswag_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create_service_file
2525
def nested_routes
2626
return "" if options["father"].blank?
2727

28-
"#{options["father"].downcase.pluralize}/{#{options["father"].singularize.downcase}_id}/#{plural_name}"
28+
"#{options["father"].downcase.pluralize}/{#{options["father"].singularize.downcase}_id}"
2929
end
3030

3131
def spec_routes

lib/generators/rest_api_generator/spec/templates/rspec/resource_controller_spec.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RSpec.describe "<%= class_name %>", type: :request do
3838

3939
describe "DELETE /<%= plural_name %>/:id" do
4040
it "deletes an <%= plural_name %>" do
41-
item = create(:<%= singular_name %>)
41+
<%= singular_name %> = create(:<%= singular_name %>)
4242
expect do
4343
delete "<%= spec_routes[:delete] %>"
4444
end.to change(<%= class_name %>, :count).by(-1)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
require "generators/rest_api_generator/spec/rspec_generator"
5+
require "support/generators"
6+
7+
RSpec.describe RestApiGenerator::Spec::RspecGenerator, type: :generator do
8+
setup_default_destination
9+
10+
context "when is nested resource" do
11+
before do
12+
run_generator ["Driver", "car:references", "name:string", "--father", "Cars"]
13+
end
14+
15+
describe "spec file" do
16+
subject(:spec_file) { file("spec/requests/cars/drivers_controller_spec.rb") }
17+
18+
it { is_expected.to exist }
19+
20+
it "describe right routes for show" do
21+
expect(spec_file).to contain('/cars/#{car.id}/drivers/#{driver.id}')
22+
end
23+
end
24+
end
25+
26+
context "when is scoped resource" do
27+
before do
28+
run_generator ["Driver", "car:references", "name:string", "--scope", "Cars"]
29+
end
30+
31+
describe "spec file" do
32+
subject(:spec_file) { file("spec/requests/cars/drivers_controller_spec.rb") }
33+
34+
it { is_expected.to exist }
35+
36+
it "describe right routes for show" do
37+
expect(spec_file).to contain('/cars/drivers/#{driver.id}')
38+
end
39+
end
40+
end
41+
42+
context "when is scoped and nested resource" do
43+
before do
44+
run_generator ["Driver", "car:references", "name:string", "--scope", "api", "--father", "Cars"]
45+
end
46+
47+
describe "spec file" do
48+
subject(:spec_file) { file("spec/requests/api/cars/drivers_controller_spec.rb") }
49+
50+
it { is_expected.to exist }
51+
52+
it "describe right routes for show" do
53+
expect(spec_file).to contain('/api/cars/#{car.id}/drivers/#{driver.id}')
54+
end
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)