更好的方法来检测发电机中的HAML?

时间:2013-09-18 14:27:59

标签: ruby-on-rails

使用rails生成器,我希望能够根据用户设置的内容复制haml / erb模板。我试过看Rails.application.config.app_generators.template_engine,但我似乎得到一个空哈希,无论是否设置了haml或erb。

我现在正在做的是:

file_type = 'erb'
begin
  if Haml
    file_type = 'haml'
  end
rescue NameError
end
copy_file "views/errors/internal_server_error.html.#{file_type}", "app/views/errors/internal_server_error.html.#{file_type}"
copy_file "views/errors/not_found.html.#{file_type}", "app/views/errors/not_found.html.#{file_type}"
copy_file "views/errors/unprocessable_entity.html.#{file_type}", "app/views/errors/unprocessable_entity.html.#{file_type}"

但那令人难以置信的hacky。还有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

能够弄清楚。使用haml,它将选项上的template_engine设置为:haml。在生成器内部,我能够像这样访问该值:

Rails::Generators.options[:rails][:template_engine]

我可以在这里看到我如何使用它:https://github.com/agmcleod/mosaic-errors/blob/95dff6fb8140b74e636d1dee0f6bf023a50706c6/lib/generators/errors_generator.rb