Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Task G:Check Out

  • Loading branch information...
commit 057383b5ec2c92ee93f1e4f6128fa1cba16c94c0 1 parent 4a99aa1
@aaronmix authored
View
10 Gemfile
@@ -1,6 +1,6 @@
source 'http://rubygems.org'
-gem 'rails', '3.1.0'
+gem 'rails', '3.2.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
@@ -11,9 +11,9 @@ gem 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
- gem 'sass-rails', " ~> 3.1.0"
- gem 'coffee-rails', "~> 3.1.0"
- gem 'uglifier'
+ gem 'sass-rails', " ~> 3.2.3"
+ gem 'coffee-rails', "~> 3.2.1"
+ gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
@@ -27,6 +27,8 @@ gem 'jquery-rails'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
+gem 'will_paginate', '~> 3.0'
+
group :test do
# Pretty printed test output
gem 'turn', :require => false
View
105 Gemfile.lock
@@ -1,42 +1,39 @@
GEM
remote: http://rubygems.org/
specs:
- actionmailer (3.1.0)
- actionpack (= 3.1.0)
- mail (~> 2.3.0)
- actionpack (3.1.0)
- activemodel (= 3.1.0)
- activesupport (= 3.1.0)
+ actionmailer (3.2.0)
+ actionpack (= 3.2.0)
+ mail (~> 2.4.0)
+ actionpack (3.2.0)
+ activemodel (= 3.2.0)
+ activesupport (= 3.2.0)
builder (~> 3.0.0)
erubis (~> 2.7.0)
- i18n (~> 0.6)
- rack (~> 1.3.2)
- rack-cache (~> 1.0.3)
- rack-mount (~> 0.8.2)
+ journey (~> 1.0.0)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.1)
rack-test (~> 0.6.1)
- sprockets (~> 2.0.0)
- activemodel (3.1.0)
- activesupport (= 3.1.0)
- bcrypt-ruby (~> 3.0.0)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.0)
+ activesupport (= 3.2.0)
builder (~> 3.0.0)
- i18n (~> 0.6)
- activerecord (3.1.0)
- activemodel (= 3.1.0)
- activesupport (= 3.1.0)
- arel (~> 2.2.1)
+ activerecord (3.2.0)
+ activemodel (= 3.2.0)
+ activesupport (= 3.2.0)
+ arel (~> 3.0.0)
tzinfo (~> 0.3.29)
- activeresource (3.1.0)
- activemodel (= 3.1.0)
- activesupport (= 3.1.0)
- activesupport (3.1.0)
+ activeresource (3.2.0)
+ activemodel (= 3.2.0)
+ activesupport (= 3.2.0)
+ activesupport (3.2.0)
+ i18n (~> 0.6)
multi_json (~> 1.0)
ansi (1.4.2)
- arel (2.2.3)
- bcrypt-ruby (3.0.1)
+ arel (3.0.2)
builder (3.0.0)
- coffee-rails (3.1.1)
+ coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
- railties (~> 3.1.0)
+ railties (~> 3.2.0)
coffee-script (2.2.0)
coffee-script-source
execjs
@@ -46,37 +43,36 @@ GEM
multi_json (~> 1.0)
hike (1.2.1)
i18n (0.6.0)
- jquery-rails (1.0.19)
- railties (~> 3.0)
+ journey (1.0.3)
+ jquery-rails (2.0.0)
+ railties (>= 3.2.0.beta, < 5.0)
thor (~> 0.14)
json (1.6.5)
- mail (2.3.0)
+ mail (2.4.1)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.1.0)
polyglot (0.3.3)
- rack (1.3.6)
- rack-cache (1.0.3)
+ rack (1.4.1)
+ rack-cache (1.1)
rack (>= 0.4)
- rack-mount (0.8.3)
- rack (>= 1.0.0)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
rack (>= 1.0)
- rails (3.1.0)
- actionmailer (= 3.1.0)
- actionpack (= 3.1.0)
- activerecord (= 3.1.0)
- activeresource (= 3.1.0)
- activesupport (= 3.1.0)
+ rails (3.2.0)
+ actionmailer (= 3.2.0)
+ actionpack (= 3.2.0)
+ activerecord (= 3.2.0)
+ activeresource (= 3.2.0)
+ activesupport (= 3.2.0)
bundler (~> 1.0)
- railties (= 3.1.0)
- railties (3.1.0)
- actionpack (= 3.1.0)
- activesupport (= 3.1.0)
+ railties (= 3.2.0)
+ railties (3.2.0)
+ actionpack (= 3.2.0)
+ activesupport (= 3.2.0)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
@@ -85,12 +81,11 @@ GEM
rdoc (3.12)
json (~> 1.4)
sass (3.1.15)
- sass-rails (3.1.5)
- actionpack (~> 3.1.0)
- railties (~> 3.1.0)
- sass (~> 3.1.10)
- tilt (~> 1.3.2)
- sprockets (2.0.3)
+ sass-rails (3.2.4)
+ railties (~> 3.2.0)
+ sass (>= 3.1.10)
+ tilt (~> 1.3)
+ sprockets (2.1.2)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
@@ -106,15 +101,17 @@ GEM
uglifier (1.2.3)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ will_paginate (3.0.3)
PLATFORMS
ruby
DEPENDENCIES
- coffee-rails (~> 3.1.0)
+ coffee-rails (~> 3.2.1)
jquery-rails
- rails (= 3.1.0)
- sass-rails (~> 3.1.0)
+ rails (= 3.2.0)
+ sass-rails (~> 3.2.3)
sqlite3
turn
- uglifier
+ uglifier (>= 1.0.3)
+ will_paginate (~> 3.0)
View
2  app/assets/javascripts/store.js.coffee
@@ -3,5 +3,5 @@
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
$ ->
- $('.store .entry > img').click->
+ $('.store .entry > img').click ->
$(this).parent().find(':submit').click()
View
37 app/assets/stylesheets/application.css.scss
@@ -8,7 +8,7 @@
*= require_tree .
*/
-/* START_HIGHLIGHT */
+
#banner {
background: #9c9;
padding: 10px;
@@ -79,4 +79,37 @@
}
}
}
-/* END_HIGHLIGHT */
+
+.depot_form {
+ fieldset {
+ background: #efe;
+ legend {
+ color: #dfd;
+ background: #141;
+ font-family: sans-serif;
+ padding: 0.2em 1em;
+ }
+ }
+
+ form {
+ label {
+ width: 5em;
+ float: left;
+ text-align: right;
+ padding-top: 0.2em;
+ margin-right: 0.1em;
+ display: block;
+ }
+ select, textarea, input{
+ margin-left: 0.5em
+ }
+
+ .submit{
+ margin-left: 4em;
+ }
+
+ br{
+ display: none;
+ }
+ }
+}
View
14 app/controllers/orders_controller.rb
@@ -2,7 +2,7 @@ class OrdersController < ApplicationController
# GET /orders
# GET /orders.json
def index
- @orders = Order.all
+ @orders = Order.paginate page:params[:page], order: 'created_at desc', per_page: 10
respond_to do |format|
format.html # index.html.erb
@@ -24,6 +24,12 @@ def show
# GET /orders/new
# GET /orders/new.json
def new
+ @cart = current_cart
+ if @cart.line_items.empty?
+ redirect_to store_url, notice: "Your cart is empty"
+ return
+ end
+
@order = Order.new
respond_to do |format|
@@ -41,12 +47,16 @@ def edit
# POST /orders.json
def create
@order = Order.new(params[:order])
+ @order.add_line_items_from_cart(current_cart)
respond_to do |format|
if @order.save
- format.html { redirect_to @order, notice: 'Order was successfully created.' }
+ Cart.destroy(session[:cart_id])
+ session[:cart_id] = nil
+ format.html { redirect_to @order, notice: 'Thank you for your order' }
format.json { render json: @order, status: :created, location: @order }
else
+ @cart = current.cart
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
View
7 app/controllers/products_controller.rb
@@ -93,4 +93,11 @@ def destroy
format.json { head :ok }
end
end
+
+ def who_bought
+ @product = Product.find(params[:id])
+ respond_to do |format|
+ format.atom
+ end
+ end
end
View
1  app/models/line_item.rb
@@ -7,6 +7,7 @@
# Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
#---
class LineItem < ActiveRecord::Base
+ belongs_to :order
belongs_to :product
belongs_to :cart
View
11 app/models/order.rb
@@ -1,2 +1,13 @@
class Order < ActiveRecord::Base
+ PAYMENT_TYPES = ["Check", "Credit Card", "Purchase order"]
+ has_many :line_items, dependent: :destroy
+ validates :name, :address, :email, presence: true
+ validates :pay_type, inclusion: 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
View
2  app/models/product.rb
@@ -8,7 +8,7 @@
#---
class Product < ActiveRecord::Base
has_many :line_items
-
+ has_many :orders, through: :line_items
before_destroy :ensure_not_referenced_by_any_line_item
#...
View
3  app/views/carts/_cart.html.erb
@@ -9,6 +9,7 @@
</tr>
</table>
-<%= button_to 'Empty cart', cart, :method=>:delete,
+<%= button_to 'Checkout', new_order_path, method: :get%>
+<%= button_to 'Empty cart', cart, method: :delete,
:confirm => 'Are you sure?' %>
<% end %>
View
2  app/views/line_items/create.js.erb
@@ -1,3 +1,5 @@
+$("#notice").hide();
+
if($('#cart tr').length == 1) {$('#cart').show('blind', 1000);}
$('#cart').html("<%=j render @cart %>");
View
10 app/views/orders/_form.html.erb
@@ -13,21 +13,21 @@
<div class="field">
<%= f.label :name %><br />
- <%= f.text_field :name %>
+ <%= f.text_field :name, size: 40 %>
</div>
<div class="field">
<%= f.label :address %><br />
- <%= f.text_area :address %>
+ <%= f.text_area :address, rows: 3, cols: 40 %>
</div>
<div class="field">
<%= f.label :email %><br />
- <%= f.text_field :email %>
+ <%= f.email_field :email, size: 40 %>
</div>
<div class="field">
<%= f.label :pay_type %><br />
- <%= f.text_field :pay_type %>
+ <%= f.select :pay_type, Order::PAYMENT_TYPES, prompt: 'Select a payment method' %>
</div>
<div class="actions">
- <%= f.submit %>
+ <%= f.submit 'Place Order'%>
</div>
<% end %>
View
1  app/views/orders/index.html.erb
@@ -27,3 +27,4 @@
<br />
<%= link_to 'New Order', new_order_path %>
+<p><%= will_paginate @orders %></p>
View
11 app/views/orders/new.html.erb
@@ -1,5 +1,6 @@
-<h1>New order</h1>
-
-<%= render 'form' %>
-
-<%= link_to 'Back', orders_path %>
+<div class="depot_form">
+ <fieldset>
+ <legend>Please Enter Your Details</legend>
+ <%= render 'form' %>
+ </fieldset>
+</div>
View
39 app/views/products/who_bought.atom.builder
@@ -0,0 +1,39 @@
+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|
+ author.name order.name
+ author.email order.email
+ end
+ end
+ end
+end
View
4 config/routes.rb
@@ -15,7 +15,9 @@
get "store/index"
- resources :products
+ resources :products do
+ get :who_bought, on: :member
+ end
# ...
# The priority is based upon order of creation:
View
5 script/load_orders.rb
@@ -0,0 +1,5 @@
+Order.transaction do
+ (1..100).each do |i|
+ Order.create(name: "Customer #{i}", address: "#{i} Summer Street", email: "customer-#{i}@google.com", pay_type: "Credit Card")
+ end
+end
View
2  test/fixtures/line_items.yml
@@ -4,10 +4,12 @@ one:
#START_HIGHLIGHT
product: ruby
cart: one
+ order: one
#END_HIGHLIGHT
two:
#START_HIGHLIGHT
product: ruby
cart: one
+ order: one
#END_HIGHLIGHT
View
12 test/fixtures/orders.yml
@@ -1,13 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
one:
- name: MyString
+ name: Aaron Harvey
address: MyText
- email:
- pay_type: MyString
+ email: aaron@google.com
+ pay_type: Check
two:
- name: MyString
+ name: Jessie Han
address: MyText
- email:
- pay_type: MyString
+ email: jessie@apple.com
+ pay_type: Credit Card
View
12 test/functional/orders_controller_test.rb
@@ -11,7 +11,17 @@ class OrdersControllerTest < ActionController::TestCase
assert_not_nil assigns(:orders)
end
+ test "requires item in cart"
+ get :new
+ assert_redirected_to store_path
+ assert_equal flash[:notice], 'Your cart is empty'
+ end
+
test "should get new" do
+ cart = Cart.create
+ session[:cart_id] = cart.id
+ LineItem.create(cart: cart, product: products(:ruby))
+
get :new
assert_response :success
end
@@ -21,7 +31,7 @@ class OrdersControllerTest < ActionController::TestCase
post :create, order: @order.attributes
end
- assert_redirected_to order_path(assigns(:order))
+ assert_redirected_to store_path
end
test "should show order" do
Please sign in to comment.
Something went wrong with that request. Please try again.