Last active
August 13, 2017 04:22
-
-
Save tcopeland/d3abe559e14391282c446d548b4a22fa to your computer and use it in GitHub Desktop.
synvert rule to combine flash with redirect_to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Synvert::Rewriter.new 'rails', 'redirect_with_notice' do | |
description <<-EOS | |
Combines flash setting and redirect to. | |
flash[:notice] = "huzzah" | |
redirect_to root_path | |
=> | |
redirect_to root_path, notice: "huzzah" | |
EOS | |
within_file 'app/controllers/**/*rb' do | |
within_node type: 'def' do | |
line = nil | |
msg = nil | |
remover_action = nil | |
with_node type: 'send', receiver: 'flash', arguments: {size: 2, first: ':notice', last: {type: :str}} do | |
line = node.line | |
msg = node.arguments.last.to_source | |
remover_action = Rewriter::RemoveAction.new(self) | |
end | |
with_node type: 'send', receiver: nil, message: :redirect_to do | |
if line.present? && node.line == line+1 | |
@actions << remover_action | |
replace_with "{{message}} {{arguments}}, notice: #{msg}" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment