如何重写Rails的默认迁移生成模板 [英] How to override Rails' default migration generator template

查看:108
本文介绍了如何重写Rails的默认迁移生成模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重写这些迁移模板:

I need to override these migration templates:

<一个href="https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb" rel="nofollow">https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb  和<一href="https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb" rel="nofollow">https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb

在我的Rails应用程序,让他们拿起模板从Rails应用程序,而不是宝石本身。我已经尝试了所有可能的组合,到目前为止,但没有运气。

inside my rails application so that they pick up the template from rails application instead of the gem itself. I've tried all possible combinations so far but no luck.

我已经试过什么:

- 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

任何人都可以点我到正确的方向?

Can anyone point me into the right direction?

推荐答案

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

I figured out a workaround for my case:

我创建Rails生成器使用该命令(在我的Rails应用程序目录) -

I create a Rails generator with this command(inside my Rails app directory) -

rails g generator my_migrator

它创建:

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

然后我说这个code。通过添加此code到my_generator.rb延长我的发电机导轨迁移产生:

Then I added this code to extend the rails migration generator in my generator by adding this code to my_generator.rb:

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

在一个希望这会帮助别人的某一天在将来这里张贴此。

Posted this here in a hope that it'll help someone some day in future.

这篇关于如何重写Rails的默认迁移生成模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆