gpt4 book ai didi

ruby-on-rails - 如何覆盖 Rails 的默认迁移生成器模板

转载 作者:行者123 更新时间:2023-12-04 23:49:48 27 4
gpt4 key购买 nike

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

https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/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

谁能指出我正确的方向?

最佳答案

我想出了一个解决我的案例的方法:

我用这个命令创建了一个 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

在这里张贴,希望它会在 future 的某一天帮助某人。

关于ruby-on-rails - 如何覆盖 Rails 的默认迁移生成器模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25186549/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com