Permalink
Browse files

Finished iteration G with playtime

  • Loading branch information...
arunsark committed Mar 29, 2011
1 parent 8959a95 commit 258262b88c89b98bd8b92e708f07eaed538f5975
Showing with 527 additions and 9 deletions.
  1. +2 −0 Gemfile
  2. +2 −0 Gemfile.lock
  3. +92 −0 app/controllers/orders_controller.rb
  4. +8 −0 app/controllers/products_controller.rb
  5. +2 −0 app/helpers/orders_helper.rb
  6. +3 −0 app/models/line_item.rb
  7. +15 −0 app/models/order.rb
  8. +3 −0 app/models/payment_type.rb
  9. +1 −0 app/models/product.rb
  10. +5 −0 app/views/carts/_cart.html.erb
  11. +1 −1 app/views/layouts/application.html.erb
  12. +1 −0 app/views/line_items/create.js.rjs
  13. +0 −2 app/views/line_items/new.html.erb
  14. +33 −0 app/views/orders/_form.html.erb
  15. +6 −0 app/views/orders/edit.html.erb
  16. +30 −0 app/views/orders/index.html.erb
  17. +6 −0 app/views/orders/new.html.erb
  18. +25 −0 app/views/orders/show.html.erb
  19. +41 −0 app/views/products/who_bought.atom.builder
  20. +7 −1 config/routes.rb
  21. +16 −0 db/migrate/20110328124646_create_orders.rb
  22. +9 −0 db/migrate/20110328124722_add_order_id_to_line_item.rb
  23. +13 −0 db/migrate/20110329105645_create_payment_types.rb
  24. +9 −0 db/migrate/20110329122127_remove_pay_type_from_order.rb
  25. +9 −0 db/migrate/20110329124602_add_payment_type_id_to_order.rb
  26. +17 −1 db/schema.rb
  27. +35 −0 public/stylesheets/depot.css
  28. +11 −0 script/load_orders.rb
  29. +5 −0 script/load_payment_types.rb
  30. +9 −4 test/fixtures/line_items.yml
  31. +13 −0 test/fixtures/orders.yml
  32. +7 −0 test/fixtures/payment_types.yml
  33. +12 −0 test/functional/line_items_controller_test.rb
  34. +59 −0 test/functional/orders_controller_test.rb
  35. +4 −0 test/unit/helpers/orders_helper_test.rb
  36. +8 −0 test/unit/order_test.rb
  37. +8 −0 test/unit/payment_type_test.rb
View
@@ -29,3 +29,5 @@ gem 'sqlite3-ruby', :require => 'sqlite3'
# group :development, :test do
# gem 'webrat'
# end
+
+gem 'will_paginate', '>= 3.0.pre'
View
@@ -66,10 +66,12 @@ GEM
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.25)
+ will_paginate (3.0.pre2)
PLATFORMS
ruby
DEPENDENCIES
rails (= 3.0.3)
sqlite3-ruby
+ will_paginate (>= 3.0.pre)
@@ -0,0 +1,92 @@
+class OrdersController < ApplicationController
+ # GET /orders
+ # GET /orders.xml
+ def index
+ @orders = Order.paginate :page=>params[:page], :order=> 'created_at desc', :per_page=>10
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @orders }
+ end
+ end
+
+ # GET /orders/1
+ # GET /orders/1.xml
+ def show
+ @order = Order.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @order }
+ end
+ end
+
+ # GET /orders/new
+ # GET /orders/new.xml
+ def new
+ @cart = current_cart
+ if @cart.line_items.empty?
+ redirect_to store_url, :notice => "Your cart is empty"
+ return
+ end
+ @order = Order.new
+ @checkout = true
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @order }
+ end
+ end
+
+ # GET /orders/1/edit
+ def edit
+ @order = Order.find(params[:id])
+ end
+
+ # POST /orders
+ # POST /orders.xml
+ def create
+ @order = Order.new(params[:order])
+ @order.add_line_items_from_cart(current_cart)
+
+ respond_to do |format|
+ if @order.save
+ Cart.destroy(session[:cart_id])
+ session[:cart_id] = nil
+
+ format.html { redirect_to(store_url, :notice => 'Thank your for your Order.') }
+ format.xml { render :xml => @order, :status => :created, :location => @order }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /orders/1
+ # PUT /orders/1.xml
+ def update
+ @order = Order.find(params[:id])
+
+ respond_to do |format|
+ if @order.update_attributes(params[:order])
+ format.html { redirect_to(@order, :notice => 'Order was successfully updated.') }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /orders/1
+ # DELETE /orders/1.xml
+ def destroy
+ @order = Order.find(params[:id])
+ @order.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(orders_url) }
+ format.xml { head :ok }
+ end
+ end
+end
@@ -80,4 +80,12 @@ def destroy
format.xml { head :ok }
end
end
+
+ def who_bought
+ @product = Product.find(params[:id])
+ respond_to do |format|
+ format.atom
+ format.xml { render :xml => @product }
+ end
+ end
end
@@ -0,0 +1,2 @@
+module OrdersHelper
+end
View
@@ -1,4 +1,5 @@
class LineItem < ActiveRecord::Base
+ belongs_to :order
belongs_to :product
belongs_to :cart
@@ -8,11 +9,13 @@ def total_price
def reduce_qty?
qty = quantity - 1
+ puts "Qty #{qty}"
if qty == 0
destroy
return true
else
update_attribute(:quantity , qty )
+ puts "qty #{quantity}"
end
return false
end
View
@@ -0,0 +1,15 @@
+class Order < ActiveRecord::Base
+ has_many :line_items, :dependent => :destroy
+ #PAYMENT_TYPES = ["Check","Credit Card","Purchase Order"]
+ validates :name, :address, :email, :payment_type_id, :presence => true
+ #validates :pay_type, :inclusion => PAYMENT_TYPES
+ validate :payment_type_id, :inclusion => PaymentType.find(:all).map {|p| p.id}
+ belongs_to :payment_types
+
+ def add_line_items_from_cart(cart)
+ cart.line_items.each do |item|
+ item.cart_id = nil
+ line_items << item
+ end
+ end
+end
@@ -0,0 +1,3 @@
+class PaymentType < ActiveRecord::Base
+ has_one :order
+end
View
@@ -10,6 +10,7 @@ class Product < ActiveRecord::Base
}
has_many :line_items
+ has_many :orders, :through => :line_items
before_destroy :ensure_not_referenced_by_any_line_item
@@ -7,5 +7,10 @@
<td class="total_cell"> <%= number_to_currency(cart.total_price) %> </td>
</tr>
</table>
+<% if @checkout -%>
+<%= button_to "Checkout", new_order_path, :method=>:get,:disabled=>true %>
+<% else -%>
+<%= button_to "Checkout", new_order_path, :method=>:get %>
+<% end -%>
<%= button_to "Empty Cart" , cart, :method=>:delete, :confirm=>"Are you sure?",:remote=>true %>
@@ -21,7 +21,7 @@
<% end %>
<% end %>
-<a href="localhost:3000">Home</a> <br />
+<a href="/">Home</a> <br />
<a href="localhost:3000">Questions</a> <br />
<a href="localhost:3000">News</a> <br />
<a href="localhost:3000">Contact</a> <br />
@@ -1,3 +1,4 @@
+page.select("#notice").each{ |notice| notice.hide }
page.replace_html('cart',render(@cart))
page[:cart].visual_effect :blind_down if @cart.total_items==1
page[:current_item].visual_effect :highlight, :startcolor=>"#88ff88",:endcolor=>"#114411"
@@ -1,5 +1,3 @@
<h1>New line_item</h1>
-
<%= render 'form' %>
-
<%= link_to 'Back', line_items_path %>
@@ -0,0 +1,33 @@
+<%= form_for(@order) do |f| %>
+ <% if @order.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>
+
+ <ul>
+ <% @order.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name, :size => 40 %>
+ </div>
+ <div class="field">
+ <%= f.label :address %><br />
+ <%= f.text_area :address, :rows=>3, :cols=>40 %>
+ </div>
+ <div class="field">
+ <%= f.label :email %><br />
+ <%= f.text_field :email, :size => 40 %>
+ </div>
+ <div class="field">
+ <%= f.label :payment_type_id %><br />
+ <%= f.collection_select(:payment_type_id,PaymentType.all,:id,:payment_type) %>
+ </div>
+ <div class="actions">
+ <%= f.submit 'Place Order'%>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing order</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @order %> |
+<%= link_to 'Back', orders_path %>
@@ -0,0 +1,30 @@
+<h1>Listing orders</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Address</th>
+ <th>Email</th>
+ <th>Pay type</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @orders.each do |order| %>
+ <tr>
+ <td><%= order.name %></td>
+ <td><%= order.address %></td>
+ <td><%= order.email %></td>
+ <td><%= order.pay_type %></td>
+ <td><%= link_to 'Show', order %></td>
+ <td><%= link_to 'Edit', edit_order_path(order) %></td>
+ <td><%= link_to 'Destroy', order, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Order', new_order_path %>
+<p> <%= will_paginate @orders %>
@@ -0,0 +1,6 @@
+<div class="depot_form">
+<fieldset>
+<legend>Please enter your details</legend>
+<%= render 'form' %>
+</fieldset>
+</div>
@@ -0,0 +1,25 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Name:</b>
+ <%= @order.name %>
+</p>
+
+<p>
+ <b>Address:</b>
+ <%= @order.address %>
+</p>
+
+<p>
+ <b>Email:</b>
+ <%= @order.email %>
+</p>
+
+<p>
+ <b>Pay type:</b>
+ <%= @order.pay_type %>
+</p>
+
+
+<%= link_to 'Edit', edit_order_path(@order) %> |
+<%= link_to 'Back', orders_path %>
@@ -0,0 +1,41 @@
+atom_feed do |feed|
+ feed.title "Who bought #{@product.title}"
+
+ latest_order = @product.orders.sort_by(&:updated_at).last
+ feed.updated( latest_order && latest_order.updated_at )
+
+ @product.orders.each do |order|
+ feed.entry(order) do |entry|
+ entry.title "Order #{order.id}"
+ entry.summary :type => 'xhtml' do |xhtml|
+ xhtml.p "Shipped to #{order.address}"
+
+ xhtml.table do
+ xhtml.tr do
+ xhtml.th 'Product'
+ xhtml.th 'Quantity'
+ xhtml.th 'Total Price'
+ end
+ order.line_items.each do |item|
+ xhtml.tr do
+ xhtml.td item.product.title
+ xhtml.td item.quantity
+ xhtml.td number_to_currency item.total_price
+ end
+ end
+ xhtml.tr do
+ xhtml.th 'total', :colspan => 2
+ xhtml.th number_to_currency \
+ order.line_items.map(&:total_price).sum
+ end
+ end
+
+ xhtml.p "Paid by #{order.pay_type}"
+ end
+ entry.author do |author|
+ entry.name order.name
+ entry.email order.email
+ end
+ end
+ end
+end
View
@@ -1,11 +1,17 @@
Depot::Application.routes.draw do
+ resources :payment_types
+
+ resources :orders
+
resources :line_items
resources :carts
get "store/index"
- resources :products
+ resources :products do
+ get :who_bought, :on => :member
+ end
root :to => 'store#index', :as => 'store'
@@ -0,0 +1,16 @@
+class CreateOrders < ActiveRecord::Migration
+ def self.up
+ create_table :orders do |t|
+ t.string :name
+ t.text :address
+ t.string :email
+ t.string :pay_type
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :orders
+ end
+end
Oops, something went wrong.

0 comments on commit 258262b

Please sign in to comment.