Skip to content

Instantly share code, notes, and snippets.

@goggin13
Last active December 13, 2015 16:48
Show Gist options
  • Save goggin13/4942476 to your computer and use it in GitHub Desktop.
Save goggin13/4942476 to your computer and use it in GitHub Desktop.
Rename a model named Micropost to one named MicroPost for our INFO 2310 class
def replace_in_file(path, str, replacement)
puts "replacing #{str} with #{replacement} in #{path}"
contents = File.read(path)
File.write(path, contents.gsub(str, replacement))
end
def do_cmd(cmd)
puts cmd
`#{cmd}`
end
if File.exist? "app/models/micropost.rb"
["app/**/*", "spec/**/*"].each do |root|
Dir.glob(root).each do |f|
if (f.index "microp") && !File.directory?(f)
replace_in_file f, "Micropost", "MicroPost"
replace_in_file f, "micropost", "micro_post"
end
end
Dir.glob(root).each do |f|
if (f.index "microp") && File.directory?(f)
do_cmd "git mv #{f} #{f.sub("microp", "micro_p")}"
end
end
Dir.glob(root).each do |f|
if (f.index "microp") && !File.directory?(f)
do_cmd "git mv #{f} #{f.sub("microp", "micro_p")}"
end
end
end
replace_in_file "config/routes.rb", "microp", "micro_p"
# create migration file
timestamp = Time.now.to_s.split(" ")[0..1].join(" ").gsub!(/\D/, "")
file_name = "db/migrate/#{timestamp}_rename_microposts_to_micro_posts.rb"
File.write(file_name,
<<-ruby
class RenameMicropostsToMicroPosts < ActiveRecord::Migration
def change
rename_table :microposts, :micro_posts
end
end
ruby
)
do_cmd "bundle exec rake db:migrate"
do_cmd "bundle exec rake db:test:prepare"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment