繁体   English   中英

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

[英]Converting rails 2 generators to rails 3?

将轨道2发电机转换为轨道3有多困难? 我一直在寻找有用的插件,但后来发现生成器只用于rails 2.我意识到它只是一个方便,但是如果将生成器迁移到rails 3就像调整生成器代码中的几行一样简单,我会只需这样做(并将工作提交给github以供将来的用户使用)。

这是我一直在考虑使用的一个这样的发生器,例如(来自反馈

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

这实际上取决于它的发电机种类。 很多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中重新创建生成器似乎并不太具挑战性。 在我看来绝对值得一试。

另外,我知道该指南的一位作者很快就会出版一本关于发电机的章节 - 所以它肯定会引起更多关注。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM