2012-08-22 15 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 
相关问题