0

このコードをクリーンアップしたいと思います:

  def insert_general_methods
    inject_into_file "app/controllers/application_controller.rb", after: "protect_from_forgery"  do
      a = "\n\n  private\n\n  def current_user\n"
      b = "    @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]\n"
      c = "  end\n"
      d = "\n  helper_method :current_user\n\n"
      e = "  def authorize\n"
      f = "    redirect_to login_url, alert: 'Not authorized. Please login.' if current_user.nil?\n"
      g = "  end\n"
      a+b+c+d+e+f+g
    end
  end

このメソッドをよりエレガントな形式で注入できるようにする、Rails の Thor または Generator モジュールのメソッドはありますか?

4

1 に答える 1

1

ヒアドキュメント構文を使用します。

 inject_into_file 'app/controllers/application_controller.rb', after: "protect_from_forgery"  do <<-RUBY
   # some code
 RUBY
 end
于 2012-10-19T00:44:12.643 に答える