插件中的生成器和迁移 (rails 3)
我只是想创建一个没有任何参数的插件迁移生成器,例如: $railsgenerate yaffle
这应该将迁移文件(lib/generators/yaffle/template/create_yaffle.rb)复制到 db/迁移/[时间戳]_create_yaffle.rb。
- 我在这里面临的问题是,它的复制,但没有时间戳。
- 另外,当我运行 $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.
- The problem I am facing here is, its copying, but without timestamp.
- Also, when I run
$rails generate yaffle
it gives me a message that arguments are not provided, it expects to be in this formatrails generate yaffle NAME [options]
. I dont want to have any options/arguments, it should just berails 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我找到了答案...
Rails::Generators::NamedBase
而不是Rails::Generators::Base
!当您使用 NamedBase 时,它总是期望传递一个参数(这是初始化程序的名称)解释:guides.rubyonrails.org/generators
template
方法而不是migration_template
因为迁移文件不会产生任何迁移number说明: Rails::Generators::Migration.migration_template< /a>
最后,这成功了!
Ok, I found the answer...
Rails::Generators::NamedBase
instead ofRails::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
template
method instead ofmigration_template
because of which migration files din't produce any migration numberExplanation: Rails::Generators::Migration.migration_template
So finally, this worked!
对解决方案的一个小改进 - 为了省去定义迁移时间戳的麻烦,并在 Rails 核心团队决定使用另一种标记方式(例如 SHA 哈希值截断为 10 个字符)的情况下证明您的生成器的未来,您可以
require 'rails/generators/active_record'
并扩展 ActiveRecord::Generators::Migration
像这样:UPDATE In Rails 4
ActiveRecord::Generators::Migration
不再是一个模块,所以使用: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'
andextend ActiveRecord::Generators::Migration
like this:UPDATE In Rails 4
ActiveRecord::Generators::Migration
is no longer a module, so use instead:您可以简单地从
ActiveRecord::Generators::Base
继承,一切都会正常工作you can simply inherit from
ActiveRecord::Generators::Base
and everything will work