msn email google-talk twitter tumblr flickr

使用template生成rails项目

此处先感谢俩个人的Dave童鞋,不然对这个template的功能我恐怕还要后知后觉…

在生成rails项目时 使用-m参数 加上要用的template.rb就ok了 如下

   1      rails blog -m ~/my_template.rb
   2      #Or apply an app template to an existing app:
   3      rake rails:template LOCATION=http://mydomain.com/templates/start.rb

github上搜template会有很多,自己改改就能用了.这里也贴下我现在的template.rb.

   1  # Delete unnecessary files
   2      run 'rm README'
   3      run 'rm public/index.html'
   4      run 'rm public/favicon.ico'
   5      run 'rm public/robots.txt'
   6  
   7      # Copy database.yml for distribution use
   8      run 'cp config/database.yml config/database.yml.example'
   9  
  10      run "rm public/javascripts/controls.js"
  11      run "rm public/javascripts/dragdrop.js"
  12      run "rm public/javascripts/effects.js"
  13      run "rm public/javascripts/prototype.js"
  14  
  15      # Download JQuery
  16      run "curl -L http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js > public/javascripts/jquery.js"
  17  
  18      # file 'app/views/layouts/_flashes.html.erb', 
  19      # %q{<div id="flash">
  20      #  <% flash.each do |key, value| -%>
  21      #    <div id="flash_<%= key %>"><%=h value %></div>
  22      #  <% end -%>
  23      # </div>
  24      # }
  25      # 
  26      # file 'app/views/layouts/application.html.erb', 
  27      # %q{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  28      #  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  29      # <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  30      #  <head>
  31      #    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  32      #    <title><%= PROJECT_NAME.humanize %></title>
  33      #    <%= stylesheet_link_tag 'application' %>
  34      #    <%= javascript_include_tag 'jquery' %>
  35      #  </head>
  36      #  <body class="<%= body_class %>">
  37      #    <%= render :partial => 'layouts/flashes' -%>
  38      #    <%= yield %>
  39      #  </body>
  40      # </html>
  41      # }
  42  
  43      file 'lib/core_ext.rb',
  44      %q{
  45       require'iconv'  
  46       class String  
  47        def to_gbk  
  48          Iconv.iconv("GB2312//IGNORE","UTF-8//IGNORE",self).to_s  
  49        end  
  50        def to_utf8  
  51          Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE",self).to_s  
  52        end  
  53        def utf8?    
  54          unpack('U*') rescue return false    
  55          true    
  56        end
  57       end  
  58      }
  59  
  60      file 'lib/tasks/bootstrap.rake',
  61      %q{
  62        namespace :db do
  63          desc "reset db,clone test, load fixtures under db/bootstrap"
  64          task :bootstrap do |task_args|  
  65            %w(environment db:migrate:reset
  66               db:test:clone db:bootstrap:load).each { |t| Rake::Task[t].invoke  }
  67            puts "ok"
  68          end  
  69          namespace :bootstrap do
  70           desc "Load initial database fixtures (in db/bootstrap/*.yml) into the current environment's database.  Load specific fixtures using FIXTURES=x,y"
  71           task :load => :environment do
  72             require 'active_record/fixtures'
  73             ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
  74             (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'db', 'bootstrap', '*.{yml,csv}'))).each do |fixture_file|
  75               Fixtures.create_fixtures('db/bootstrap', File.basename(fixture_file, '.*'))
  76             end
  77           end
  78  
  79          end 
  80        end
  81      }
  82      run "mkdir db/bootstrap"
  83      run "cp -r /Users/howl/init_rails/ar_fixtures ./vendor/plugins"
  84  
  85      # Create public controller
  86      # generate(:controller, "public", "index", "about", "contact")
  87      # route "map.root :controller => 'public'"
  88      # route "map.about 'about', :controller => 'public', :action => 'about'"
  89      # route "map.contact 'contact', :controller => 'public', :action => 'contact'"
  90  
  91      # Plugins
  92      plugin 'authlogic', :git => 'git://github.com/binarylogic/authlogic.git'
  93      plugin 'paperclip', :git => 'git://github.com/thoughtbot/paperclip.git'
  94      plugin 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git'
  95      plugin 'annotate_models', :git => 'git://github.com/ctran/annotate_models.git'
  96      plugin 'acts_as_list', :git => 'git://github.com/rails/acts_as_list.git'
  97      plugin 'acts_as_tree', :git => 'git://github.com/rails/acts_as_tree.git'
  98      # plugin 'ar_fixtures', :git => 'git://github.com/topfunky/ar_fixtures.git'
  99      # plugin "resource_controller", :git => "git://github.com/giraffesoft/resource_controller.git"
 100      # plugin 'sematic_form_builder', :git => 'git://github.com/rubypond/semantic-form-builder.git'
 101      # plugin 'factory_girl', :git => "git://github.com/thoughtbot/factory_girl.git"
 102      # plugin 'exception_notifier', :git => 'git://github.com/rails/exception_notification.git'
 103      # plugin 'asset_packager', :git => 'http://synthesis.sbecker.net/pages/asset_packager'
 104      # plugin 'lazy_developer', :git => 'git://github.com/napcs/lazy_developer.git'
 105      # plugin 'rspec', :git => "git://github.com/dchelimsky/rspec.git"
 106      # plugin 'rspec-rails', :git => "git://github.com/dchelimsky/rspec-rails.git"
 107      # plugin 'cucumber', :git => "git://github.com/aslakhellesoy/cucumber.git"
 108  
 109  
 110      rake("rails:freeze:gems") if yes?("Freeze rails gems ?")
 111      generate(:nifty_layout)
 112      # generate(:scaffold, "post title:string body:text")
 113      # route "map.root :controller => 'post'"
 114      # rake("db:create")
 115      # rake("db:migrate")
 116  
 117      git :init
 118      git :add => "."
 119      git :commit => "-a -m 'Initial commit'"