2009-05-19 60 views

回答

7

您可以运行生成器并忽略模型/迁移/灯具。

ruby script/generate rspec_model User --skip-migration --skip-fixture --skip 

我一直在寻找写东西来做到这一点,但没有任何其他人的兴趣。

17

在用于Rails 3的rspec-rails-2中,所有rspec生成器都已被删除。

您可以通过运行导轨模型生成器来解决此问题。您可以添加-s以跳过任何现有文件,并且可以使用--migration = false跳过创建迁移文件。

像这样:

rails generate model example -s --migration=false 
+0

无法在导轨上工作3.2.x – Rubytastic 2013-03-28 19:11:22

0

https://gist.github.com/omenking/7774140

require 'fileutils' 
namespace :spec do 

    def progress name, x, y 
    print "\r #{name}: #{x}/#{y} %6.2f%%" % [x.to_f/y * 100] 
    end 

    def generate_files name 
    kind  = name.to_s.singularize 
    collection = Dir.glob Rails.root.join('app',name.to_s,'**','*').to_s 
    root  = Rails.root.join('app',name.to_s).to_s<<'/' 
    ext  = case name 
        when :controllers then '_controller.rb' 
        when :models  then '.rb' 
       end 
    count = collection.count 
    collection.each_with_index do |i,index| 
     `rails g #{kind} #{$1} -s` if i =~ /#{root}(.+)#{ext}/ 
     progress name, index, count 
    end 
    end 

    task generate_missing: :environment do 
    generate_files :controllers 
    generate_files :models 
    end 
end 

# if you dont want certian things generated than 
# configure your generators in your application.rb eg. 
# 
# config.generators do |g| 
#  g.orm :active_record 
#  g.template_engine :haml 
#  g.stylesheets false 
#  g.javascripts false 
#  g.test_framework :rspec, 
#      fixture: false, 
#      fixture_replacement: nil 
# end 
# 
1

如果缺少规范的数量是相当小的,你可以简单地缺少规范运行的每个组件rails generate命令。

当发生冲突时,只需选择不覆盖原始文件。生成器将忽略现有文件并生成缺失的文件。