Rails 5-如何创建自定义脚手架生成器?

时间:2018-12-03 03:05:16

标签: ruby-on-rails ruby ruby-on-rails-5 ruby-on-rails-5.1

目标是指挥...

bin/rails generate custom_scaffold Thing

...生成以下6个文件:

db/migrate/201812031331_create_things.rb
app/models/thing.rb
app/controllers/things_controller.rb
app/serializers/thing_serializer.rb
test/fixtures/things.yml
test/integration/requests/things_request_test.rb

...使用Rails 5。

我当前的设置会生成app/models/thing.rb,但不会填充Thing

预期:

class Thing < ApplicationRecord

end

当前:

class <%= class_name %> < ApplicationRecord

end

我已经阅读了这些Rails guides,但收效甚微。

有人有可行的例子吗?


我的设置:

# lib/generators/custom_scaffold/custom_scaffold_generator.rb

class CustomScaffoldGenerator < Rails::Generators::NamedBase
  source_root File.expand_path('templates', __dir__)

  def create_files
    copy_file 'migration.rb', "db/migrate/#{timestamp}_create_#{plural_name}.rb"
    copy_file 'model.rb', "app/models/#{file_name}.rb"
    copy_file 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
    copy_file 'serializer.rb', "app/serializers/#{file_name}_serializer.rb"
    copy_file 'fixture.yml', "test/fixtures/#{plural_name}.yml"
    copy_file 'request_test.rb', "test/integration/requests/#{plural_name}_request_test.rb"
  end

  private

  def timestamp
    Time.now.utc.strftime('%Y%m%d%H%M%S')
  end
end
# lib/generators/custom_scaffold/templates/model.rb

class <%= class_name %> < ApplicationRecord

end
# lib/generators/custom_scaffold/templates/controller.rb

module V1
  module Public
    class <%= class_name.pluralize %>Controller < ApplicationController

    end
  end
end
# lib/generators/custom_scaffold/templates/migration.rb
# Ignore for now
# lib/generators/custom_scaffold/templates/serializer.rb
# Ignore for now
# lib/generators/custom_scaffold/templates/fixture.yml
# Ignore for now
# lib/generators/custom_scaffold/templates/request_test.rb
# Ignore for now
# Gemfile

source 'https://rubygems.org'

ruby '2.4.1'
gem 'rails', '~> 5.1.6'
gem 'puma', '~> 3.7'
gem 'pg'
gem 'rack-cors', require: 'rack/cors'
gem 'olive_branch'
gem 'fast_jsonapi'
gem 'awesome_print'
gem 'byebug', '~> 10.0', groups: %i[development test]
gem 'yaml_db'

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'mina', '~> 1.2', require: false
  gem 'mina-puma', require: false
  gem 'rubocop', require: false
  gem 'annotate', require: false
end

1 个答案:

答案 0 :(得分:1)

您需要将文件指定为Thor模板。 Rails使用Thor模板生成内部带有ERB样式代码的模板。

替换:
copy_file 'model.rb', "app/models/#{file_name}.rb"

使用:
template 'model.rb.tt', "app/models/#{file_name}.rb"

通过添加.tt扩展名,您告诉生成器将文件作为Thor模板处理,该模板将解释文件中的Ruby代码(ERB样式),然后创建一个名称为减号的文件.tt扩展名。您拥有的任何不带扩展名.tt的文件,生成器都将批量复制,而无需执行其中的任何代码。

有用的提示:有时您想在Thor模板文件中保留一些ERB代码而不执行它们。默认情况下,.tt文件中的所有ERB样式标签都将被处理,并在该位置将字符串写入输出文件。您可以避免处理ERB标签,而在标签中使用双百分号。

例如,假设您有一个名为foo.erb.tt的文件,它将在生成器运行时创建文件foo.erb。我们还假设我们有一个article_name变量,其值为Breaking News

如果将<%= article_name %>放入文件中,则会将Breaking News写入foo.erb

如果您放置<%%= article_name %>(注意%%),则会将<%= article_name %>写入foo.erb

在学习这些东西时,我发现以下参考资料很方便。

Thor附带了一些有助于脚本和生成器任务的操作。您可能对它们很熟悉,因为其中一些来自Rails模板。它们是:sayaskyes?no?add_fileremove_filecopy_filetemplatedirectoryinsideruninject_into_file等。

相关问题