Skip to content

Instantly share code, notes, and snippets.

@ainame
Created July 14, 2011 16:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ainame/1082820 to your computer and use it in GitHub Desktop.
Save ainame/1082820 to your computer and use it in GitHub Desktop.
Rails3のscaffoldで生成されたコントローラーからrespond_toブロックを消す。lib/taskに配置して使う
# -*- coding: utf-8 -*-
require "tempfile"
desc "scaffoldで生成されたコントローラーからrespond_toブロックを消す"
namespace :my do
task :rm_respond_to do
Dir::glob("app/controllers/*_*.rb") do |ctrl|
if !(File::basename(ctrl) =~ /application/)
temp = Tempfile::new("tmp",Dir::pwd)
open(ctrl) do |f|
text = f.read
text.gsub!(/\n.+?respond_to.+?\n.+?\n.+?\n.+?end\n/) { |s|
if s =~ /(redirect_to\(.+?\))/
s = "\n\s\s\s\s"+$1+"\n"
else
s = ""
end
}
text.gsub!(/\n.+?respond_to.+?(\n.+?if.+?\n.+?\n.+?\n.+?else.*?\n.+?\n.+?\n.+?end\n).+?end\n/) {|s|
#if block取り出し
if_block = $1
while if_block =~ /format.html {(.+?)}/
tmp = $1
if_block.sub!(/\sformat.html {(.+?)}/,tmp)
end
if_block.gsub!(/\n.*?format.xml.*?{.+?}/,"")
#フォーマットを整える
if_block
if_block.gsub!(/^\s\s/,"")
s = "\n"+if_block.sub(/^\s/,"")
}
temp.write(text)
end
temp.close
temp.open
open(ctrl,"w") do |f|
f.write(temp.open.read)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment