class DustAlbumsGenerator < Rails::Generator::Base def manifest record do |m| app(m) migrations(m) css(m) javascript(m) images(m) initializers(m) routes(m) m.readme 'README' end end def app(m) m.template "app/controllers/albums_controller.rb", "app/controllers/albums_controller.rb" m.template "app/controllers/photos_controller.rb", "app/controllers/photos_controller.rb" m.template "app/controllers/view_albums_controller.rb", "app/controllers/view_albums_controller.rb" m.template "app/helpers/albums_helper.rb", "app/helpers/albums_helper.rb" m.template "app/helpers/photos_helper.rb", "app/helpers/photos_helper.rb" m.template "app/helpers/view_albums_helper.rb", "app/helpers/view_albums_helper.rb" m.template "app/models/album.rb", "app/models/album.rb" m.template "app/models/photo.rb", "app/models/photo.rb" photos_dir = File.join(@source_root, 'app', 'views', 'photos') albums_dir = File.join(@source_root, 'app', 'views', 'albums') view_albums_dir = File.join(@source_root, 'app', 'views', 'view_albums') dst_dir = File.join(RAILS_ROOT, 'app', 'views') FileUtils.cp_r photos_dir, dst_dir, :verbose => true FileUtils.cp_r albums_dir, dst_dir, :verbose => true FileUtils.cp_r view_albums_dir, dst_dir, :verbose => true end def migrations(m) m.migration_template "migration/albums_migration.rb", "db/migrate", :migration_file_name => "create_albums" end def css(m) m.template "stylesheets/dust_album.css", "public/stylesheets/dust_album.css" m.template "stylesheets/uploadify.css", "public/stylesheets/uploadify.css" m.template "stylesheets/dust_album_app.css", "public/stylesheets/dust_album_app.css" end def javascript(m) m.template "javascript/dragsort.js", "public/javascripts/dragsort.js" m.template "javascript/dust_album.js", "public/javascripts/dust_album.js" src_dir = File.join(@source_root, 'javascript', 'uploadify') dst_dir = File.join(RAILS_ROOT, 'public', 'javascripts') FileUtils.cp_r src_dir, dst_dir, :verbose => true end def images(m) m.template "images/cancel.png", "public/images/cancel.png" m.template "images/browse.png", "public/images/browse.png" m.template "images/upload.png", "public/images/upload.png" m.template "images/save_position.png", "public/images/save_position.png" end def initializers(m) m.template "initializers/flash_session_cookie_middleware.rb", "config/initializers/flash_session_cookie_middleware.rb" m.template "initializers/session_store_middleware.rb", "config/initializers/session_store_middleware.rb" end def routes(m) m.route :name => 'view_albums', :url => 'all/albums', :controller => 'view_albums', :action => 'index' m.route :name => 'view_album', :url => 'photos-for/:filename', :controller => 'view_albums', :action => 'show' m.route :name => 'manage_photos', :url => 'manage-photos/:id', :controller => 'albums', :action => 'manage' m.route_resources 'photos' m.route_resources 'albums' end end module Rails module Generator module Commands class Base def route_code(route_options) "map.#{route_options[:name]} '#{route_options[:url]}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'" end end # Here's a readable version of the long string used above in route_code; # but it should be kept on one line to avoid inserting extra whitespace # into routes.rb when the generator is run: # "map.#{route_options[:name]} '#{route_options[:name]}', # :controller => '#{route_options[:controller]}', # :action => '#{route_options[:action]}'" class Create def route(route_options) sentinel = 'ActionController::Routing::Routes.draw do |map|' logger.route route_code(route_options) gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |m| "#{m}\n #{route_code(route_options)}\n" end end end class Destroy def route(route_options) logger.remove_route route_code(route_options) to_remove = "\n #{route_code(route_options)}" gsub_file 'config/routes.rb', /(#{to_remove})/mi, '' end end end end end