Skip to content

Commit bdb97ce

Browse files
committed
Extract method from scaffold request spec template to ScaffoldGenerator
1 parent f8aaf75 commit bdb97ce

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/generators/rspec/scaffold/scaffold_generator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ def ns_suffix
112112
@ns_suffix ||= ns_parts[-1]
113113
end
114114

115+
def singular_url_helper(record_name)
116+
# Similar to Rails::Generators::NamedBase.show_helper - but singular_route_name method is not available
117+
"#{singular_table_name}_url(#{record_name})"
118+
end
119+
115120
def value_for(attribute)
116121
raw_value_for(attribute).inspect
117122
end

lib/generators/rspec/scaffold/templates/request_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
describe "GET /show" do
4141
it "renders a successful response" do
4242
<%= file_name %> = <%= class_name %>.create! valid_attributes
43-
get <%= "#{singular_table_name}_url(#{file_name})" %>
43+
get <%= singular_url_helper(file_name) %>
4444
expect(response).to be_successful
4545
end
4646
end
@@ -55,7 +55,7 @@
5555
describe "GET /edit" do
5656
it "render a successful response" do
5757
<%= file_name %> = <%= class_name %>.create! valid_attributes
58-
get <%= "edit_#{singular_table_name}_url(#{file_name})" %>
58+
get <%= "edit_#{singular_url_helper(file_name)}" %>
5959
expect(response).to be_successful
6060
end
6161
end
@@ -70,7 +70,7 @@
7070
7171
it "redirects to the created <%= ns_file_name %>" do
7272
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes }
73-
expect(response).to redirect_to(<%= singular_table_name %>_url(<%= class_name %>.last))
73+
expect(response).to redirect_to(<%= singular_url_helper("#{class_name}.last") %>)
7474
end
7575
end
7676
@@ -103,7 +103,7 @@
103103
104104
it "redirects to the <%= ns_file_name %>" do
105105
<%= file_name %> = <%= class_name %>.create! valid_attributes
106-
patch <%= "#{singular_table_name}_url(#{file_name})" %>, params: { <%= singular_table_name %>: new_attributes }
106+
patch <%= singular_url_helper(file_name) %>, params: { <%= singular_table_name %>: new_attributes }
107107
<%= file_name %>.reload
108108
expect(response).to redirect_to(<%= singular_table_name %>_url(<%= file_name %>))
109109
end
@@ -112,7 +112,7 @@
112112
context "with invalid parameters" do
113113
it "renders a successful response (i.e. to display the 'edit' template)" do
114114
<%= file_name %> = <%= class_name %>.create! valid_attributes
115-
patch <%= "#{singular_table_name}_url(#{file_name})" %>, params: { <%= singular_table_name %>: invalid_attributes }
115+
patch <%= singular_url_helper(file_name) %>, params: { <%= singular_table_name %>: invalid_attributes }
116116
expect(response).to be_successful
117117
end
118118
end
@@ -122,13 +122,13 @@
122122
it "destroys the requested <%= ns_file_name %>" do
123123
<%= file_name %> = <%= class_name %>.create! valid_attributes
124124
expect {
125-
delete <%= "#{singular_table_name}_url(#{file_name})" %>
125+
delete <%= singular_url_helper(file_name) %>
126126
}.to change(<%= class_name %>, :count).by(-1)
127127
end
128128
129129
it "redirects to the <%= table_name %> list" do
130130
<%= file_name %> = <%= class_name %>.create! valid_attributes
131-
delete <%= "#{singular_table_name}_url(#{file_name})" %>
131+
delete <%= singular_url_helper(file_name) %>
132132
expect(response).to redirect_to(<%= "#{index_helper}_url" %>)
133133
end
134134
end

0 commit comments

Comments
 (0)