application_controller.rb 862 B

12345678910111213141516171819202122232425262728293031
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. class ApplicationController < ActionController::Base
  10. before_filter :authorize
  11. protect_from_forgery
  12. private
  13. def current_cart
  14. Cart.find(session[:cart_id])
  15. rescue ActiveRecord::RecordNotFound
  16. cart = Cart.create
  17. session[:cart_id] = cart.id
  18. cart
  19. end
  20. # ...
  21. protected
  22. def authorize
  23. unless User.find_by_id(session[:user_id])
  24. redirect_to login_url, notice: "Please log in"
  25. end
  26. end
  27. end