Skip to content

Commit

Permalink
Ajax functionality for shopping cart added
Browse files Browse the repository at this point in the history
  • Loading branch information
chugh97 committed Nov 22, 2013
1 parent 8204812 commit 64356a6
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 193 deletions.
406 changes: 218 additions & 188 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ gem 'coffee-rails', '~> 4.0.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'


# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

Expand Down
23 changes: 23 additions & 0 deletions app/assets/javascripts/cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Created with JetBrains RubyMine.
* User: shaleenchugh
* Date: 22/11/13
* Time: 22:24
* To change this template use File | Settings | File Templates.
*/
$(document).ready(function () {
$.ajax({
url: "/carts/show",
async: true,
contentType: "application/json",
success: function(data) {
var rows = data;
if (rows && rows !== undefined){
$('#shopping_cart').append('<tr><td>Product name</td><td>quantity</td><td>Price</td></tr>');
for (var i=0;i< rows.length;i++){
$('#shopping_cart').append('<tr><td>'+ rows[i].product_id +'</td><td>'+ rows[i].quantity +'</td><td>£'+ rows[i].unit_price +'</td></tr>');
}
}
}
})
});
18 changes: 17 additions & 1 deletion app/controllers/carts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ def new

end

def add
#if !Cart.find_by(:session_id => session[:session_id])
product = Product.find_by(:id => params[:product][:id])
product_price = product.get_product_price.first
Cart.create!(:session_id => session[:session_id], :product_id => params[:product][:id], :quantity => params[:product][:quantity], :unit_price => product_price[:price])


#render json: @cart
#redirect_to products_path(@cart, format: :json)
# return
#end
redirect_to products_path

end

def index

end

def show

@cart = Cart.where(:session_id => session[:session_id])
render json: @cart
end

def create
Expand Down
6 changes: 5 additions & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Product < ActiveRecord::Base
belongs_to :category

has_many :product_prices
validates :category_id, :presence => true

def get_product_price
product_prices.where("effective_start_date >= ?", Date.today)
end

end
3 changes: 3 additions & 0 deletions app/models/product_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ProductPrice < ActiveRecord::Base
has_many :products
end
Empty file added app/views/carts/_cart.html.erb
Empty file.
1 change: 0 additions & 1 deletion app/views/carts/show.html.erb

This file was deleted.

10 changes: 9 additions & 1 deletion app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
TEST PRODUCTS
<div id="cart">
<table id="shopping_cart"></table>
</div>

<%@products.each do |product|%>
<%=form_tag('/carts/add', method: "post") do%>
<h1><%=product.category.name%></h1>
<h2><%=product.name%></h2>
<div>Quanity to buy <%=text_field_tag("#{product.id}")%></div>
<%= hidden_field(:product,:id, :value => product.id)%>
<div>Quanity to buy <%=text_field(:product, :quantity)%></div>
<%= submit_tag("Add to Cart") %>
<% end %>
<%end%>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):

post 'carts/add' => 'carts#add'

resources :carts do

resources :products
end

Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20131122205931_product_prices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ProductPrices < ActiveRecord::Migration
def change
create_table :product_prices do |t|
t.integer :product_id
t.decimal :price
t.datetime :effective_start_date
end
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131120212846) do
ActiveRecord::Schema.define(version: 20131122205931) do

create_table "carts", force: true do |t|
t.string "session_id"
Expand All @@ -36,6 +36,12 @@
t.datetime "updated_at"
end

create_table "product_prices", force: true do |t|
t.integer "product_id"
t.decimal "price", precision: 10, scale: 0
t.datetime "effective_start_date"
end

create_table "products", force: true do |t|
t.string "name"
t.text "description"
Expand Down

0 comments on commit 64356a6

Please sign in to comment.