Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

code thru chapter 12

  • Loading branch information...
commit a524fffedeb3bbdb6b58565a9a76d5a56078d62a 1 parent f94b815
@arttour authored
View
2  Gemfile
@@ -40,3 +40,5 @@ gem 'jquery-rails'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
+
+gem 'will_paginate', '~> 3.0'
View
12 Gemfile.lock
@@ -36,11 +36,11 @@ GEM
coffee-script (2.2.0)
coffee-script-source
execjs
- coffee-script-source (1.3.1)
+ coffee-script-source (1.3.3)
daemons (1.1.8)
erubis (2.7.0)
eventmachine (0.12.10)
- execjs (1.3.2)
+ execjs (1.4.0)
multi_json (~> 1.0)
hike (1.2.1)
i18n (0.6.0)
@@ -48,14 +48,14 @@ GEM
jquery-rails (2.0.2)
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
- json (1.7.1)
+ json (1.7.3)
libv8 (3.3.10.4)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.18)
- multi_json (1.3.4)
+ multi_json (1.3.6)
polyglot (0.3.3)
quiet_assets (1.0.1)
railties (~> 3.1)
@@ -84,7 +84,7 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
- sass (3.1.17)
+ sass (3.1.19)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
@@ -109,6 +109,7 @@ GEM
uglifier (1.2.4)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ will_paginate (3.0.3)
PLATFORMS
ruby
@@ -123,3 +124,4 @@ DEPENDENCIES
therubyracer
thin
uglifier (>= 1.0.3)
+ will_paginate (~> 3.0)
View
1  app/assets/javascripts/application.js
@@ -11,5 +11,6 @@
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
+//= require jquery-ui
//= require jquery_ujs
//= require_tree .
View
3  app/assets/javascripts/store.js.coffee
@@ -1,3 +1,6 @@
# 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/
+$ ->
+ $('.store .entry > img').click ->
+ $(this).parent().find(':submit').click()
View
10 app/controllers/application_controller.rb
@@ -1,3 +1,13 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ private
+
+ def current_cart
+ Cart.find(session[:cart_id])
+ rescue ActiveRecord::RecordNotFound
+ cart = Cart.create
+ session[:cart_id] = cart.id
+ cart
+ end
end
View
9 app/controllers/products_controller.rb
@@ -80,4 +80,13 @@ def destroy
format.json { head :no_content }
end
end
+
+
+ def who_bought
+ @product = Product.find(params[:id])
+ respond_to do |format|
+ format.atom
+ end
+ end
+
end
View
1  app/controllers/store_controller.rb
@@ -1,5 +1,6 @@
class StoreController < ApplicationController
def index
@products = Product.order(:title)
+ @cart = current_cart
end
end
View
6 app/helpers/application_helper.rb
@@ -1,2 +1,8 @@
module ApplicationHelper
+ def hidden_div_if(condition, attributes = {}, &block)
+ if condition
+ attributes["style"] = "display: none"
+ end
+ content_tag("div", attributes, &block)
+ end
end
View
20 app/models/product.rb
@@ -1,5 +1,9 @@
class Product < ActiveRecord::Base
attr_accessible :description, :image_url, :price, :title
+ has_many :line_items
+ has_many :orders, through: :line_items
+ before_destroy :ensure_not_referenced_by_any_line_item
+
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
@@ -7,8 +11,16 @@ class Product < ActiveRecord::Base
with: %r{\.(gif|jpg|png)$}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
- # validates :price, format: {
- # with: %r{\d*\d\d\z},
- # message: 'must be valid dollar format.'
- # }
+
+ private
+
+ # ensure that there are no line items referencing this product
+ def ensure_not_referenced_by_any_line_item
+ if line_items.empty?
+ return true
+ else
+ errors.add(:base, 'Line Items present')
+ return false
+ end
+ end
end
View
7 app/views/layouts/application.html.erb
@@ -13,8 +13,13 @@
</div>
<div id="columns">
<div id="side">
+ <%if @cart %>
+ <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
+ <%= render @cart %>
+ <% end %>
+ <% end %>
<ul>
- <li><a href="http://www....">Home</a></li>
+ <li><a href="/">Home</a></li>
<li><a href="http://www..../faq">Questions</a></li>
<li><a href="http://www..../news">News</a></li>
<li><a href="http://www..../contact">Contact</a></li>
View
4 app/views/store/index.html.erb
@@ -3,16 +3,14 @@
<% end %>
<h1>Your Pragmatic 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">
-<!-- START:currency -->
<span class="price"><%= number_to_currency(product.price) %></span>
-<!-- END:currency -->
+ <%= button_to 'Add to Cart', line_items_path(product_id: product), remote: true %>
</div>
</div>
<% end %>
View
10 config/routes.rb
@@ -1,7 +1,15 @@
Depot::Application.routes.draw do
+ resources :orders
+
+ resources :line_items
+
+ resources :carts
+
get "store/index"
- resources :products
+ resources :products do
+ get :who_bought, on: :member
+ end
# The priority is based upon order of creation:
# first created -> highest priority.
View
25 db/schema.rb
@@ -11,7 +11,30 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120509003109) do
+ActiveRecord::Schema.define(:version => 20120613070002) do
+
+ create_table "carts", :force => true do |t|
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "line_items", :force => true do |t|
+ t.integer "product_id"
+ t.integer "cart_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "quantity", :default => 1
+ t.integer "order_id"
+ end
+
+ create_table "orders", :force => true do |t|
+ t.string "name"
+ t.text "address"
+ t.string "email"
+ t.string "pay_type"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
create_table "products", :force => true do |t|
t.string "title"
View
6 test/functional/store_controller_test.rb
@@ -9,5 +9,11 @@ class StoreControllerTest < ActionController::TestCase
assert_select 'h3', 'Programming Ruby 1.9'
assert_select '.price', /\$[,\d]+\.\d\d/
end
+
+ test "markup needed for store.js.coffee is in place" do
+ get :index
+ assert_select '.store .entry > img', 3
+ assert_select '.entry input[type=submit]', 3
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.