Skip to content

Instantly share code, notes, and snippets.

@fidel
Last active December 12, 2015 08:29
Show Gist options
  • Save fidel/4744938 to your computer and use it in GitHub Desktop.
Save fidel/4744938 to your computer and use it in GitHub Desktop.
Medżik js
<!-- app/views/line_items/decrement.js.erb -->
$('#cart').html("<%= escape_javascript(render(@cart)) %>");
$('#current_item').css({'background-color':'#88ff88'}).animate({'background-color':'#114411'}, 1000);
if ($('#cart tr').length==1) {
// Hide the cart
$('#cart').hide('blind', 1000);
}
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
belongs_to :cart
attr_accessible :cart_id, :product_id
def total_price
product.price * quantity
end
def decrement_quantity
if self.quantity > 1
self.update_attribute(:quantity, self.quantity -=1)
else
self.destroy
end
self
end
end
# app/controllers/line_items_controller.rb
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.add_product(product.id)
@line_item.product = product
respond_to do |format|
if @line_item.save
format.html { redirect_to store_url }
format.js { @current_item = @line_item }
format.json { render json: @line_item,
status: :created, location: @line_item }
else
format.html { render action: "new" }
format.json { render json: @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 :no_content }
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 :no_content }
end
end
def decrement
@line_item = LineItem.find(params[:id])
@cart = @line_item.cart
respond_to do |format|
if @line_item.decrement_quantity
format.html { redirect_to store_path, notice: 'Line item was successfully updated.' }
format.js { @current_item = @line_item }
end
end
end
end
resources :line_items do
member do
put :decrement
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment