插件中的生成器和迁移 (rails 3)

发布于 2024-10-01 08:03:42 字数 1139 浏览 2 评论 0原文

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

  1. 我在这里面临的问题是,它的复制,但没有时间戳。
  2. 另外,当我运行 $railsgenerate yaffle 时,它​​会向我显示一条消息,指出未提供参数,它预计采用这种格式:railsgenerate yaffle NAME [options]。我不想有任何选项/参数,它应该只是 railsgenerate 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

I am simply trying to create a plugin migration generator without any parameters, like : $rails generate yaffle and this should copy the migration file (lib/generators/yaffle/template/create_yaffle.rb) to db/migrate/[timestamp]_create_yaffle.rb.

  1. The problem I am facing here is, its copying, but without timestamp.
  2. Also, when I run $rails generate yaffle it gives me a message that arguments are not provided, it expects to be in this format rails generate yaffle NAME [options]. I dont want to have any options/arguments, it should just be rails generate yaffle.

What should I do?

I followed the generator used in acts_as_commentable , it looks pretty simple, but I don't know where to modify these settings... can anybody help?

Generator Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2024-10-08 08:03:42

好的,我找到了答案...

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

    解释:guides.rubyonrails.org/generators

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

    说明: Rails::Generators::Migration.migration_template< /a>

最后,这成功了!

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

Ok, I found the answer...

  1. I was using Rails::Generators::NamedBase instead of Rails::Generators::Base in my generator file! When you use NamedBase, it always expects an argument to be passed (which is the name of initializer)

    Explanation : guides.rubyonrails.org/generators

  2. And I was using template method instead of migration_template because of which migration files din't produce any migration number

    Explanation: Rails::Generators::Migration.migration_template

So finally, this worked!

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
清旖 2024-10-08 08:03:42

对解决方案的一个小改进 - 为了省去定义迁移时间戳的麻烦,并在 Rails 核心团队决定使用另一种标记方式(例如 SHA 哈希值截断为 10 个字符)的情况下证明您的生成器的未来,您可以require 'rails/generators/active_record'扩展 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

UPDATE In 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

A small polish on the solution - to save yourself the hassle of defining the timestamp for the migration and future proof your generator in case Rails core team decides to use another way of stamping (e.g. SHA hashes truncated to 10 characters), you can require 'rails/generators/active_record' and extend ActiveRecord::Generators::Migration like this:

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

UPDATE In Rails 4 ActiveRecord::Generators::Migration is no longer a module, so use instead:

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
大姐,你呐 2024-10-08 08:03:42

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

you can simply inherit from ActiveRecord::Generators::Base and everything will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文