Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
head fork: zsherman/vinyl
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 29 files changed
  • 0 commit comments
  • 1 contributor
Showing with 199 additions and 25 deletions.
  1. +3 −0  app/assets/javascripts/carts.js.coffee
  2. +3 −0  app/assets/javascripts/line_items.js.coffee
  3. +3 −0  app/assets/javascripts/orders.js.coffee
  4. +3 −0  app/assets/stylesheets/carts.css.scss
  5. +3 −0  app/assets/stylesheets/line_items.css.scss
  6. +3 −0  app/assets/stylesheets/orders.css.scss
  7. +7 −2 app/controllers/application_controller.rb
  8. +5 −0 app/controllers/carts_controller.rb
  9. +8 −0 app/controllers/line_items_controller.rb
  10. +2 −0  app/controllers/orders_controller.rb
  11. +2 −0  app/helpers/carts_helper.rb
  12. +2 −0  app/helpers/line_items_helper.rb
  13. +2 −0  app/helpers/orders_helper.rb
  14. +25 −1 app/models/cart.rb
  15. +9 −1 app/models/line_item.rb
  16. +28 −0 app/views/carts/show.html.erb
  17. +7 −12 app/views/deals/index.html.erb
  18. +8 −2 config/routes.rb
  19. +2 −1  db/migrate/20120519231725_create_carts.rb
  20. +5 −1 db/migrate/20120519231738_create_line_items.rb
  21. +5 −1 db/migrate/20120519232316_create_orders.rb
  22. +1 −1  db/migrate/20120520042954_add_price_and_featured_to_deals.rb
  23. +30 −3 db/schema.rb
  24. +7 −0 test/functional/carts_controller_test.rb
  25. +7 −0 test/functional/line_items_controller_test.rb
  26. +7 −0 test/functional/orders_controller_test.rb
  27. +4 −0 test/unit/helpers/carts_helper_test.rb
  28. +4 −0 test/unit/helpers/line_items_helper_test.rb
  29. +4 −0 test/unit/helpers/orders_helper_test.rb
View
3  app/assets/javascripts/carts.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3  app/assets/javascripts/line_items.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3  app/assets/javascripts/orders.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3  app/assets/stylesheets/carts.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the carts controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
3  app/assets/stylesheets/line_items.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the line_items controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
3  app/assets/stylesheets/orders.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the orders controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
9 app/controllers/application_controller.rb
@@ -1,6 +1,7 @@
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
+ helper_method :current_user
private
@@ -8,8 +9,12 @@ def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
- def test
-
+ def current_cart
+ Cart.find(session[:cart_id])
+ rescue ActiveRecord::RecordNotFound
+ cart = Cart.create
+ session[:cart_id] = cart.id
+ cart
end
end
View
5 app/controllers/carts_controller.rb
@@ -0,0 +1,5 @@
+class CartsController < ApplicationController
+ def show
+ @cart = current_cart
+ end
+end
View
8 app/controllers/line_items_controller.rb
@@ -0,0 +1,8 @@
+class LineItemsController < ApplicationController
+ def create
+ @deal = Deal.find(params[:deal_id])
+ @line_item = LineItem.create!(:cart => current_cart, :deal_id => @deal.id, :quantity => 1, :price => @deal.price)
+ flash[:notice] = "Added #{@deal.album} to cart."
+ redirect_to '/cart'
+ end
+end
View
2  app/controllers/orders_controller.rb
@@ -0,0 +1,2 @@
+class OrdersController < ApplicationController
+end
View
2  app/helpers/carts_helper.rb
@@ -0,0 +1,2 @@
+module CartsHelper
+end
View
2  app/helpers/line_items_helper.rb
@@ -0,0 +1,2 @@
+module LineItemsHelper
+end
View
2  app/helpers/orders_helper.rb
@@ -0,0 +1,2 @@
+module OrdersHelper
+end
View
26 app/models/cart.rb
@@ -1,3 +1,27 @@
class Cart < ActiveRecord::Base
- # attr_accessible :title, :body
+ has_many :line_items
+
+ def total_price
+ # convert to array so it doesn't try to do sum on database directly
+ line_items.to_a.sum(&:full_price)
+ end
+
+ def paypal_url(return_url)
+ values = {
+ :business => 'seller_1229899173_biz@railscasts.com',
+ :cmd => '_cart',
+ :upload => 1,
+ :return => return_url,
+ :invoice => id
+ }
+ line_items.each_with_index do |item, index|
+ values.merge!({
+ "amount_#{index+1}" => item.unit_price,
+ "item_name_#{index+1}" => item.product.name,
+ "item_number_#{index+1}" => item.id,
+ "quantity_#{index+1}" => item.quantity
+ })
+ end
+ "https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
+ end
end
View
10 app/models/line_item.rb
@@ -1,3 +1,11 @@
class LineItem < ActiveRecord::Base
- # attr_accessible :title, :body
+
+ attr_accessible :cart, :deal_id, :quantity, :price
+
+ belongs_to :cart
+ belongs_to :deal
+
+ def full_price
+ price * quantity
+ end
end
View
28 app/views/carts/show.html.erb
@@ -0,0 +1,28 @@
+<%= "Shopping Cart" %>
+
+<table id="line_items">
+ <tr>
+ <th>Product</th>
+ <th>Qty</th>
+ <th class="price">Unit Price</th>
+ <th class="price">Full Price</th>
+ </tr>
+ <% for line_item in @cart.line_items %>
+ <tr class="<%= cycle :odd, :even %>">
+ <td><%=h line_item.deal.album %></td>
+ <td class="qty"><%= line_item.quantity %></td>
+ <td class="price"><%= number_to_currency(line_item.price) %></td>
+ <td class="price"><%= number_to_currency(line_item.full_price) %></td>
+ </tr>
+ <% end %>
+ <tr>
+ <td class="total price" colspan="4">
+ Total: <%= number_to_currency @cart.total_price %>
+ </td>
+ </tr>
+</table>
+
+<p>
+ <%= link_to "Continue Shopping", deals_path %> |
+ <% #link_to "Checkout", @cart.paypal_url(deals_path) %>
+</p>
View
19 app/views/deals/index.html.erb
@@ -1,17 +1,12 @@
Featured deals:
-<%=
-@featured_deals.each do |deal|
- deal
-end
+<% @featured_deals.each do |deal| %>
+ <%= deal.album %>
+ <%= link_to "Add to Cart", line_items_path(:deal_id => deal.id), :method => :post %>
+<% end %>
-%>
<br><br>
All other deals:
-<%=
-
-@other_deals.each do |deal|
- deal
-end
-
-%>
+<% @other_deals.each do |deal| %>
+ <%= deal.album %>
+<% end %>
View
10 config/routes.rb
@@ -15,8 +15,14 @@
resources :deals
- get "users/new"
-
+ resources :line_items
+
+ resources :carts
+
+ resources :orders
+
+ match 'cart' => 'carts#show', :as => :cart
+
# The priority is based upon order of creation:
# first created -> highest priority.
View
3  db/migrate/20120519231725_create_carts.rb
@@ -1,7 +1,8 @@
class CreateCarts < ActiveRecord::Migration
def change
create_table :carts do |t|
-
+ t.integer :user_id
+ t.datetime :purchased_at
t.timestamps
end
end
View
6 db/migrate/20120519231738_create_line_items.rb
@@ -1,7 +1,11 @@
class CreateLineItems < ActiveRecord::Migration
def change
create_table :line_items do |t|
-
+ t.integer :deal_id
+ t.integer :cart_id
+ t.integer :quantity, :default => 1
+ t.decimal :price, :precision => 8, :scale => 2, :default => 0.0
+ t.integer :order_id
t.timestamps
end
end
View
6 db/migrate/20120519232316_create_orders.rb
@@ -1,7 +1,11 @@
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
-
+ t.string :name
+ t.text :address
+ t.string :email
+ t.string :pay_type
+ t.integer :user_id
t.timestamps
end
end
View
2  db/migrate/20120520042954_add_price_and_featured_to_deals.rb
@@ -1,6 +1,6 @@
class AddPriceAndFeaturedToDeals < ActiveRecord::Migration
def change
- add_column :deals, :price, :float
+ add_column :deals, :price, :decimal, :precision => 8, :scale => 2, :default => 0.0
add_column :deals, :featured, :boolean
end
end
View
33 db/schema.rb
@@ -13,6 +13,13 @@
ActiveRecord::Schema.define(:version => 20120520042954) do
+ create_table "carts", :force => true do |t|
+ t.integer "user_id"
+ t.datetime "purchased_at"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "deals", :force => true do |t|
t.string "artist"
t.string "album"
@@ -22,12 +29,32 @@
t.text "album_info"
t.text "label_info"
t.integer "copies"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
- t.float "price"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.decimal "price", :precision => 8, :scale => 2, :default => 0.0
t.boolean "featured"
end
+ create_table "line_items", :force => true do |t|
+ t.integer "deal_id"
+ t.integer "cart_id"
+ t.integer "quantity", :default => 1
+ t.decimal "price", :precision => 8, :scale => 2, :default => 0.0
+ t.integer "order_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "orders", :force => true do |t|
+ t.string "name"
+ t.text "address"
+ t.string "email"
+ t.string "pay_type"
+ t.integer "user_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "users", :force => true do |t|
t.string "email"
t.string "password_hash"
View
7 test/functional/carts_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CartsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
7 test/functional/line_items_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class LineItemsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
7 test/functional/orders_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class OrdersControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/carts_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CartsHelperTest < ActionView::TestCase
+end
View
4 test/unit/helpers/line_items_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class LineItemsHelperTest < ActionView::TestCase
+end
View
4 test/unit/helpers/orders_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class OrdersHelperTest < ActionView::TestCase
+end

No commit comments for this range

Something went wrong with that request. Please try again.