2012-08-22 43 views
0

我會愛乾淨的代碼了:Rails - 生成器模塊 - 如何將方法插入到application_controller?

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 

是否有滑軌,讓我以更優雅的形式注入該方法的雷神或發生器模塊的任何方法?

回答

0

使用定界符:

inject_into_file 'app/controllers/application_controller.rb', after: "protect_from_forgery" do <<-RUBY 
    # some code 
RUBY 
end 
相關問題