无论如何,我认为这不是干净但可行的解决方案:
配置/应用程序.rb
module YourApp
class Application < Rails::Application
config.generators do |g|
g.orm :concerns # to get run our generator under ActiveRecord
g.test_framework :rspec
end
end
end
lib/generators/rails/concerns/concerns_generator.rb
require 'rails/generators/active_record/model/model_generator'
module Rails
module Generators
hide_namespace 'concerns'
class ConcernsGenerator < ActiveRecord::Generators::ModelGenerator
source_root "#{base_root}/active_record/model/templates"
def create_concern_file
create_file "app/models/concerns/#{file_name}_concern.rb", <<-FILE
class #{class_name}Concern
end
FILE
end
def create_concern_spec
create_file "spec/models/#{file_name}_concern_spec.rb", <<-FILE
require 'spec_helper'
describe #{class_name}Concern
pending "add some examples to (or delete) #{__FILE__}"
end
FILE
end
end
end
end
这是给我们的:
$ rails g model Product name:string description:text
invoke concerns
create db/migrate/20131005122831_create_products.rb
create app/models/product.rb
invoke rspec
create spec/models/product_spec.rb
create app/models/concerns/product_concern.rb
create spec/models/product_concern_spec.rb