【问题标题】:generators and migrations in plugins (rails 3)插件中的生成器和迁移(rails 3)
【发布时间】:2010-11-10 06:14:36
【问题描述】:

我只是想创建一个不带任何参数的插件迁移生成器,例如:$rails generate yaffle,这应该将迁移文件 (lib/generators/yaffle/template/create_yaffle.rb) 复制到 db/migrate/[timestamp] _create_yaffle.rb。

  1. 我在这里面临的问题是,它的复制,但没有时间戳。
  2. 此外,当我运行$rails generate yaffle 时,它会提示我没有提供参数,它预计会采用这种格式rails generate yaffle NAME [options]。我不想有任何选项/参数,应该只是rails generate yaffle

我该怎么办?

我按照acts_as_commentable中使用的生成器,看起来很简单,但我不知道在哪里修改这些设置...有人可以帮忙吗?

生成器代码:

require 'rails/generators'
require 'rails/generators/migration'

class ThumbitGenerator  Rails::Generators::NamedBase
  source_root File.expand_path('../templates', __FILE__)

  def self.next_migration_number(path)
    Time.now.utc.strftime("%Y%m%d%H%M%S")
  end

  def create_model_file
    template "like.rb", "app/models/like.rb"
    template "liking.rb", "app/models/liking.rb"
    template "create_likes.rb", "db/migrate/create_likes.rb"
    template "create_likings.rb", "db/migrate/create_likings.rb"
  end

end

【问题讨论】:

  • 是否可以链接您的插件以便我们进行测试?
  • 嘿,我想通了(看看答案)...我已经用我的生成器代码更新了帖子...

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-plugins


【解决方案1】:

好的,我找到了答案……

  1. 我在生成器文件中使用了Rails::Generators::NamedBase 而不是Rails::Generators::Base!当你使用 NamedBase 时,它​​总是希望传递一个参数(这是初始化器的名称)

    解释:guides.rubyonrails.org/generators

  2. 我使用的是template 方法而不是migration_template,因为迁移文件不会产生任何迁移编号

    说明:Rails::Generators::Migration.migration_template

终于,这成功了!

require 'rails/generators'
require 'rails/generators/migration'

class ThumbitGenerator < Rails::Generators::Base
  include Rails::Generators::Migration
  source_root File.expand_path('../templates', __FILE__)

  def self.next_migration_number(path)
    Time.now.utc.strftime("%Y%m%d%H%M%S")
  end

  def create_model_file
    template "like.rb", "app/models/like.rb"
    template "liking.rb", "app/models/liking.rb"
    migration_template "create_likes.rb", "db/migrate/create_likes.rb"
    migration_template "create_likings.rb", "db/migrate/create_likings.rb"
  end
end

【讨论】:

  • 当我使用多个迁移执行此操作并运行 rake db:migrate 我得到:多个迁移的版本号为 20110413160337,这是有道理的,因为两个迁移都是使用相同的时间戳创建的。你遇到过这个吗?
  • @Mike Farmer:将%6N 附加到 strftime 格式字符串以获取微秒数,您应该很高兴。像这样:gist.github.com/1110373
  • 您可以require 'rails/generators/active_record' 然后使用ActiveRecord::Generators::Base.next_migration_number(path) 作为 next_migration_number
  • 伙计们,如果我想使用类似 rails generate yaffle:install 的东西,有可能吗?谢谢
  • @NARKOZ,migration_template 将创建重复的迁移版本。
【解决方案2】:

解决方案的一个小改进 - 为您省去为迁移定义时间戳的麻烦,并在 Rails 核心团队决定使用另一种标记方式(例如,截断为 10 个字符的 SHA 哈希值)时为您的生成器提供未来证明require 'rails/generators/active_record'extend ActiveRecord::Generators::Migration可以这样:

require 'rails/generators'
require 'rails/generators/migration'
require 'rails/generators/active_record'

class ThumbitGenerator < Rails::Generators::Base
  include Rails::Generators::Migration
  extend ActiveRecord::Generators::Migration

  source_root File.expand_path('../templates', __FILE__)

  def create_model_file
    template "like.rb", "app/models/like.rb"
    template "liking.rb", "app/models/liking.rb"
    migration_template "create_likes.rb", "db/migrate/create_likes.rb"
    migration_template "create_likings.rb", "db/migrate/create_likings.rb"
  end
end

在 Rails 4 中更新 ActiveRecord::Generators::Migration 不再是模块,因此请改用:

require 'rails/generators'
require 'rails/generators/migration'
require 'rails/generators/active_record'

class ThumbitGenerator < Rails::Generators::Base
  include Rails::Generators::Migration
  # Implement the required interface for Rails::Generators::Migration
  def self.next_migration_number(dirname)
    ActiveRecord::Generators::Base.next_migration_number(dirname)
  end

  source_root File.expand_path('../templates', __FILE__)

  def create_model_file
    template "like.rb", "app/models/like.rb"
    template "liking.rb", "app/models/liking.rb"
    migration_template "create_likes.rb", "db/migrate/create_likes.rb"
    migration_template "create_likings.rb", "db/migrate/create_likings.rb"
  end
end

【讨论】:

    【解决方案3】:

    您可以简单地从 ActiveRecord::Generators::Base 继承,一切都会正常工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-28
      • 2013-02-16
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多