Skip to content

Commit b7472ce

Browse files
committed
add snippet rails/explicitly-render-with-formats
1 parent 72c2ec1 commit b7472ce

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
Synvert::Rewriter.new 'rails', 'explicitly-render-with-formats' do
4+
configure(parser: Synvert::PRISM_PARSER)
5+
6+
description <<~EOS
7+
It calls render with formats explicitly.
8+
9+
```ruby
10+
render template: 'index.json'
11+
```
12+
13+
=>
14+
15+
```ruby
16+
render template: 'index', foramts: [:json]
17+
```
18+
EOS
19+
20+
if_gem 'active_support', '>= 7.0'
21+
22+
within_files Synvert::RAILS_CONTROLLER_FILES do
23+
find_node ".call_node[receiver=nil][name=render][arguments=.arguments_node[arguments.size=1][arguments.0=.keyword_hash_node]]" do
24+
template_value = node.arguments.arguments.first.template_value.to_value
25+
if template_value&.split('.')&.size == 2
26+
replace 'arguments.arguments.0.template_value', with: "'#{template_value.split('.').first}'"
27+
insert "formats: [:#{template_value.split('.').last}]", at: 'end', to: 'arguments.arguments.0', and_comma: true
28+
end
29+
end
30+
end
31+
end

lib/rails/upgrade_6_1_to_7_0.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
add_snippet 'rails', 'deprecate_errors_as_hash'
88
add_snippet 'rails', 'remove_active_support_dependencies_private_api'
99
add_snippet 'rails', 'update_active_storage_variant_argument'
10+
add_snippet 'rails', 'explicitly-render-with-formats'
1011
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
Synvert::Rewriter.new 'rails_best_practices', 'use_scope_access' do
4+
configure(parser: Synvert::PRISM_PARSER)
5+
6+
description <<~EOS
7+
It converts Foo to Bar
8+
9+
```ruby
10+
Foo
11+
```
12+
13+
=>
14+
15+
```ruby
16+
Bar
17+
```
18+
EOS
19+
20+
within_files '**/*.rb' do
21+
with_node type: 'const', to_source: 'Foo' do
22+
replace_with 'Bar'
23+
end
24+
end
25+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe 'Explicitly render with formats' do
6+
let(:rewriter_name) { 'rails/explicitly-render-with-formats' }
7+
let(:fake_file_path) { 'app/controllers/foo_controller.rb' }
8+
let(:test_content) { "render template: 'index.json'" }
9+
let(:test_rewritten_content) { "render template: 'index', formats: [:json]" }
10+
11+
include_examples 'convertable'
12+
end

0 commit comments

Comments
 (0)