Skip to content

Instantly share code, notes, and snippets.

@tcopeland
Last active August 13, 2017 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcopeland/d3abe559e14391282c446d548b4a22fa to your computer and use it in GitHub Desktop.
Save tcopeland/d3abe559e14391282c446d548b4a22fa to your computer and use it in GitHub Desktop.
synvert rule to combine flash with redirect_to
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