シンプルなビュージェネレーターを作成しようとしていますが、DRYの原則を使用しているので、独自のhtml(erb / haml / Slim)テンプレートは必要ありません。ジェネレーターを既存のテンプレートエンジンにフックして、いくつかの引数を渡したいのですが。
私のview_generator.rb
ファイルは次のようになります。
class ViewGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
def some_custom_method
(...)
end
hook_for :template_engine, :as => :scaffold
end
すべてがこのように正常に動作します。私がやりたいのsome_custom_method
は、いくつかの属性を追加することです。
def some_custom_method
new_attribute = Rails::Generators::GeneratedAttribute.new("description")
new_attribute.type = :integer
attributes << new_attribute
end
配列に挿入するnew_attribute
と、が実行されると、変数はコマンドラインから渡された元の変数に戻ります。attributes
hook_for
attribute
どうすればこれを回避できますか?