0

シンプルなビュージェネレーターを作成しようとしていますが、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と、が実行されると、変数はコマンドラインから渡された元の変数に戻ります。attributeshook_forattribute

どうすればこれを回避できますか?

4

1 に答える 1

1

呼び出された時点some_custom_methodで、属性は(を介して)すでに設定されておりARGV、コードをチェックしても、そこから属性を変更する明確な方法がわかりません。start次のように、ジェネレータでクラスメソッドをオーバーライドし、引数を直接操作することで、別のアプローチを使用できます。

class ViewGenerator < Rails::Generators::NamedBase
  # your code ...
  def self.start(args, config)
    args.insert(1, 'description:integer') # 0 being the view name
    super
  end
end
于 2012-12-15T20:16:58.750 に答える