-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathconvert_head_response.rb
85 lines (74 loc) · 2.22 KB
/
convert_head_response.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true
Synvert::Rewriter.new 'rails', 'convert_head_response' do
configure(parser: Synvert::PRISM_PARSER)
description <<~EOS
It replaces render head response in controller files.
```ruby
render nothing: true
render nothing: true, status: :created
head status: 406
head location: '/foo'
```
=>
```ruby
head :ok
head :created
head 406
head :ok, location: '/foo'
```
EOS
if_gem 'actionpack', '>= 5.0'
within_file Synvert::RAILS_CONTROLLER_FILES do
# render nothing: true
# render nothing: true, status: :created
# =>
# head :ok
# head :created
with_node node_type: 'call_node',
receiver: nil,
name: 'render',
arguments: {
node_type: 'arguments_node',
arguments: { size: 1, first: { node_type: 'keyword_hash_node', nothing_value: true } }
} do
group do
replace :message, with: 'head'
if node.arguments.arguments.first.status_value.nil?
replace 'arguments.arguments.0', with: ':ok'
else
replace 'arguments.arguments.0', with: '{{arguments.arguments.0.status_source}}'
end
end
end
# head location: '/foo'
# =>
# head :ok, location: '/foo'
with_node node_type: 'call_node',
receiver: nil,
name: 'head',
arguments: {
node_type: 'arguments_node',
arguments: {
size: 1,
first: { node_type: 'keyword_hash_node', location_value: { not: nil } }
}
} do
replace 'arguments.arguments.0', with: ':ok, {{arguments.arguments.0.to_source}}'
end
# head status: 406
# =>
# head 406
with_node node_type: 'call_node',
receiver: nil,
name: 'head',
arguments: {
node_type: 'arguments_node',
arguments: {
size: 1,
first: { node_type: 'keyword_hash_node', status_value: { not: nil } }
}
} do
replace 'arguments.arguments.0', with: '{{arguments.arguments.0.status_source}}'
end
end
end