包含迁移的通用方法。它假设迁移采用 [0-9]*_name 格式,并且可以通过其他框架(如 Sequel)使用,只需实现 next_migration_number
方法即可。
方法
属性
[R] | migration_class_name | |
[R] | migration_file_name | |
[R] | migration_number |
实例公共方法
create_migration(destination, data, config = {}, &block) 链接
来源: 显示 | 在 GitHub 上
# File railties/lib/rails/generators/migration.rb, line 35 def create_migration(destination, data, config = {}, &block) action Rails::Generators::Actions::CreateMigration.new(self, destination, block || data.to_s, config) end
migration_template(source, destination, config = {}) 链接
在给定的目标位置创建一个迁移模板。与默认模板方法的不同之处在于,迁移号会预先添加到目标文件名中。
迁移号、迁移文件名、迁移类名作为实例变量在模板中可用,以便进行渲染。
migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb"
来源: 显示 | 在 GitHub 上
# File railties/lib/rails/generators/migration.rb, line 56 def migration_template(source, destination, config = {}) source = File.expand_path(find_in_source_paths(source.to_s)) set_migration_assigns!(destination) dir, base = File.split(destination) numbered_destination = File.join(dir, ["%migration_number%", base].join("_")) file = create_migration numbered_destination, nil, config do ERB.new(::File.binread(source), trim_mode: "-", eoutvar: "@output_buffer").result(binding) end Rails::Generators.add_generated_file(file) end
set_migration_assigns!(destination) 链接
来源: 显示 | 在 GitHub 上
# File railties/lib/rails/generators/migration.rb, line 39 def set_migration_assigns!(destination) destination = File.expand_path(destination, destination_root) migration_dir = File.dirname(destination) @migration_number = self.class.next_migration_number(migration_dir) @migration_file_name = File.basename(destination, ".rb") @migration_class_name = @migration_file_name.camelize end