class SimpleAuditGenerator < Rails::Generator::NamedBase def manifest record do |m| m.migration_template 'migration.rb', 'db/migrate', :assigns => { :migration_name => "CreateAudits" }, :migration_file_name => "create_audits" sentinel = 'ActionController::Routing::Routes.draw do |map|' logger.route "map.resources :audits" gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match| "#{match}\n map.resources :audits\n map.resources :users, :has_many => 'audits'\n" end end end def gsub_file(relative_destination, regexp, *args, &block) path = destination_path(relative_destination) content = File.read(path).gsub(regexp, *args, &block) File.open(path, 'wb') { |file| file.write(content) } end end