Add list of just-purchased items (this transaction) to the purchase view
authorIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 08:01:14 +0000 (00:01 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 08:01:14 +0000 (00:01 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
app/controllers/purchase_controller.rb
app/models/merchandise.rb
app/views/bitem/list.rhtml
app/views/merchandise/_form.rhtml
app/views/purchase/menu.rhtml
db/development.sqlite3
db/migrate/020_rebuild_merchandises_table.rb [new file with mode: 0644]
db/schema.rb

index 4445c07..704e38b 100644 (file)
@@ -12,6 +12,7 @@ class PurchaseController < ApplicationController
     # enter a customer id here
     render :action => 'begin'
     session[:total] = 0.00
+    session[:items] = []
   end
 
   def customer_ok
@@ -27,6 +28,7 @@ class PurchaseController < ApplicationController
   def menu
     @customer = Customer.find_by_id(session[:customer_id])
     @total_price = session[:total]
+    @items = session[:items]
     render :action => 'menu'
   end
 
@@ -69,10 +71,13 @@ class PurchaseController < ApplicationController
     purchase.customer_id = session[:customer_id]
     purchase.date = Time.now.to_date
     purchase.price = @rentable.calculated_price
-    session[:total] += @rentable.calculated_price
     purchase.rentable = @rentable
     purchase.save!
 
+    # Add te session variables
+    session[:total] += @rentable.calculated_price
+    session[:items].push @rentable
+
     flash[:notice] = "Successfully made purchase"
     redirect_to :action => :menu
   end
@@ -108,11 +113,14 @@ class PurchaseController < ApplicationController
     purchase.customer_id = session[:customer_id]
     purchase.date = Time.now.to_date
     purchase.price = @merchandise.price
-    session[:total] += @merchandise.price
     purchase.merchandise = @merchandise
     purchase.quantity = 1
     @merchandise.quantity -= 1
 
+    # Add to session variables
+    session[:total] += @merchandise.price
+    session[:items].push @merchandise
+
     # Save both the merchandise (we changed the quantity) and the purchase to the log
     @merchandise.save!
     purchase.save!
index 16fd1e8..efb7c7e 100644 (file)
@@ -1,7 +1,7 @@
 class Merchandise < ActiveRecord::Base
   has_many :merchandise_purchases
 
-  validates_presence_of :name
+  validates_presence_of :title
   validates_numericality_of :quantity
   validates_numericality_of :price
 
index 6b5bb14..81a7d7c 100644 (file)
@@ -12,7 +12,7 @@
 <% for bitem in @bitems %>
   <tr>
   <td><%=h bitem.customer.name %></td>
-  <td><%=h bitem.merchandise.name %></td>
+  <td><%=h bitem.merchandise.title %></td>
   <% for column in Bitem.content_columns %>
     <td><%=h bitem.send(column.name) %></td>
   <% end %>
index d7f7735..35db026 100644 (file)
@@ -1,8 +1,8 @@
 <%= error_messages_for 'merchandise' %>
 
 <!--[form:merchandise]-->
-<p><label for="merchandise_name">Name</label><br/>
-<%= text_field 'merchandise', 'name'  %></p>
+<p><label for="merchandise_title">Title</label><br/>
+<%= text_field 'merchandise', 'title'  %></p>
 
 <p><label for="merchandise_quantity">Quantity</label><br/>
 <%= text_field 'merchandise', 'quantity'  %></p>
index ce8a646..6f5abd1 100644 (file)
@@ -1,11 +1,23 @@
 <h1>Purchase Menu</h1>
 
+<h3>Customer Information</h3>
 <ul>
   <li>Customer ID: <%= @customer.id %></li>
   <li>Customer Name: <%= @customer.name %></li>
   <li>Total Price: <%= @total_price %></li>
 </ul>
 
+<h3>Items Purchased</h3>
+<% if @items.empty? %>
+  None
+<% else %>
+<ul>
+<% for item in @items %>
+  <li><%= item.title %></li>
+<% end %>
+</ul>
+<% end %>
+
 <ul>
   <li><%=link_to 'Rent an Item', :action => 'rent_begin' %></li>
   <li><%=link_to 'Buy Merchandise', :action => 'buy_begin' %></li>
index f482dbe..5ddceca 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ
diff --git a/db/migrate/020_rebuild_merchandises_table.rb b/db/migrate/020_rebuild_merchandises_table.rb
new file mode 100644 (file)
index 0000000..e59901a
--- /dev/null
@@ -0,0 +1,24 @@
+class RebuildMerchandisesTable < ActiveRecord::Migration
+
+  # This whole thing is an awful way of doing a column rename
+
+  def self.up
+    drop_table :merchandises
+
+    create_table :merchandises do |t|
+      t.column :title, :string, :null => false
+      t.column :quantity, :integer, :null => false, :default => 0
+      t.column :price, :decimal, :precision => 8, :scale =>2, :default => 0
+    end
+  end
+
+  def self.down
+    drop_table :merchandises
+
+    create_table :merchandises do |t|
+      t.column :name, :string, :null => false
+      t.column :quantity, :integer, :null => false, :default => 0
+      t.column :price, :decimal, :precision => 8, :scale =>2, :default => 0
+    end
+  end
+end
index 2e0f138..ae1459b 100644 (file)
@@ -2,7 +2,7 @@
 # migrations feature of ActiveRecord to incrementally modify your database, and
 # then regenerate this schema definition.
 
-ActiveRecord::Schema.define(:version => 19) do
+ActiveRecord::Schema.define(:version => 20) do
 
   create_table "bitems", :force => true do |t|
     t.column "customer_id",    :integer, :null => false
@@ -33,7 +33,7 @@ ActiveRecord::Schema.define(:version => 19) do
   end
 
   create_table "merchandises", :force => true do |t|
-    t.column "name",     :string,                                                 :null => false
+    t.column "title",    :string,                                                 :null => false
     t.column "quantity", :integer,                               :default => 0,   :null => false
     t.column "price",    :decimal, :precision => 8, :scale => 2, :default => 0.0
   end