Skip to content

Commit 8eafbc1

Browse files
rubyszzak
andcommitted
make "g scaffold" with no field produce rubocop compliant code
When there are no fields: * Omit blank line in migration prior to "t.timestamps" * Omit leading and trailing spaced in empty hashes in create and update controller and api functional tests Co-authored-by: zzak <[email protected]>
1 parent 3accb8d commit 8eafbc1

File tree

6 files changed

+17
-10
lines changed

6 files changed

+17
-10
lines changed

activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Mi
1212
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
1313
<% end -%>
1414
<% end -%>
15-
<% if options[:timestamps] %>
15+
<% unless attributes.empty? -%>
16+
17+
<% end -%>
18+
<% if options[:timestamps] -%>
1619
t.timestamps
1720
<% end -%>
1821
end

railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ def fixture_name
3939

4040
private
4141
def attributes_string
42-
attributes_hash.map { |k, v| "#{k}: #{v}" }.join(", ")
42+
if attributes_hash.empty?
43+
"{}"
44+
else
45+
"{ #{attributes_hash.map { |k, v| "#{k}: #{v}" }.join(", ")} }"
46+
end
4347
end
4448

4549
def attributes_hash

railties/lib/rails/generators/test_unit/scaffold/templates/api_functional_test.rb.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe
1717

1818
test "should create <%= singular_table_name %>" do
1919
assert_difference("<%= class_name %>.count") do
20-
post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }, as: :json
20+
post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: #{attributes_string}" %> }, as: :json
2121
end
2222

2323
assert_response :created
@@ -29,7 +29,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe
2929
end
3030

3131
test "should update <%= singular_table_name %>" do
32-
patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }, as: :json
32+
patch <%= show_helper %>, params: { <%= "#{singular_table_name}: #{attributes_string}" %> }, as: :json
3333
assert_response :success
3434
end
3535

railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe
2222

2323
test "should create <%= singular_table_name %>" do
2424
assert_difference("<%= class_name %>.count") do
25-
post <%= index_helper(type: :url) %>, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }
25+
post <%= index_helper(type: :url) %>, params: { <%= "#{singular_table_name}: #{attributes_string}" %> }
2626
end
2727

2828
assert_redirected_to <%= show_helper("#{class_name}.last") %>
@@ -39,7 +39,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe
3939
end
4040

4141
test "should update <%= singular_table_name %>" do
42-
patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }
42+
patch <%= show_helper %>, params: { <%= "#{singular_table_name}: #{attributes_string}" %> }
4343
assert_redirected_to <%= show_helper %>
4444
end
4545

railties/test/generators/scaffold_controller_generator_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ def test_functional_tests_without_attributes
159159
assert_file "test/controllers/users_controller_test.rb" do |content|
160160
assert_match(/class UsersControllerTest < ActionDispatch::IntegrationTest/, content)
161161
assert_match(/test "should get index"/, content)
162-
assert_match(/post users_url, params: \{ user: \{ \} \}/, content)
163-
assert_match(/patch user_url\(@user\), params: \{ user: \{ \} \}/, content)
162+
assert_match(/post users_url, params: \{ user: \{\} \}/, content)
163+
assert_match(/patch user_url\(@user\), params: \{ user: \{\} \}/, content)
164164
end
165165
end
166166

railties/test/generators/scaffold_generator_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def test_functional_tests_without_attributes
170170
assert_file "test/controllers/product_lines_controller_test.rb" do |content|
171171
assert_match(/class ProductLinesControllerTest < ActionDispatch::IntegrationTest/, content)
172172
assert_match(/test "should get index"/, content)
173-
assert_match(/post product_lines_url, params: \{ product_line: \{ \} \}/, content)
174-
assert_match(/patch product_line_url\(@product_line\), params: \{ product_line: \{ \} \}/, content)
173+
assert_match(/post product_lines_url, params: \{ product_line: \{\} \}/, content)
174+
assert_match(/patch product_line_url\(@product_line\), params: \{ product_line: \{\} \}/, content)
175175
end
176176
end
177177

0 commit comments

Comments
 (0)