Permalink
Browse files

Create Cart

  • Loading branch information...
1 parent b3deb85 commit c48adefae99736a2a02ea4414c19ddc94f22bbba @lamhaianh committed Oct 12, 2011
Showing with 531 additions and 0 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/store.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/store.css.scss
  7. +83 −0 app/controllers/carts_controller.rb
  8. +85 −0 app/controllers/line_items_controller.rb
  9. +6 −0 app/controllers/store_controller.rb
  10. +2 −0 app/helpers/carts_helper.rb
  11. +2 −0 app/helpers/line_items_helper.rb
  12. +2 −0 app/helpers/store_helper.rb
  13. +3 −0 app/models/cart.rb
  14. +4 −0 app/models/line_item.rb
  15. +17 −0 app/views/carts/_form.html.erb
  16. +6 −0 app/views/carts/edit.html.erb
  17. +21 −0 app/views/carts/index.html.erb
  18. +5 −0 app/views/carts/new.html.erb
  19. +10 −0 app/views/carts/show.html.erb
  20. +25 −0 app/views/line_items/_form.html.erb
  21. +6 −0 app/views/line_items/edit.html.erb
  22. +25 −0 app/views/line_items/index.html.erb
  23. +5 −0 app/views/line_items/new.html.erb
  24. +15 −0 app/views/line_items/show.html.erb
  25. +17 −0 app/views/store/index.html.erb
  26. +8 −0 db/migrate/20111012193358_create_carts.rb
  27. +10 −0 db/migrate/20111012193705_create_line_items.rb
  28. +11 −0 test/fixtures/carts.yml
  29. +9 −0 test/fixtures/line_items.yml
  30. +49 −0 test/functional/carts_controller_test.rb
  31. +49 −0 test/functional/line_items_controller_test.rb
  32. +12 −0 test/functional/store_controller_test.rb
  33. +7 −0 test/unit/cart_test.rb
  34. +4 −0 test/unit/helpers/carts_helper_test.rb
  35. +4 −0 test/unit/helpers/line_items_helper_test.rb
  36. +4 −0 test/unit/helpers/store_helper_test.rb
  37. +7 −0 test/unit/line_item_test.rb
@@ -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/
@@ -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/
@@ -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/
@@ -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/
@@ -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/
@@ -0,0 +1,3 @@
+// Place all the styles related to the store controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,83 @@
+class CartsController < ApplicationController
+ # GET /carts
+ # GET /carts.json
+ def index
+ @carts = Cart.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render :json => @carts }
+ end
+ end
+
+ # GET /carts/1
+ # GET /carts/1.json
+ def show
+ @cart = Cart.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render :json => @cart }
+ end
+ end
+
+ # GET /carts/new
+ # GET /carts/new.json
+ def new
+ @cart = Cart.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render :json => @cart }
+ end
+ end
+
+ # GET /carts/1/edit
+ def edit
+ @cart = Cart.find(params[:id])
+ end
+
+ # POST /carts
+ # POST /carts.json
+ def create
+ @cart = Cart.new(params[:cart])
+
+ respond_to do |format|
+ if @cart.save
+ format.html { redirect_to @cart, :notice => 'Cart was successfully created.' }
+ format.json { render :json => @cart, :status => :created, :location => @cart }
+ else
+ format.html { render :action => "new" }
+ format.json { render :json => @cart.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /carts/1
+ # PUT /carts/1.json
+ def update
+ @cart = Cart.find(params[:id])
+
+ respond_to do |format|
+ if @cart.update_attributes(params[:cart])
+ format.html { redirect_to @cart, :notice => 'Cart was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.json { render :json => @cart.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /carts/1
+ # DELETE /carts/1.json
+ def destroy
+ @cart = Cart.find(params[:id])
+ @cart.destroy
+
+ respond_to do |format|
+ format.html { redirect_to carts_url }
+ format.json { head :ok }
+ end
+ end
+end
@@ -0,0 +1,85 @@
+class LineItemsController < ApplicationController
+ # GET /line_items
+ # GET /line_items.json
+ def index
+ @line_items = LineItem.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render :json => @line_items }
+ end
+ end
+
+ # GET /line_items/1
+ # GET /line_items/1.json
+ def show
+ @line_item = LineItem.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render :json => @line_item }
+ end
+ end
+
+ # GET /line_items/new
+ # GET /line_items/new.json
+ def new
+ @line_item = LineItem.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render :json => @line_item }
+ end
+ end
+
+ # GET /line_items/1/edit
+ def edit
+ @line_item = LineItem.find(params[:id])
+ end
+
+ # POST /line_items
+ # POST /line_items.json
+ def create
+ @cart = current_cart
+ product = Product.find(params[:product_id])
+ @line_item = @cart.line_items.build(:product => product)
+
+ respond_to do |format|
+ if @line_item.save
+ format.html { redirect_to(@line_item.cart, :notice => 'Line item was successfully created.') }
+ format.xml { render :xml => @line_item, :status => :created, :location => @line_item }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @line_item.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /line_items/1
+ # PUT /line_items/1.json
+ def update
+ @line_item = LineItem.find(params[:id])
+
+ respond_to do |format|
+ if @line_item.update_attributes(params[:line_item])
+ format.html { redirect_to @line_item, :notice => 'Line item was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.json { render :json => @line_item.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /line_items/1
+ # DELETE /line_items/1.json
+ def destroy
+ @line_item = LineItem.find(params[:id])
+ @line_item.destroy
+
+ respond_to do |format|
+ format.html { redirect_to line_items_url }
+ format.json { head :ok }
+ end
+ end
+end
@@ -0,0 +1,6 @@
+class StoreController < ApplicationController
+ def index
+ @products = Product.all
+ end
+
+end
@@ -0,0 +1,2 @@
+module CartsHelper
+end
@@ -0,0 +1,2 @@
+module LineItemsHelper
+end
@@ -0,0 +1,2 @@
+module StoreHelper
+end
View
@@ -0,0 +1,3 @@
+class Cart < ActiveRecord::Base
+ has_many :line_items, :dependent => :destroy
+end
View
@@ -0,0 +1,4 @@
+class LineItem < ActiveRecord::Base
+ belongs_to :product
+ belongs_to :cart
+end
@@ -0,0 +1,17 @@
+<%= form_for(@cart) do |f| %>
+ <% if @cart.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@cart.errors.count, "error") %> prohibited this cart from being saved:</h2>
+
+ <ul>
+ <% @cart.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing cart</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @cart %> |
+<%= link_to 'Back', carts_path %>
@@ -0,0 +1,21 @@
+<h1>Listing carts</h1>
+
+<table>
+ <tr>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @carts.each do |cart| %>
+ <tr>
+ <td><%= link_to 'Show', cart %></td>
+ <td><%= link_to 'Edit', edit_cart_path(cart) %></td>
+ <td><%= link_to 'Destroy', cart, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Cart', new_cart_path %>
@@ -0,0 +1,5 @@
+<h1>New cart</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', carts_path %>
@@ -0,0 +1,10 @@
+<p id="notice"><%= notice %></p>
+<h2>Your Cart</h2>
+<ul>
+ <% for item in @cart.line_items %>
+ <li> <%= item.product.title %></li>
+ <% end %>
+</ul>
+
+<%= link_to 'Edit', edit_cart_path(@cart) %> |
+<%= link_to 'Back', carts_path %>
@@ -0,0 +1,25 @@
+<%= form_for(@line_item) do |f| %>
+ <% if @line_item.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@line_item.errors.count, "error") %> prohibited this line_item from being saved:</h2>
+
+ <ul>
+ <% @line_item.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :product_id %><br />
+ <%= f.number_field :product_id %>
+ </div>
+ <div class="field">
+ <%= f.label :cart_id %><br />
+ <%= f.number_field :cart_id %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing line_item</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @line_item %> |
+<%= link_to 'Back', line_items_path %>
@@ -0,0 +1,25 @@
+<h1>Listing line_items</h1>
+
+<table>
+ <tr>
+ <th>Product</th>
+ <th>Cart</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @line_items.each do |line_item| %>
+ <tr>
+ <td><%= line_item.product_id %></td>
+ <td><%= line_item.cart_id %></td>
+ <td><%= link_to 'Show', line_item %></td>
+ <td><%= link_to 'Edit', edit_line_item_path(line_item) %></td>
+ <td><%= link_to 'Destroy', line_item, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Line item', new_line_item_path %>
@@ -0,0 +1,5 @@
+<h1>New line_item</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', line_items_path %>
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Product:</b>
+ <%= @line_item.product_id %>
+</p>
+
+<p>
+ <b>Cart:</b>
+ <%= @line_item.cart_id %>
+</p>
+
+
+<%= link_to 'Edit', edit_line_item_path(@line_item) %> |
+<%= link_to 'Back', line_items_path %>
@@ -0,0 +1,17 @@
+<% if notice %>
+ <p id="notice"> <%= notice %> </p>
+<% end %>
+
+<h1> Catalog </h1>
+
+<% @products.each do |product| %>
+ <div class="entry">
+ <%= image_tag(product.image_url) %>
+ <h3><%= product.title %></h3>
+ <%= sanitize product.description %>
+ <div class="price_line">
+ <span class="price"><%= number_to_currency(product.price) %></span>
+ <%= button_to 'Add to cart', line_items_path(:product_id => product) %>
+ </div>
+ </div>
+<% end %>
@@ -0,0 +1,8 @@
+class CreateCarts < ActiveRecord::Migration
+ def change
+ create_table :carts do |t|
+
+ t.timestamps
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit c48adef

Please sign in to comment.