5

我需要覆盖这些迁移模板:

https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/migration.rbhttps://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b/ activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb

在我的 rails 应用程序中,以便他们从 rails 应用程序而不是 gem 本身获取模板。到目前为止,我已经尝试了所有可能的组合,但没有运气。

我试过的:

- Rails.root/lib/templates/rails/migration/migration.rb
- Rails.root/lib/templates/rails/migration_generator/migration.rb
- Rails.root/lib/rails/templates/rails/migration/migration.rb
- Rails.root/lib/rails/templates/rails/migration_generator/migration.rb
- Rails.root/lib/rails/generators/templates/rails/migration/migration.rb
- Rails.root/lib/rails/generators/templates/rails/migration_generator/migration.rb
- Rails.root/lib/rails/templates/generators/rails/migration/migration.rb
- Rails.root/lib/rails/templates/generators/rails/migration_generator/migration.rb
- Rails.root/lib/rails/generators/active_record/migration/templates/migration.rb
- Rails.root/lib/rails/generators/active_record/migration_generator/templates/migration.rb
- Rails.root/lib/rails/generators/active_record/templates/migration.rb
- Rails.root/lib/rails/generators/active_record/templates/migration.rb

谁能指出我正确的方向?

4

1 回答 1

7

我为我的情况想出了一个解决方法:

我用这个命令创建了一个 Rails 生成器(在我的 Rails 应用程序目录中) -

rails g generator my_migrator

它创建:

lib/generators/my_migrator/my_migrator.rb
lib/generators/my_migrator/templates
lib/generators/my_migrator/USAGE

然后我添加了这段代码,通过将这段代码添加到 my_generator.rb 来扩展我的生成器中的 rails 迁移生成器:

require 'rails/generators'
require 'rails/generators/active_record'
require 'rails/generators/actions/create_migration'

class MyGenerator < ActiveRecord::Generators::Base
  source_root File.expand_path('../templates', __FILE__)
  argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"

  def create_migration_file
    set_local_assigns!
    validate_file_name!
    migration_template @migration_template, "db/migrate/#{file_name}.rb"
    # more code here as per requirement.
  end

  # more code below as per requirement..
end

将其发布在这里,希望将来有一天能对某人有所帮助。

于 2014-08-19T08:18:20.563 回答