Skip to content

Instantly share code, notes, and snippets.

@lenny
Last active August 29, 2015 13:57
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 lenny/9639657 to your computer and use it in GitHub Desktop.
Save lenny/9639657 to your computer and use it in GitHub Desktop.
Script used while upgrading from Rails 2 to Rails 3/4
dir = ARGV[0]
raise "Usage: #{$0} <DIR>" if dir.nil?
Dir[File.join(dir, '**/*.haml')].each do |f|
s = File.read(f)
lines = []
s.each_line do |l|
l = l.gsub('&=', '=')
lines << l
end
File.open(f, 'w') { |f| f.write(lines.join('')) }
end
~
require 'json'
dir = ARGV[0]
raise "Usage: #{$0} <DIR>" if dir.nil?
Dir[File.join(dir, '**/*.haml')].each do |f|
s = File.read(f)
lines = []
s.each_line do |l|
# get rid of parens e.g. form_for(..)
l = l.sub(/form_for\(([^\)]+)\)/, 'form_for \1')
if l.include?('- form_for ')
puts l
md = l.match(/^(\s*)-\s*form_for\s+(.*)do(.*)$/)
indentation, args_and_opts, blockargs = md.captures
if (md = args_and_opts.match(/(.*?),\s*(\S+\s*=>.*)/))
args, opts = md.captures
else
args, opts = args_and_opts, nil
end
#puts "args: #{args} opts: #{opts}"
if opts
if opts.include?('method')
md = opts.match(/(.method[^,\}]+)/)
method = md[1]
opts.sub!(method, '')
opts.sub!(/.html.?\s*=>\s*\{\s*\}/, '')
opts.sub!(/,\s*$/, '') # e.g. :url => user_rates_path(@user),
opts.sub!(/^\s*,\s*/, '')
#puts "method: #{method} opts: #{opts}"
end
opts.sub!(/\{[, ]*/, '{ ') # e.g. :html => { , :multipart => true }
opts.sub!(/[, ]*\}/, ' }') # :html => { :id => 'editScheduleForm', })
else
method = nil
end
args = args.split(',').map(&:strip)
if args.size == 2
name, object = args[0], args[1]
args[0] = object
args[1] = ":as => #{name}"
end
#puts "args: #{args.inspect} opts: #{opts.inspect}"
parts = ["#{indentation}= form_for #{args.join(', ')}"]
parts << method.strip unless method.nil?
parts << opts.strip unless opts.nil?
#puts "parts: #{parts.inspect}"
replacement_line = "#{parts.join(', ')} do #{blockargs.strip}\n"
puts replacement_line
lines << replacement_line
else
l.sub!('- form_tag', '= form_tag')
l.sub!(/- (\S*)fields_for /, '= \1fields_for ')
lines << l
end
end
File.open(f, 'w') { |f| f.write(lines.join('')) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment