Permalink
Browse files

authorization completed, universal style changes made, testing (hopef…

…ully) completed for all of the functional bits.
  • Loading branch information...
1 parent 028c433 commit 8cbb96a329a139a8bb3b339d0be0b8221f771fde @illuminerdi committed Feb 16, 2009
@@ -15,6 +15,9 @@ def login
end
def logout
+ session[:user_id] = nil
+ flash[:notice] = "Logged out"
+ redirect_to(:action => :login)
end
def index
@@ -1,7 +1,8 @@
class StoreController < ApplicationController
+ before_filter :find_cart, :except => :empty_cart
+
def index
@products = Product.find_products_for_sale
- @cart = find_cart
end
def add_to_cart
@@ -11,7 +12,6 @@ def add_to_cart
logger.error("Attempt to access invalid product: #{params[:id]}")
redirect_to_index "Invalid Product"
else
- @cart = find_cart
@current_item = @cart.add_product(product)
respond_to do |format|
format.js if request.xhr?
@@ -26,7 +26,6 @@ def empty_cart
end
def checkout
- @cart = find_cart
if @cart.items.empty?
redirect_to_index("Your cart is empty")
else
@@ -35,7 +34,6 @@ def checkout
end
def save_order
- @cart = find_cart
@order = Order.new(params[:order])
#@order.add_line_items_from_cart(@cart)
if @order.save
@@ -49,7 +47,7 @@ def save_order
private
def find_cart
- session[:cart] ||= Cart.new
+ @cart = (session[:cart] ||= Cart.new)
end
def redirect_to_index(msg = nil)
@@ -76,6 +76,12 @@ def update
def destroy
@user = User.find(params[:id])
@user.destroy
+ begin
+ flash[:notice] = "User #{@user.name} deleted"
+ @user.destroy
+ rescue Exception => e
+ flash[:notice] = e.message
+ end
respond_to do |format|
format.html { redirect_to(users_url) }
View
@@ -37,6 +37,12 @@ def password=(pwd)
self.hashed_password = User.encrypted_password(self.password, self.salt)
end
+ def after_destroy
+ if User.count.zero?
+ raise "Can't delete last user"
+ end
+ end
+
private
def password_not_blank
@@ -0,0 +1,44 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <title>Pragprog Books Online Store</title>
+ <%= stylesheet_link_tag "depot", :media => "all" %>
+ <%= javascript_include_tag :defaults %>
+</head>
+<body id="store">
+ <div id="banner">
+ <%= image_tag("logo.png") %>
+ <%= @page_title || "Illuminerdi Bookshelf" %>
+ </div>
+ <div id="columns">
+ <div id="side">
+ <% if @cart -%>
+ <% hidden_div_if(@cart.items.empty?, :id => "cart") do -%>
+ <%= render(:partial => "cart", :object => @cart) %>
+ <% end -%>
+ <% end %>
+ <%= link_to 'Home', '/' %><br/>
+
+ <% if session[:user_id] %>
+ <br />
+ <%= link_to 'Orders', :controller => 'orders' %><br />
+ <%= link_to 'Products', :controller => 'products' %><br />
+ <%= link_to 'Users', :controller => 'users' %><br />
+ <br />
+ <%= link_to 'Logout', :controller => :admin, :action => 'logout' %>
+ <% end %>
+
+ </div>
+ <div id="main">
+ <% if flash[:notice] -%>
+ <div id="notice"><%= flash[:notice] %></div>
+ <% end -%>
+
+ <%= yield :layout %>
+ </div>
+ </div>
+</body>
+</html>
@@ -26,4 +26,12 @@ class AdminControllerTest < ActionController::TestCase
assert_redirected_to users_path
end
+
+ test "user gets logged out" do
+ @request.session[:user_id] = users(:one).id
+ get :logout
+ assert_redirected_to :controller => :admin, :action => :login
+ assert ! @response.session[:user_id]
+ assert_equal @response.flash[:notice], "Logged out"
+ end
end
@@ -71,5 +71,18 @@ def setup
end
assert_redirected_to users_path
+ assert_equal @response.flash[:notice], "User #{users(:one).name} deleted"
+ end
+
+ test "should not destroy the last user" do
+ users = User.find(:all)
+ assert_raise(RuntimeError) do
+ loop do
+ users.first.destroy
+ users.shift
+ end
+ end
+
+ assert_equal 1, users.length
end
end

0 comments on commit 8cbb96a

Please sign in to comment.