将轨道 2 发电机转换为轨道 3?

发布于 2024-10-13 08:43:13 字数 3564 浏览 2 评论 0原文

将轨 2 发电机转换为轨 3 有多困难?我不断寻找有用的插件,但后来发现生成器仅适用于 Rails 2。我意识到其中一些只是为了方便,但如果将生成器迁移到 Rails 3 就像调整生成器代码中的几行一样简单,我会就这样做(并将工作提交到 github 供未来的用户使用)。

例如,我一直在考虑使用这样一个生成器(来自 feedback

require File.expand_path(File.dirname(__FILE__) + "/lib/insert_routes.rb")

class FeedbackFormGenerator < Rails::Generator::Base

  attr_accessor :name, 
    :model_class_name,
    :controller_class_name,
    :helper_class_name,
    :mailer_class_name


  def initialize(runtime_args, runtime_options = {})
    super
    @name = (runtime_args[0] || "feedback").downcase
    @model_class_name = name.classify
    @mailer_class_name = "#{@model_class_name}Mailer"
    @controller_class_name = "#{@model_class_name.pluralize}Controller"
    @helper_class_name = "#{@model_class_name.pluralize}Helper"
    #@js_framework = (runtime_options[''])

  end

  def manifest
    record do |m|

      puts "hello"
      add_model(m)
      add_mailer(m)
      add_controller(m)
      add_helper(m)
      add_views(m)
      add_routes(m)
      add_unit_test(m)
      add_functional_test(m)
      add_stylesheet(m)
      add_javascript(m)
      add_images(m)
    end
  end


  def add_stylesheet(m)
    m.directory 'public/stylesheets'
    m.file 'feedback.css', 'public/stylesheets/feedback.css'

  end

  def add_javascript(m)
    m.directory 'public/javascripts'
    file_name = options[:jquery] ? 'jquery.feedback.js' : 'prototype.feedback.js'
    m.file file_name, "public/javascripts/#{file_name}"
  end

  def add_images(m)
    m.directory 'public/images/feedback'
    m.file "images/feedback_tab.png", "public/images/feedback/feedback_tab.png"
    m.file "images/feedback_tab_h.png", "public/images/feedback/feedback_tab_h.png"
    m.file "images/closelabel.gif", "public/images/feedback/closelabel.gif"
    m.file "images/loading.gif", "public/images/feedback/loading.gif"
  end

  def add_model(m)
    m.template 'feedback_model.rb.erb', "app/models/#{name}.rb"
  end

  def add_mailer(m)
    m.template 'feedback_mailer.rb.erb', "app/models/#{name}_mailer.rb"
    m.directory "app/views/#{name}_mailer"
    m.file 'views/feedback_mailer/feedback.html.erb', "app/views/#{name}_mailer/feedback.html.erb"

  end

  def add_controller(m)
    m.template 'feedbacks_controller.rb.erb', "app/controllers/#{name.pluralize}_controller.rb"
  end

  def add_helper(m)
    template_name = options[:jquery] ? 'feedbacks_helper.rb.jquery.erb' : 'feedbacks_helper.rb.prototype.erb'
    m.template template_name, "app/helpers/#{name.pluralize}_helper.rb"
  end

  def add_views(m)
    m.directory "app/views/#{name.pluralize}"
    m.file 'views/feedbacks/new.html.erb', "app/views/#{name.pluralize}/new.html.erb"
  end

  def add_routes(m)
    m.route_name "new_feedback", "feedbacks/new", {:controller => name.pluralize, :action => "new"}
    m.route_name "feedback", "feedbacks", {:controller => name.pluralize, :action => "create"}
  end

  def add_unit_test(m)
    m.template 'feedback_test.rb.erb', "test/unit/#{name}_test.rb"
    m.template 'feedback_mailer_test.rb.erb', "test/unit/#{name}_mailer_test.rb"
  end

  def add_functional_test(m)
    m.template 'feedbacks_controller_test.rb.erb', "test/functional/#{name.pluralize}_controller_test.rb"    
  end

  protected 

  def add_options!(opt)
    opt.separator ''
    opt.separator 'Options:'
    opt.on("--jquery",
      "Use jquery Javascript framework, default is Prototyp")           { |v| options[:jquery] = true }
  end
end

How difficult is it to convert rails 2 generators to rails 3? I keep finding useful plugins but then finding that the generators are only for rails 2. I realize some of it is just a convenience, but if migrating the generator to rails 3 is as simple as tweaking a few lines in the generator code, I would be down to just do that (and commit the work to github for future users as well).

Here's one such generator I've been thinking about using, for example (from feedback)

require File.expand_path(File.dirname(__FILE__) + "/lib/insert_routes.rb")

class FeedbackFormGenerator < Rails::Generator::Base

  attr_accessor :name, 
    :model_class_name,
    :controller_class_name,
    :helper_class_name,
    :mailer_class_name


  def initialize(runtime_args, runtime_options = {})
    super
    @name = (runtime_args[0] || "feedback").downcase
    @model_class_name = name.classify
    @mailer_class_name = "#{@model_class_name}Mailer"
    @controller_class_name = "#{@model_class_name.pluralize}Controller"
    @helper_class_name = "#{@model_class_name.pluralize}Helper"
    #@js_framework = (runtime_options[''])

  end

  def manifest
    record do |m|

      puts "hello"
      add_model(m)
      add_mailer(m)
      add_controller(m)
      add_helper(m)
      add_views(m)
      add_routes(m)
      add_unit_test(m)
      add_functional_test(m)
      add_stylesheet(m)
      add_javascript(m)
      add_images(m)
    end
  end


  def add_stylesheet(m)
    m.directory 'public/stylesheets'
    m.file 'feedback.css', 'public/stylesheets/feedback.css'

  end

  def add_javascript(m)
    m.directory 'public/javascripts'
    file_name = options[:jquery] ? 'jquery.feedback.js' : 'prototype.feedback.js'
    m.file file_name, "public/javascripts/#{file_name}"
  end

  def add_images(m)
    m.directory 'public/images/feedback'
    m.file "images/feedback_tab.png", "public/images/feedback/feedback_tab.png"
    m.file "images/feedback_tab_h.png", "public/images/feedback/feedback_tab_h.png"
    m.file "images/closelabel.gif", "public/images/feedback/closelabel.gif"
    m.file "images/loading.gif", "public/images/feedback/loading.gif"
  end

  def add_model(m)
    m.template 'feedback_model.rb.erb', "app/models/#{name}.rb"
  end

  def add_mailer(m)
    m.template 'feedback_mailer.rb.erb', "app/models/#{name}_mailer.rb"
    m.directory "app/views/#{name}_mailer"
    m.file 'views/feedback_mailer/feedback.html.erb', "app/views/#{name}_mailer/feedback.html.erb"

  end

  def add_controller(m)
    m.template 'feedbacks_controller.rb.erb', "app/controllers/#{name.pluralize}_controller.rb"
  end

  def add_helper(m)
    template_name = options[:jquery] ? 'feedbacks_helper.rb.jquery.erb' : 'feedbacks_helper.rb.prototype.erb'
    m.template template_name, "app/helpers/#{name.pluralize}_helper.rb"
  end

  def add_views(m)
    m.directory "app/views/#{name.pluralize}"
    m.file 'views/feedbacks/new.html.erb', "app/views/#{name.pluralize}/new.html.erb"
  end

  def add_routes(m)
    m.route_name "new_feedback", "feedbacks/new", {:controller => name.pluralize, :action => "new"}
    m.route_name "feedback", "feedbacks", {:controller => name.pluralize, :action => "create"}
  end

  def add_unit_test(m)
    m.template 'feedback_test.rb.erb', "test/unit/#{name}_test.rb"
    m.template 'feedback_mailer_test.rb.erb', "test/unit/#{name}_mailer_test.rb"
  end

  def add_functional_test(m)
    m.template 'feedbacks_controller_test.rb.erb', "test/functional/#{name.pluralize}_controller_test.rb"    
  end

  protected 

  def add_options!(opt)
    opt.separator ''
    opt.separator 'Options:'
    opt.on("--jquery",
      "Use jquery Javascript framework, default is Prototyp")           { |v| options[:jquery] = true }
  end
end

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

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

发布评论

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

评论(1

像极了他 2024-10-20 08:43:13

这实际上取决于发电机的类型。 Rails 的很多内部结构在 2 和 3 之间发生了很大变化。让我向您展示我最近将我的一个应用程序中的一个非常简单的迁移生成器从 2 转换为 3 的经验。

是 2 的代码:

class LegacyMigrationGenerator < MigrationGenerator
  def manifest
    record do |m|
      m.migration_template 'legacy_migration.rb', 'db/migrate'
    end
  end
end

这 3 代码:

require 'rails/generators/active_record'

class LegacyMigrationGenerator < ActiveRecord::Generators::Base
  source_root File.expand_path('../templates', __FILE__)
  def create_migration_file
    migration_template "legacy_migration.rb", "db/migrate/#{file_name}.rb"
  end
end

因此,正如您所看到的 - 完全不同的重写方法,现在必须从生成器继承,现在必须调用此 source_root (以前是自动的),并且不再调用块中的 migration_template

这个小小的转换花了我大约三个小时来通过源头寻找所有的片段。最好的部分是我根本不需要更改我的模板(我相信对于大多数生成器来说都是如此)。

话虽这么说,我认为迁移生成可能是记录最少的,查看生成器指南在 Rails3 中重新创建生成器似乎并没有太大的挑战性。在我看来绝对值得一试。

另外,我知道该指南的一位作者很快就会出版一本书,其中有一章都是关于生成器的 - 所以它肯定会受到更多关注。

It really depends on the sort of generator it is. A lot of the internals of Rails have changed a lot between 2 and 3. Let me show you my recent experience in converting a very simple migration generator which I have in one of my apps from 2 to 3.

Here's the 2 code:

class LegacyMigrationGenerator < MigrationGenerator
  def manifest
    record do |m|
      m.migration_template 'legacy_migration.rb', 'db/migrate'
    end
  end
end

And here's the 3 code:

require 'rails/generators/active_record'

class LegacyMigrationGenerator < ActiveRecord::Generators::Base
  source_root File.expand_path('../templates', __FILE__)
  def create_migration_file
    migration_template "legacy_migration.rb", "db/migrate/#{file_name}.rb"
  end
end

So, as you can see - totally different method to override, had to inherit from a generator now, had to call this source_root now (used to be automatic), and no longer calling the migration_template in a block.

This small conversion took me about three hours to hunt down all the pieces through the source. The best part was that I didn't have to change my template at all (and I believe that will be true for most generators).

All that being said, I think migration generation is probably the least well documented, looking at the generator guide it really doesn't seem too challenging to recreate generators in Rails3. Definitely worth taking a crack at in my opinion.

Also, I know that one of the authors of that guide has a book coming out soon with a chapter all about generators - so it's definitely something which will be getting more attention.

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