Skip to content

Instantly share code, notes, and snippets.

@bhgames
Created June 12, 2010 20:33
Show Gist options
  • Save bhgames/436051 to your computer and use it in GitHub Desktop.
Save bhgames/436051 to your computer and use it in GitHub Desktop.
class LineItemsController < ApplicationController
# GET /line_items
# GET /line_items.xml
def index
@line_items = LineItem.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @line_items }
end
end
# GET /line_items/1
# GET /line_items/1.xml
def show
@line_item = LineItem.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @line_item }
end
end
# GET /line_items/new
# GET /line_items/new.xml
def new
@line_item = LineItem.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @line_item }
end
end
# GET /line_items/1/edit
def edit
@line_item = LineItem.find(params[:id])
end
# POST /line_items
# POST /line_items.xml
def create
@cart = find_or_create_cart
product = Product.find[params[:product_id]] #params is what was passed,
#and we ask for product_id out of that param hash for the find method of the Product
#array.
@line_item = @cart.line_items.build(:product => product)
respond_to do |format|
if @line_item.save #redirect to sends you to cart page, not line_item again!
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.xml
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.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @line_item.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.xml
def destroy
@line_item = LineItem.find(params[:id])
@line_item.destroy
respond_to do |format|
format.html { redirect_to(line_items_url) }
format.xml { head :ok }
end
end
end
require 'test_helper'
class LineItemsControllerTest < ActionController::TestCase
setup do
@line_item = line_items(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:line_items)
end
test "should get new" do
get :new
assert_response :success
end
test "should create line_item" do
assert_difference('LineItem.count') do
post :create, :product_id => products(:ruby).id
end
assert_redirected_to cart_path(LineItem.last.cart_id)
end
test "should show line_item" do
get :show, :id => @line_item.to_param
assert_response :success
end
test "should get edit" do
get :edit, :id => @line_item.to_param
assert_response :success
end
test "should update line_item" do
put :update, :id => @line_item.to_param, :line_item => @line_item.attributes
assert_redirected_to line_item_path(assigns(:line_item))
end
test "should destroy line_item" do
assert_difference('LineItem.count', -1) do
delete :destroy, :id => @line_item.to_param
end
assert_redirected_to line_items_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment