5

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

这里有一个这样的发生器我一直在思考如何使用,例如(从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 
+0

我有同样的问题。我想使用向导,但原始源只有Rails 2兼容。然后我发现有人创建了Rails 3分支(https://github.com/jfelchner/wizardly),但生成器仍然在Rails 2代码中。 – 2011-04-18 02:57:13

+0

他们是完全不同的...有反馈的Rails-3叉,顺便说一句:https://github.com/alkesh/feedback – Roman 2011-04-19 08:39:14

回答

4

这真的取决于排序生成的是。很多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在一个块中。

这个小小的转换花了我大约三个小时的时间,通过源头追捕所有作品。最好的部分是我根本不必改变我的模板(我相信大多数发电机都是如此)。

所有的说法,我认为迁移一代可能是最少的记录,looking at the generator guide它真的没有太大的挑战,重新创建Rails3中的发电机。在我看来,绝对值得一提。

此外,我知道该指南的作者之一有一本书即将出版,其中有关发电机的一章 - 所以它肯定会得到更多的关注。