Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial checkin

  • Loading branch information...
commit 9826059636a6b5361925ad1f2f0ddf33b345c0ad 0 parents
@cheezy authored
Showing with 11,872 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +32 −0 Gemfile
  3. +75 −0 Gemfile.lock
  4. +4 −0 README
  5. +7 −0 Rakefile
  6. +6 −0 app/controllers/admin_controller.rb
  7. +87 −0 app/controllers/adoptions_controller.rb
  8. +7 −0 app/controllers/agency_controller.rb
  9. +22 −0 app/controllers/application_controller.rb
  10. +91 −0 app/controllers/carts_controller.rb
  11. +94 −0 app/controllers/orders_controller.rb
  12. +85 −0 app/controllers/puppies_controller.rb
  13. +21 −0 app/controllers/sessions_controller.rb
  14. +90 −0 app/controllers/users_controller.rb
  15. +2 −0  app/helpers/admin_helper.rb
  16. +2 −0  app/helpers/adoptions_helper.rb
  17. +2 −0  app/helpers/agency_helper.rb
  18. +2 −0  app/helpers/application_helper.rb
  19. +2 −0  app/helpers/carts_helper.rb
  20. +2 −0  app/helpers/orders_helper.rb
  21. +2 −0  app/helpers/puppies_helper.rb
  22. +2 −0  app/helpers/sessions_helper.rb
  23. +2 −0  app/helpers/users_helper.rb
  24. +9 −0 app/models/adoption.rb
  25. +18 −0 app/models/cart.rb
  26. +14 −0 app/models/order.rb
  27. +21 −0 app/models/puppy.rb
  28. +48 −0 app/models/user.rb
  29. +4 −0 app/views/admin/index.html.erb
  30. +5 −0 app/views/adoptions/_adoption.html.erb
  31. +25 −0 app/views/adoptions/_form.html.erb
  32. +6 −0 app/views/adoptions/edit.html.erb
  33. +25 −0 app/views/adoptions/index.html.erb
  34. +5 −0 app/views/adoptions/new.html.erb
  35. +15 −0 app/views/adoptions/show.html.erb
  36. +18 −0 app/views/agency/index.html.erb
  37. +17 −0 app/views/carts/_form.html.erb
  38. +6 −0 app/views/carts/edit.html.erb
  39. +21 −0 app/views/carts/index.html.erb
  40. +5 −0 app/views/carts/new.html.erb
  41. +13 −0 app/views/carts/show.html.erb
  42. +38 −0 app/views/layouts/application.html.erb
  43. +34 −0 app/views/orders/_form.html.erb
  44. +6 −0 app/views/orders/edit.html.erb
  45. +30 −0 app/views/orders/index.html.erb
  46. +7 −0 app/views/orders/new.html.erb
  47. +25 −0 app/views/orders/show.html.erb
  48. +41 −0 app/views/puppies/_form.html.erb
  49. +6 −0 app/views/puppies/edit.html.erb
  50. +30 −0 app/views/puppies/index.html.erb
  51. +5 −0 app/views/puppies/new.html.erb
  52. +16 −0 app/views/puppies/show.html.erb
  53. +2 −0  app/views/sessions/create.html.erb
  54. +2 −0  app/views/sessions/destroy.html.erb
  55. +25 −0 app/views/sessions/new.html.erb
  56. +34 −0 app/views/users/_form.html.erb
  57. +6 −0 app/views/users/edit.html.erb
  58. +28 −0 app/views/users/index.html.erb
  59. +5 −0 app/views/users/new.html.erb
  60. +20 −0 app/views/users/show.html.erb
  61. +4 −0 config.ru
  62. +42 −0 config/application.rb
  63. +13 −0 config/boot.rb
  64. +22 −0 config/database.yml
  65. +5 −0 config/environment.rb
  66. +26 −0 config/environments/development.rb
  67. +49 −0 config/environments/production.rb
  68. +35 −0 config/environments/test.rb
  69. +7 −0 config/initializers/backtrace_silencers.rb
  70. +10 −0 config/initializers/inflections.rb
  71. +5 −0 config/initializers/mime_types.rb
  72. +7 −0 config/initializers/secret_token.rb
  73. +8 −0 config/initializers/session_store.rb
  74. +5 −0 config/locales/en.yml
  75. +78 −0 config/routes.rb
  76. +18 −0 db/migrate/20101017133235_create_puppies.rb
  77. +12 −0 db/migrate/20101017213458_create_carts.rb
  78. +14 −0 db/migrate/20101017223008_create_adoptions.rb
  79. +9 −0 db/migrate/20101018100440_add_quantity_to_adoption.rb
  80. +23 −0 db/migrate/20101018110302_combilne_items_in_cart.rb
  81. +16 −0 db/migrate/20101128125809_create_orders.rb
  82. +9 −0 db/migrate/20101128125854_add_order_id_to_adoption.rb
  83. +15 −0 db/migrate/20101128180606_create_users.rb
  84. +57 −0 db/schema.rb
  85. +60 −0 db/seeds.rb
  86. +2 −0  doc/README_FOR_APP
  87. 0  lib/tasks/.gitkeep
  88. +26 −0 public/404.html
  89. +26 −0 public/422.html
  90. +26 −0 public/500.html
  91. 0  public/favicon.ico
  92. BIN  public/images/Brook.jpg
  93. BIN  public/images/Hannah.jpg
  94. BIN  public/images/MaggieMae.jpg
  95. BIN  public/images/Spud.jpg
  96. BIN  public/images/rails.png
  97. +2 −0  public/javascripts/application.js
  98. +965 −0 public/javascripts/controls.js
  99. +974 −0 public/javascripts/dragdrop.js
  100. +1,123 −0 public/javascripts/effects.js
  101. +6,001 −0 public/javascripts/prototype.js
  102. +175 −0 public/javascripts/rails.js
  103. +5 −0 public/robots.txt
  104. 0  public/stylesheets/.gitkeep
  105. +215 −0 public/stylesheets/puppies.css
  106. +56 −0 public/stylesheets/scaffold.css
  107. +6 −0 script/load_orders.rb
  108. +6 −0 script/rails
  109. +9 −0 test/fixtures/adoptions.yml
  110. +11 −0 test/fixtures/carts.yml
  111. +13 −0 test/fixtures/orders.yml
  112. +17 −0 test/fixtures/puppies.yml
  113. +11 −0 test/fixtures/users.yml
  114. +9 −0 test/functional/admin_controller_test.rb
  115. +49 −0 test/functional/adoptions_controller_test.rb
  116. +9 −0 test/functional/agency_controller_test.rb
  117. +49 −0 test/functional/carts_controller_test.rb
  118. +49 −0 test/functional/orders_controller_test.rb
  119. +49 −0 test/functional/puppies_controller_test.rb
  120. +19 −0 test/functional/sessions_controller_test.rb
  121. +49 −0 test/functional/users_controller_test.rb
  122. +9 −0 test/performance/browsing_test.rb
  123. +13 −0 test/test_helper.rb
  124. +8 −0 test/unit/adoption_test.rb
  125. +8 −0 test/unit/cart_test.rb
  126. +4 −0 test/unit/helpers/admin_helper_test.rb
  127. +4 −0 test/unit/helpers/adoptions_helper_test.rb
  128. +4 −0 test/unit/helpers/agency_helper_test.rb
  129. +4 −0 test/unit/helpers/carts_helper_test.rb
  130. +4 −0 test/unit/helpers/orders_helper_test.rb
  131. +4 −0 test/unit/helpers/puppies_helper_test.rb
  132. +4 −0 test/unit/helpers/sessions_helper_test.rb
  133. +4 −0 test/unit/helpers/users_helper_test.rb
  134. +8 −0 test/unit/order_test.rb
  135. +8 −0 test/unit/puppy_test.rb
  136. +8 −0 test/unit/user_test.rb
  137. 0  vendor/plugins/.gitkeep
4 .gitignore
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/**/*
32 Gemfile
@@ -0,0 +1,32 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.0'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'sqlite3-ruby', :require => 'sqlite3'
+
+# Use unicorn as the web server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger
+# gem 'ruby-debug'
+
+# Bundle the extra gems:
+# gem 'bj'
+# gem 'nokogiri'
+# gem 'sqlite3-ruby', :require => 'sqlite3'
+# gem 'aws-s3', :require => 'aws/s3'
+
+gem 'will_paginate', '>=3.0pre'
+
+# Bundle gems for the local environment. Make sure to
+# put test-only gems in this group so their generators
+# and rake tasks are available in development mode:
+# group :development, :test do
+# gem 'webrat'
+# end
75 Gemfile.lock
@@ -0,0 +1,75 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.0)
+ actionpack (= 3.0.0)
+ mail (~> 2.2.5)
+ actionpack (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4.1)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.12)
+ rack-test (~> 0.5.4)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ i18n (~> 0.4.1)
+ activerecord (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ arel (~> 1.0.0)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ activesupport (3.0.0)
+ arel (1.0.1)
+ activesupport (~> 3.0.0)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.4.1)
+ mail (2.2.7)
+ activesupport (>= 2.3.6)
+ mime-types
+ treetop (>= 1.4.5)
+ mime-types (1.16)
+ polyglot (0.3.1)
+ rack (1.2.1)
+ rack-mount (0.6.13)
+ rack (>= 1.0.0)
+ rack-test (0.5.6)
+ rack (>= 1.0)
+ rails (3.0.0)
+ actionmailer (= 3.0.0)
+ actionpack (= 3.0.0)
+ activerecord (= 3.0.0)
+ activeresource (= 3.0.0)
+ activesupport (= 3.0.0)
+ bundler (~> 1.0.0)
+ railties (= 3.0.0)
+ railties (3.0.0)
+ actionpack (= 3.0.0)
+ activesupport (= 3.0.0)
+ rake (>= 0.8.4)
+ thor (~> 0.14.0)
+ rake (0.8.7)
+ sqlite3-ruby (1.3.1)
+ thor (0.14.3)
+ treetop (1.4.8)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.23)
+ will_paginate (3.0.pre2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rails (= 3.0.0)
+ sqlite3-ruby
+ will_paginate (>= 3.0pre)
4 README
@@ -0,0 +1,4 @@
+== Puppies example application
+
+This is an example application that I am developing to use in an ATDD course as well
+as my book.
7 Rakefile
@@ -0,0 +1,7 @@
+# Add your own tasks in files placed in lib/tasks ending in .rake,
+# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
+
+require File.expand_path('../config/application', __FILE__)
+require 'rake'
+
+Puppies::Application.load_tasks
6 app/controllers/admin_controller.rb
@@ -0,0 +1,6 @@
+class AdminController < ApplicationController
+ def index
+ @total_orders = Order.count
+ end
+
+end
87 app/controllers/adoptions_controller.rb
@@ -0,0 +1,87 @@
+class AdoptionsController < ApplicationController
+ skip_before_filter :authorize
+
+ # GET /adoptions
+ # GET /adoptions.xml
+ def index
+ @adoptions = Adoption.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @adoptions }
+ end
+ end
+
+ # GET /adoptions/1
+ # GET /adoptions/1.xml
+ def show
+ @adoption = Adoption.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @adoption }
+ end
+ end
+
+ # GET /adoptions/new
+ # GET /adoptions/new.xml
+ def new
+ @adoption = Adoption.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @adoption }
+ end
+ end
+
+ # GET /adoptions/1/edit
+ def edit
+ @adoption = Adoption.find(params[:id])
+ end
+
+ # POST /adoptions
+ # POST /adoptions.xml
+ def create
+ @cart = current_cart
+ puppy = Puppy.find(params[:puppy_id])
+ @adoption = @cart.add_puppy(puppy.id)
+
+ respond_to do |format|
+ if @adoption.save
+ format.html { redirect_to(@adoption.cart) }
+ format.xml { render :xml => @adoption, :status => :created, :location => @adoption }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @adoption.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /adoptions/1
+ # PUT /adoptions/1.xml
+ def update
+ @adoption = Adoption.find(params[:id])
+
+ respond_to do |format|
+ if @adoption.update_attributes(params[:adoption])
+ format.html { redirect_to(@adoption, :notice => 'Adoption was successfully updated.') }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @adoption.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /adoptions/1
+ # DELETE /adoptions/1.xml
+ def destroy
+ @adoption = Adoption.find(params[:id])
+ @adoption.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(adoptions_url) }
+ format.xml { head :ok }
+ end
+ end
+end
7 app/controllers/agency_controller.rb
@@ -0,0 +1,7 @@
+class AgencyController < ApplicationController
+ skip_before_filter :authorize
+ def index
+ @puppies = Puppy.all
+ end
+
+end
22 app/controllers/application_controller.rb
@@ -0,0 +1,22 @@
+class ApplicationController < ActionController::Base
+ before_filter :authorize
+ 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
+
+ protected
+
+ def authorize
+ unless User.find_by_id(session[:user_id])
+ redirect_to login_url, :notice => "Please log in"
+ end
+ end
+end
91 app/controllers/carts_controller.rb
@@ -0,0 +1,91 @@
+class CartsController < ApplicationController
+ skip_before_filter :authorize, :only => [:show, :create, :update, :destroy]
+
+ # GET /carts
+ # GET /carts.xml
+ def index
+ @carts = Cart.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @carts }
+ end
+ end
+
+ # GET /carts/1
+ # GET /carts/1.xml
+ def show
+ begin
+ @cart = Cart.find(params[:id])
+ rescue ActiveRecord::RecordNotFound
+ logger.error "Attempt to access invalid cart #{params[:id]}"
+ redirect_to agency_url, :notice => 'Invalid cart'
+ else
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @cart }
+ end
+ end
+ end
+
+ # GET /carts/new
+ # GET /carts/new.xml
+ def new
+ @cart = Cart.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @cart }
+ end
+ end
+
+ # GET /carts/1/edit
+ def edit
+ @cart = Cart.find(params[:id])
+ end
+
+ # POST /carts
+ # POST /carts.xml
+ 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.xml { render :xml => @cart, :status => :created, :location => @cart }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @cart.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /carts/1
+ # PUT /carts/1.xml
+ 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.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @cart.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /carts/1
+ # DELETE /carts/1.xml
+ def destroy
+ @cart = Cart.find(params[:id])
+ @cart.destroy
+ session[:cart_id] = nil
+
+ respond_to do |format|
+ format.html { redirect_to(agency_url, :notice => 'Your car is currently empty') }
+ format.xml { head :ok }
+ end
+ end
+end
94 app/controllers/orders_controller.rb
@@ -0,0 +1,94 @@
+class OrdersController < ApplicationController
+ skip_before_filter :authorize, :only => [:new, :create]
+ # GET /orders
+ # GET /orders.xml
+ def index
+ @orders = Order.paginate :page=>params[:page], :order=>'created_at desc',
+ :per_page => 10
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @orders }
+ end
+ end
+
+ # GET /orders/1
+ # GET /orders/1.xml
+ def show
+ @order = Order.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @order }
+ end
+ end
+
+ # GET /orders/new
+ # GET /orders/new.xml
+ def new
+ @cart = current_cart
+ if current_cart.adoptions.empty?
+ redirect_to agency_url, :notice => "Your cart is empty"
+ return
+ end
+
+ @order = Order.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @order }
+ end
+ end
+
+ # GET /orders/1/edit
+ def edit
+ @order = Order.find(params[:id])
+ end
+
+ # POST /orders
+ # POST /orders.xml
+ def create
+ @order = Order.new(params[:order])
+ @order.add_adoptions_from_cart(current_cart)
+
+ respond_to do |format|
+ if @order.save
+ Cart.destroy(session[:cart_id])
+ session[:cart_id] = nil
+ format.html { redirect_to(agency_url, :notice => 'Thank you for your order.') }
+ format.xml { render :xml => @order, :status => :created, :location => @order }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /orders/1
+ # PUT /orders/1.xml
+ def update
+ @order = Order.find(params[:id])
+
+ respond_to do |format|
+ if @order.update_attributes(params[:order])
+ format.html { redirect_to(@order, :notice => 'Order was successfully updated.') }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /orders/1
+ # DELETE /orders/1.xml
+ def destroy
+ @order = Order.find(params[:id])
+ @order.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(orders_url) }
+ format.xml { head :ok }
+ end
+ end
+end
85 app/controllers/puppies_controller.rb
@@ -0,0 +1,85 @@
+class PuppiesController < ApplicationController
+ skip_before_filter :authorize, :only => [:index, :show]
+
+ # GET /puppies
+ # GET /puppies.xml
+ def index
+ @puppies = Puppy.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @puppies }
+ end
+ end
+
+ # GET /puppies/1
+ # GET /puppies/1.xml
+ def show
+ @puppy = Puppy.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @puppy }
+ end
+ end
+
+ # GET /puppies/new
+ # GET /puppies/new.xml
+ def new
+ @puppy = Puppy.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @puppy }
+ end
+ end
+
+ # GET /puppies/1/edit
+ def edit
+ @puppy = Puppy.find(params[:id])
+ end
+
+ # POST /puppies
+ # POST /puppies.xml
+ def create
+ @puppy = Puppy.new(params[:puppy])
+
+ respond_to do |format|
+ if @puppy.save
+ format.html { redirect_to(@puppy, :notice => 'Puppy was successfully created.') }
+ format.xml { render :xml => @puppy, :status => :created, :location => @puppy }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @puppy.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /puppies/1
+ # PUT /puppies/1.xml
+ def update
+ @puppy = Puppy.find(params[:id])
+
+ respond_to do |format|
+ if @puppy.update_attributes(params[:puppy])
+ format.html { redirect_to(@puppy, :notice => 'Puppy was successfully updated.') }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @puppy.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /puppies/1
+ # DELETE /puppies/1.xml
+ def destroy
+ @puppy = Puppy.find(params[:id])
+ @puppy.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(puppies_url) }
+ format.xml { head :ok }
+ end
+ end
+end
21 app/controllers/sessions_controller.rb
@@ -0,0 +1,21 @@
+class SessionsController < ApplicationController
+ skip_before_filter :authorize
+
+ def new
+ end
+
+ def create
+ if user = User.authenticate(params[:name], params[:password])
+ session[:user_id] = user.id
+ redirect_to admin_url
+ else
+ redirect_to login_url, :alert => "Invalid user/password conbination"
+ end
+ end
+
+ def destroy
+ session[:user_id] = nil
+ redirect_to store_url, :notice => "Logged out"
+ end
+
+end
90 app/controllers/users_controller.rb
@@ -0,0 +1,90 @@
+class UsersController < ApplicationController
+ # GET /users
+ # GET /users.xml
+ def index
+ @users = User.order(:name)
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @users }
+ end
+ end
+
+ # GET /users/1
+ # GET /users/1.xml
+ def show
+ @user = User.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @user }
+ end
+ end
+
+ # GET /users/new
+ # GET /users/new.xml
+ def new
+ @user = User.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @user }
+ end
+ end
+
+ # GET /users/1/edit
+ def edit
+ @user = User.find(params[:id])
+ end
+
+ # POST /users
+ # POST /users.xml
+ def create
+ @user = User.new(params[:user])
+
+ respond_to do |format|
+ if @user.save
+ format.html { redirect_to(users_url,
+ :notice => "User #{@user.name} was successfully created.") }
+ format.xml { render :xml => @user, :status => :created, :location => @user }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /users/1
+ # PUT /users/1.xml
+ def update
+ @user = User.find(params[:id])
+
+ respond_to do |format|
+ if @user.update_attributes(params[:user])
+ format.html { redirect_to(users_url,
+ :notice => "User #{@user.name} was successfully updated.") }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /users/1
+ # DELETE /users/1.xml
+ def destroy
+ @user = User.find(params[:id])
+ begin
+ @user.destroy
+ flash[:notice] = "User #{@user.name} deleted"
+ rescue Exception => e
+ flash[:notice] = e.message
+ end
+
+ respond_to do |format|
+ format.html { redirect_to(users_url) }
+ format.xml { head :ok }
+ end
+ end
+end
2  app/helpers/admin_helper.rb
@@ -0,0 +1,2 @@
+module AdminHelper
+end
2  app/helpers/adoptions_helper.rb
@@ -0,0 +1,2 @@
+module AdoptionsHelper
+end
2  app/helpers/agency_helper.rb
@@ -0,0 +1,2 @@
+module AgencyHelper
+end
2  app/helpers/application_helper.rb
@@ -0,0 +1,2 @@
+module ApplicationHelper
+end
2  app/helpers/carts_helper.rb
@@ -0,0 +1,2 @@
+module CartsHelper
+end
2  app/helpers/orders_helper.rb
@@ -0,0 +1,2 @@
+module OrdersHelper
+end
2  app/helpers/puppies_helper.rb
@@ -0,0 +1,2 @@
+module PuppiesHelper
+end
2  app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
2  app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
+module UsersHelper
+end
9 app/models/adoption.rb
@@ -0,0 +1,9 @@
+class Adoption < ActiveRecord::Base
+ belongs_to :order
+ belongs_to :puppy
+ belongs_to :cart
+
+ def total_price
+ puppy.fees * quantity
+ end
+end
18 app/models/cart.rb
@@ -0,0 +1,18 @@
+class Cart < ActiveRecord::Base
+ has_many :adoptions, :dependent => :destroy
+
+ def add_puppy(puppy_id)
+ current_adoption = adoptions.where(:puppy_id => puppy_id).first
+ if current_adoption
+ current_adoption.quantity += 1
+ else
+ current_adoption = Adoption.new(:puppy_id => puppy_id)
+ adoptions << current_adoption
+ end
+ current_adoption
+ end
+
+ def total_price
+ adoptions.to_a.sum { |adoption| adoption.total_price }
+ end
+end
14 app/models/order.rb
@@ -0,0 +1,14 @@
+class Order < ActiveRecord::Base
+ has_many :adoptions, :dependent => :destroy
+ PAYMENT_TYPES = [ 'Check', 'Credit cart', 'Purchase order' ]
+
+ validates :name, :address, :email, :pay_type, :presence => true
+ validates :pay_type, :inclusion => PAYMENT_TYPES
+
+ def add_adoptions_from_cart(cart)
+ cart.adoptions.each do |item|
+ item.cart_id = nil
+ adoptions << item
+ end
+ end
+end
21 app/models/puppy.rb
@@ -0,0 +1,21 @@
+class Puppy < ActiveRecord::Base
+ default_scope :order => 'name'
+ has_many :adoptions
+
+ before_destroy :ensure_not_referenced_by_any_adoption
+
+ validates :name, :breed, :image_url, :presence => true
+ validates :fees, :numericality => {:greater_than_or_equal_to => 0.01}
+ validates :image_url, :format => {
+ :with => %r{\.(gif|jpg|png)}i,
+ :message => 'must be a URL for GIF, JPG, or PNG image.'}
+
+ def ensure_not_referenced_by_any_adoption
+ if adoptions.count.zero?
+ return true
+ else
+ errors[:base] << "Adoption present"
+ return false
+ end
+ end
+end
48 app/models/user.rb
@@ -0,0 +1,48 @@
+class User < ActiveRecord::Base
+ after_destroy :ensure_an_admin_remains
+
+ validates :name, :presence => true, :uniqueness => true
+
+ validates :password, :confirmation => true
+ attr_accessor :password_confirmation
+ attr_reader :password
+
+ validate :password_must_be_present
+
+ def User.authenticate(name, password)
+ if user = find_by_name(name)
+ if user.hashed_password == encrypt_password(password, user.salt)
+ user
+ end
+ end
+ end
+
+ def User.encrypt_password(password, salt)
+ Digest::SHA2.hexdigest(password + 'wibble' + salt)
+ end
+
+ def password=(password)
+ @password = password
+
+ if password.present?
+ generate_salt
+ self.hashed_password = self.class.encrypt_password(password, salt)
+ end
+ end
+
+ private
+
+ def password_must_be_present
+ errors.add(:password, 'Missing password') unless hashed_password.present?
+ end
+
+ def generate_salt
+ self.salt = self.object_id.to_s + rand.to_s
+ end
+
+ def ensure_an_admin_remains
+ if User.count.zero?
+ raise "Can't delete last user"
+ end
+ end
+end
4 app/views/admin/index.html.erb
@@ -0,0 +1,4 @@
+<<h1>Welcome</h1>
+
+It's <%= Time.now %>
+We have <%= pluralize(@total_orders, "order") %>.
5 app/views/adoptions/_adoption.html.erb
@@ -0,0 +1,5 @@
+<tr>
+ <td><%= adoption.puppy.name %>:</td>
+ <td><%= adoption.puppy.gender %> - <%= adoption.puppy.breed %></td>
+ <td class="item_price"><%= number_to_currency(adoption.total_price) %></td>
+</tr>
25 app/views/adoptions/_form.html.erb
@@ -0,0 +1,25 @@
+<%= form_for(@adoption) do |f| %>
+ <% if @adoption.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@adoption.errors.count, "error") %> prohibited this adoption from being saved:</h2>
+
+ <ul>
+ <% @adoption.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :puppy_id %><br />
+ <%= f.text_field :puppy_id %>
+ </div>
+ <div class="field">
+ <%= f.label :cart_id %><br />
+ <%= f.text_field :cart_id %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
6 app/views/adoptions/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing adoption</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @adoption %> |
+<%= link_to 'Back', adoptions_path %>
25 app/views/adoptions/index.html.erb
@@ -0,0 +1,25 @@
+<h1>Listing adoptions</h1>
+
+<table>
+ <tr>
+ <th>Puppy</th>
+ <th>Cart</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @adoptions.each do |adoption| %>
+ <tr>
+ <td><%= adoption.puppy_id %></td>
+ <td><%= adoption.cart_id %></td>
+ <td><%= link_to 'Show', adoption %></td>
+ <td><%= link_to 'Edit', edit_adoption_path(adoption) %></td>
+ <td><%= link_to 'Destroy', adoption, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Adoption', new_adoption_path %>
5 app/views/adoptions/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New adoption</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', adoptions_path %>
15 app/views/adoptions/show.html.erb
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Puppy:</b>
+ <%= @adoption.puppy_id %>
+</p>
+
+<p>
+ <b>Cart:</b>
+ <%= @adoption.cart_id %>
+</p>
+
+
+<%= link_to 'Edit', edit_adoption_path(@adoption) %> |
+<%= link_to 'Back', adoptions_path %>
18 app/views/agency/index.html.erb
@@ -0,0 +1,18 @@
+
+<% if notice -%>
+ <p id='notice'><%= notice %></p>
+<% end %>
+
+<h1>Puppy List</h1>
+
+<% @puppies.each do |puppy| -%>
+ <div class='entry'>
+ <%= image_tag(puppy.image_url) %>
+ <h3><%= puppy.name %></h3>
+ <h4><%= puppy.gender %> <%= puppy.breed %></h4>
+ <div class='fees_line'>
+ <span class="fees"><%= number_to_currency(puppy.fees) %></span>
+ <%= button_to 'View Details', puppy_path(puppy) %>
+ </div>
+ </div>
+<% end %>
17 app/views/carts/_form.html.erb
@@ -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 %>
6 app/views/carts/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing cart</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @cart %> |
+<%= link_to 'Back', carts_path %>
21 app/views/carts/index.html.erb
@@ -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 %>
5 app/views/carts/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New cart</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', carts_path %>
13 app/views/carts/show.html.erb
@@ -0,0 +1,13 @@
+<div class="cart_title">Your Cart</div>
+<table>
+ <%= render(@cart.adoptions) %>
+
+ <tr class="total_line">
+ <td colspan="2">Total</td>
+ <td class="total_cell"><%= number_to_currency(@cart.total_price) %></td>
+ </tr>
+</table>
+
+<%= button_to 'Complete Adoption', new_order_path, :method => :get %>
+<%= button_to 'Adopt Another', agency_path %>
+<%= button_to 'Empty cart', @cart, :method => :delete, :confirm => 'Are you sure?' %>
38 app/views/layouts/application.html.erb
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Cheezy's Puppy Adoption Agency</title>
+ <%= stylesheet_link_tag 'scaffold' %>
+ <%= stylesheet_link_tag 'puppies', :media => 'all' %>
+ <%= javascript_include_tag :defaults %>
+ <%= csrf_meta_tag %>
+</head>
+<body id='agency'>
+ <div id='banner'>
+ <%= @page_title || "Cheezy's Puppy Adoption Agency" %>
+ </div>
+
+ <div id='columns'>
+ <div id='side'>
+ <a href="http://www...">Home</a><br />
+ <a href="http://www...">Questions</a><br />
+ <a href="http://www...">News</a><br />
+ <a href="http://www...">Contact</a><br />
+
+ <% if session[:user_id] -%>
+ <br />
+ <%= link_to "Orders", orders_path %><br />
+ <%= link_to "Puppies", puppies_path %><br />
+ <%= link_to "Users", users_path %><br />
+ <br />
+ <%= button_to 'Logout', logout_path, :method => :delete %>
+ <% end -%>
+ </div>
+ <div id='main'>
+ <%= yield %>
+ <div>
+ </div>
+
+
+</body>
+</html>
34 app/views/orders/_form.html.erb
@@ -0,0 +1,34 @@
+<%= form_for(@order) do |f| %>
+ <% if @order.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>
+
+ <ul>
+ <% @order.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :name %>
+ <%= f.text_field :name, :size => 40 %>
+ </div>
+ <div class="field">
+ <%= f.label :address %>
+ <%= f.text_area :address, :rows => 3, :cols => 40 %>
+ </div>
+ <div class="field">
+ <%= f.label :email %>
+ <%= f.email_field :email, :size => 40 %>
+ </div>
+ <div class="field">
+ <%= f.label :pay_type %>
+ <%= f.select :pay_type, Order::PAYMENT_TYPES,
+ :prompt => 'Select a payment method' %>
+ </div>
+ <div class="actions">
+ <%= f.submit 'Place Order' %>
+ </div>
+<% end %>
6 app/views/orders/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing order</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @order %> |
+<%= link_to 'Back', orders_path %>
30 app/views/orders/index.html.erb
@@ -0,0 +1,30 @@
+<h1>Listing orders</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Address</th>
+ <th>Email</th>
+ <th>Pay type</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @orders.each do |order| %>
+ <tr>
+ <td><%= order.name %></td>
+ <td><%= order.address %></td>
+ <td><%= order.email %></td>
+ <td><%= order.pay_type %></td>
+ <td><%= link_to 'Show', order %></td>
+ <td><%= link_to 'Edit', edit_order_path(order) %></td>
+ <td><%= link_to 'Destroy', order, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Order', new_order_path %>
+<p><%= will_paginate @orders %></p>
7 app/views/orders/new.html.erb
@@ -0,0 +1,7 @@
+<div class="puppy_form">
+ <fieldset>
+ <legend>Please Enter Your Details</legend>
+ <%= render 'form' %>
+ </fieldset>
+</div>
+
25 app/views/orders/show.html.erb
@@ -0,0 +1,25 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Name:</b>
+ <%= @order.name %>
+</p>
+
+<p>
+ <b>Address:</b>
+ <%= @order.address %>
+</p>
+
+<p>
+ <b>Email:</b>
+ <%= @order.email %>
+</p>
+
+<p>
+ <b>Pay type:</b>
+ <%= @order.pay_type %>
+</p>
+
+
+<%= link_to 'Edit', edit_order_path(@order) %> |
+<%= link_to 'Back', orders_path %>
41 app/views/puppies/_form.html.erb
@@ -0,0 +1,41 @@
+<%= form_for(@puppy) do |f| %>
+ <% if @puppy.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@puppy.errors.count, "error") %> prohibited this puppy from being saved:</h2>
+
+ <ul>
+ <% @puppy.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </div>
+ <div class="field">
+ <%= f.label :breed %><br />
+ <%= f.text_field :breed %>
+ </div>
+ <div class="field">
+ <%= f.label :description %><br />
+ <%= f.text_field :description %>
+ </div>
+ <div class="field">
+ <%= f.label :gender %><br />
+ <%= f.text_field :gender %>
+ </div>
+ <div class="field">
+ <%= f.label :image_url %><br />
+ <%= f.text_field :image_url %>
+ </div>
+ <div class="field">
+ <%= f.label :fees %><br />
+ <%= f.text_field :fees %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
6 app/views/puppies/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing puppy</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @puppy %> |
+<%= link_to 'Back', puppies_path %>
30 app/views/puppies/index.html.erb
@@ -0,0 +1,30 @@
+<div id="puppy_list">
+ <h1>Listing puppies</h1>
+
+ <table>
+ <% @puppies.each do |puppy| -%>
+ <tr class="<%= cycle('list_line_odd', 'list_line_even') %>">
+ <td>
+ <%= image_tag(puppy.image_url, :class => 'list_image') %>
+ </td>
+
+ <td class='list_description'>
+ <dl>
+ <dt><%= puppy.name %></dt>
+ <dd><%= truncate(strip_tags(puppy.description), :length => 80) %></dd>
+ </dl>
+ </td>
+
+ <td class='list_actions'>
+ <%= link_to 'Show', puppy %><br />
+ <%= link_to 'Edit', edit_puppy_path(puppy) %><br />
+ <%= link_to 'Destroy', puppy, :confirm => 'Are you sure?', :method => :delete %>
+ </td>
+ </tr>
+ <% end %>
+ </table>
+</div>
+
+<br />
+
+<%= link_to 'New Puppy', new_puppy_path %>
5 app/views/puppies/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New puppy</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', puppies_path %>
16 app/views/puppies/show.html.erb
@@ -0,0 +1,16 @@
+<% if notice -%>
+ <p id='notice'><%= notice %></p>
+<% end %>
+
+<div>
+ <%= image_tag(@puppy.image_url) %>
+ <h2><%= @puppy.name %></h2>
+ <h3><%= @puppy.gender %> <%= @puppy.breed %></h3>
+ <%= sanitize @puppy.description %>
+ <div class='fees_line'>
+ <span class="fees">The fees to adopt me are <%= number_to_currency(@puppy.fees) %></span>
+ <%= button_to 'Adopt Me!', adoptions_path(:puppy_id => @puppy) %>
+ </div>
+</div>
+
+<%= link_to 'Return to List', agency_path %>
2  app/views/sessions/create.html.erb
@@ -0,0 +1,2 @@
+<h1>Sessions#create</h1>
+<p>Find me in app/views/sessions/create.html.erb</p>
2  app/views/sessions/destroy.html.erb
@@ -0,0 +1,2 @@
+<h1>Sessions#destroy</h1>
+<p>Find me in app/views/sessions/destroy.html.erb</p>
25 app/views/sessions/new.html.erb
@@ -0,0 +1,25 @@
+<div class="puppy_form">
+ <% if flash[:alert] -%>
+ <p id="notice"><%= flash[:alert] %></p>
+ <% end -%>
+
+ <%= form_tag do %>
+ <fieldset>
+ <legend>Please Log In</legend>
+
+ <div>
+ <label for="name">Name:</label>
+ <%= text_field_tag :name, params[:name] %>
+ </div>
+
+ <div>
+ <label for="password">Password:</label>
+ <%= password_field_tag :password, params[:password] %>
+ </div>
+
+ <div>
+ <%= submit_tag "Login" %>
+ </div>
+ </fieldset>
+ <% end %>
+</div>
34 app/views/users/_form.html.erb
@@ -0,0 +1,34 @@
+<div class="puppy_form">
+<%= form_for(@user) do |f| %>
+ <% if @user.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@user.errors.count, "error") %>
+ prohibited this user from being saved:</h2>
+ <ul>
+ <% @user.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <fieldset>
+ <legend>Enter User Details</legend>
+ <div class="field">
+ <%= f.label :name %>:
+ <%= f.text_field :name, :size => 40 %>
+ </div>
+ <div class="field">
+ <%= f.label :password, 'Password' %>:
+ <%= f.password_field :password, :size => 40 %>
+ </div>
+ <div class="field">
+ <%= f.label :password_confirmation, 'Confirm' %>:
+ <%= f.password_field :password_confirmation, :size => 40 %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+ </fieldset>
+<% end %>
+</div>
6 app/views/users/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing user</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @user %> |
+<%= link_to 'Back', users_path %>
28 app/views/users/index.html.erb
@@ -0,0 +1,28 @@
+<h1>Listing users</h1>
+<% if notice -%>
+ <p id="notice"><%= notice %></p>
+<% end -%>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Hashed password</th>
+ <th>Salt</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @users.each do |user| %>
+ <tr>
+ <td><%= user.name %></td>
+ <td><%= link_to 'Show', user %></td>
+ <td><%= link_to 'Edit', edit_user_path(user) %></td>
+ <td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New User', new_user_path %>
5 app/views/users/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New user</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', users_path %>
20 app/views/users/show.html.erb
@@ -0,0 +1,20 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Name:</b>
+ <%= @user.name %>
+</p>
+
+<p>
+ <b>Hashed password:</b>
+ <%= @user.hashed_password %>
+</p>
+
+<p>
+ <b>Salt:</b>
+ <%= @user.salt %>
+</p>
+
+
+<%= link_to 'Edit', edit_user_path(@user) %> |
+<%= link_to 'Back', users_path %>
4 config.ru
@@ -0,0 +1,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run Puppies::Application
42 config/application.rb
@@ -0,0 +1,42 @@
+require File.expand_path('../boot', __FILE__)
+
+require 'rails/all'
+
+# If you have a Gemfile, require the gems listed there, including any gems
+# you've limited to :test, :development, or :production.
+Bundler.require(:default, Rails.env) if defined?(Bundler)
+
+module Puppies
+ class Application < Rails::Application
+ # Settings in config/environments/* take precedence over those specified here.
+ # Application configuration should go into files in config/initializers
+ # -- all .rb files in that directory are automatically loaded.
+
+ # Custom directories with classes and modules you want to be autoloadable.
+ # config.autoload_paths += %W(#{config.root}/extras)
+
+ # Only load the plugins named here, in the order given (default is alphabetical).
+ # :all can be used as a placeholder for all plugins not explicitly named.
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
+
+ # Activate observers that should always be running.
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
+ # config.time_zone = 'Central Time (US & Canada)'
+
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
+ # config.i18n.default_locale = :de
+
+ # JavaScript files you want as :defaults (application.js is always included).
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
+
+ # Configure the default encoding used in templates for Ruby 1.9.
+ config.encoding = "utf-8"
+
+ # Configure sensitive parameters which will be filtered from the log file.
+ config.filter_parameters += [:password]
+ end
+end
13 config/boot.rb
@@ -0,0 +1,13 @@
+require 'rubygems'
+
+# Set up gems listed in the Gemfile.
+gemfile = File.expand_path('../../Gemfile', __FILE__)
+begin
+ ENV['BUNDLE_GEMFILE'] = gemfile
+ require 'bundler'
+ Bundler.setup
+rescue Bundler::GemNotFound => e
+ STDERR.puts e.message
+ STDERR.puts "Try running `bundle install`."
+ exit!
+end if File.exist?(gemfile)
22 config/database.yml
@@ -0,0 +1,22 @@
+# SQLite version 3.x
+# gem install sqlite3-ruby (not necessary on OS X Leopard)
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+
+# Warning: The database defined as "test" will be erased and
+# re-generated from your development database when you run "rake".
+# Do not set this db to the same as development or production.
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+ pool: 5
+ timeout: 5000
+
+production:
+ adapter: sqlite3
+ database: db/production.sqlite3
+ pool: 5
+ timeout: 5000
5 config/environment.rb
@@ -0,0 +1,5 @@
+# Load the rails application
+require File.expand_path('../application', __FILE__)
+
+# Initialize the rails application
+Puppies::Application.initialize!
26 config/environments/development.rb
@@ -0,0 +1,26 @@
+Puppies::Application.configure do
+ # Settings specified here will take precedence over those in config/environment.rb
+
+ # In the development environment your application's code is reloaded on
+ # every request. This slows down response time but is perfect for development
+ # since you don't have to restart the webserver when you make code changes.
+ config.cache_classes = false
+
+ # Log error messages when you accidentally call methods on nil.
+ config.whiny_nils = true
+
+ # Show full error reports and disable caching
+ config.consider_all_requests_local = true
+ config.action_view.debug_rjs = true
+ config.action_controller.perform_caching = false
+
+ # Don't care if the mailer can't send
+ config.action_mailer.raise_delivery_errors = false
+
+ # Print deprecation notices to the Rails logger
+ config.active_support.deprecation = :log
+
+ # Only use best-standards-support built into browsers
+ config.action_dispatch.best_standards_support = :builtin
+end
+
49 config/environments/production.rb
@@ -0,0 +1,49 @@
+Puppies::Application.configure do
+ # Settings specified here will take precedence over those in config/environment.rb
+
+ # The production environment is meant for finished, "live" apps.
+ # Code is not reloaded between requests
+ config.cache_classes = true
+
+ # Full error reports are disabled and caching is turned on
+ config.consider_all_requests_local = false
+ config.action_controller.perform_caching = true
+
+ # Specifies the header that your server uses for sending files
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
+
+ # For nginx:
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
+
+ # If you have no front-end server that supports something like X-Sendfile,
+ # just comment this out and Rails will serve the files
+
+ # See everything in the log (default is :info)
+ # config.log_level = :debug
+
+ # Use a different logger for distributed setups
+ # config.logger = SyslogLogger.new
+
+ # Use a different cache store in production
+ # config.cache_store = :mem_cache_store
+
+ # Disable Rails's static asset server
+ # In production, Apache or nginx will already do this
+ config.serve_static_assets = false
+
+ # Enable serving of images, stylesheets, and javascripts from an asset server
+ # config.action_controller.asset_host = "http://assets.example.com"
+
+ # Disable delivery errors, bad email addresses will be ignored
+ # config.action_mailer.raise_delivery_errors = false
+
+ # Enable threaded mode
+ # config.threadsafe!
+
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
+ # the I18n.default_locale when a translation can not be found)
+ config.i18n.fallbacks = true
+
+ # Send deprecation notices to registered listeners
+ config.active_support.deprecation = :notify
+end
35 config/environments/test.rb
@@ -0,0 +1,35 @@
+Puppies::Application.configure do
+ # Settings specified here will take precedence over those in config/environment.rb
+
+ # The test environment is used exclusively to run your application's
+ # test suite. You never need to work with it otherwise. Remember that
+ # your test database is "scratch space" for the test suite and is wiped
+ # and recreated between test runs. Don't rely on the data there!
+ config.cache_classes = true
+
+ # Log error messages when you accidentally call methods on nil.
+ config.whiny_nils = true
+
+ # Show full error reports and disable caching
+ config.consider_all_requests_local = true
+ config.action_controller.perform_caching = false
+
+ # Raise exceptions instead of rendering exception templates
+ config.action_dispatch.show_exceptions = false
+
+ # Disable request forgery protection in test environment
+ config.action_controller.allow_forgery_protection = false
+
+ # Tell Action Mailer not to deliver emails to the real world.
+ # The :test delivery method accumulates sent emails in the
+ # ActionMailer::Base.deliveries array.
+ config.action_mailer.delivery_method = :test
+
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
+ # like if you have constraints or database-specific column types
+ # config.active_record.schema_format = :sql
+
+ # Print deprecation notices to the stderr
+ config.active_support.deprecation = :stderr
+end
7 config/initializers/backtrace_silencers.rb
@@ -0,0 +1,7 @@
+# Be sure to restart your server when you modify this file.
+
+# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
+# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
+
+# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
+# Rails.backtrace_cleaner.remove_silencers!
10 config/initializers/inflections.rb
@@ -0,0 +1,10 @@
+# Be sure to restart your server when you modify this file.
+
+# Add new inflection rules using the following format
+# (all these examples are active by default):
+# ActiveSupport::Inflector.inflections do |inflect|
+# inflect.plural /^(ox)$/i, '\1en'
+# inflect.singular /^(ox)en/i, '\1'
+# inflect.irregular 'person', 'people'
+# inflect.uncountable %w( fish sheep )
+# end
5 config/initializers/mime_types.rb
@@ -0,0 +1,5 @@
+# Be sure to restart your server when you modify this file.
+
+# Add new mime types for use in respond_to blocks:
+# Mime::Type.register "text/richtext", :rtf
+# Mime::Type.register_alias "text/html", :iphone
7 config/initializers/secret_token.rb
@@ -0,0 +1,7 @@
+# Be sure to restart your server when you modify this file.
+
+# Your secret key for verifying the integrity of signed cookies.
+# If you change this key, all old signed cookies will become invalid!
+# Make sure the secret is at least 30 characters and all random,
+# no regular words or you'll be exposed to dictionary attacks.
+Puppies::Application.config.secret_token = 'c91413712a22b3f6d2630e9e7194443e17218d296b2d3a234d79d2c137649fc3c6198f87f0d1c8319b5ac009f57d87e8430f25a507b693620aa238c2b4715577'
8 config/initializers/session_store.rb
@@ -0,0 +1,8 @@
+# Be sure to restart your server when you modify this file.
+
+Puppies::Application.config.session_store :cookie_store, :key => '_puppies_session'
+
+# Use the database for sessions instead of the cookie-based default,
+# which shouldn't be used to store highly confidential information
+# (create the session table with "rake db:sessions:create")
+# Puppies::Application.config.session_store :active_record_store
5 config/locales/en.yml
@@ -0,0 +1,5 @@
+# Sample localization file for English. Add more files in this directory for other locales.
+# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
+
+en:
+ hello: "Hello world"
78 config/routes.rb
@@ -0,0 +1,78 @@
+Puppies::Application.routes.draw do
+ get 'admin' => 'admin#index'
+
+ controller :sessions do
+ get 'login' => :new
+ post 'login' => :create
+ delete 'login' => :destroy
+ end
+
+ resources :users
+
+ resources :orders
+
+ resources :adoptions
+
+ resources :carts
+
+ get "agency/index"
+
+ resources :puppies
+
+ # The priority is based upon order of creation:
+ # first created -> highest priority.
+
+ # Sample of regular route:
+ # match 'products/:id' => 'catalog#view'
+ # Keep in mind you can assign values other than :controller and :action
+
+ # Sample of named route:
+ # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
+ # This route can be invoked with purchase_url(:id => product.id)
+
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
+ # resources :products
+
+ # Sample resource route with options:
+ # resources :products do
+ # member do
+ # get 'short'
+ # post 'toggle'
+ # end
+ #
+ # collection do
+ # get 'sold'
+ # end
+ # end
+
+ # Sample resource route with sub-resources:
+ # resources :products do
+ # resources :comments, :sales
+ # resource :seller
+ # end
+
+ # Sample resource route with more complex sub-resources
+ # resources :products do
+ # resources :comments
+ # resources :sales do
+ # get 'recent', :on => :collection
+ # end
+ # end
+
+ # Sample resource route within a namespace:
+ # namespace :admin do
+ # # Directs /admin/products/* to Admin::ProductsController
+ # # (app/controllers/admin/products_controller.rb)
+ # resources :products
+ # end
+
+ # You can have the root of your site routed with "root"
+ # just remember to delete public/index.html.
+ root :to => "agency#index", :as => 'agency'
+
+ # See how all your routes lay out with "rake routes"
+
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
+ # Note: This route will make all actions in every controller accessible via GET requests.
+ # match ':controller(/:action(/:id(.:format)))'
+end
18 db/migrate/20101017133235_create_puppies.rb
@@ -0,0 +1,18 @@
+class CreatePuppies < ActiveRecord::Migration
+ def self.up
+ create_table :puppies do |t|
+ t.string :name
+ t.string :breed
+ t.string :description
+ t.string :gender
+ t.string :image_url
+ t.decimal :fees, :precision => 8, :scale => 2
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :puppies
+ end
+end
12 db/migrate/20101017213458_create_carts.rb
@@ -0,0 +1,12 @@
+class CreateCarts < ActiveRecord::Migration
+ def self.up
+ create_table :carts do |t|
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :carts
+ end
+end
14 db/migrate/20101017223008_create_adoptions.rb
@@ -0,0 +1,14 @@
+class CreateAdoptions < ActiveRecord::Migration
+ def self.up
+ create_table :adoptions do |t|
+ t.integer :puppy_id
+ t.integer :cart_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :adoptions
+ end
+end
9 db/migrate/20101018100440_add_quantity_to_adoption.rb
@@ -0,0 +1,9 @@
+class AddQuantityToAdoption < ActiveRecord::Migration
+ def self.up
+ add_column :adoptions, :quantity, :integer, :default => 1
+ end
+
+ def self.down
+ remove_column :adoptions, :quantity
+ end
+end
23 db/migrate/20101018110302_combilne_items_in_cart.rb
@@ -0,0 +1,23 @@
+class CombilneItemsInCart < ActiveRecord::Migration
+ def self.up
+ Cart.all.each do |cart|
+ sums = cart.adoptions.group(:puppy_id).sum(:quantity)
+
+ sums.each do |puppy_id, quantity|
+ if quantity > 1
+ cart.adoptions.where(:puppy_id => puppy_id).delete_all
+ cart.adoptions.create(:puppy_id => puppy_id, :quantity => quantity)
+ end
+ end
+ end
+ end
+
+ def self.down
+ Adoptions.where("quantity>1").each do |adoption|
+ adoption.quantity.times do
+ Adoptions.create :cart_id => adoption.cart_id, :puppy_id => adoption.puppy_id, :quantity => 1
+ end
+ adoption.destroy
+ end
+ end
+end
16 db/migrate/20101128125809_create_orders.rb
@@ -0,0 +1,16 @@
+class CreateOrders < ActiveRecord::Migration
+ def self.up
+ create_table :orders do |t|
+ t.string :name
+ t.text :address
+ t.string :email
+ t.string :pay_type
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :orders
+ end
+end
9 db/migrate/20101128125854_add_order_id_to_adoption.rb
@@ -0,0 +1,9 @@
+class AddOrderIdToAdoption < ActiveRecord::Migration
+ def self.up
+ add_column :adoptions, :order_id, :integer
+ end
+
+ def self.down
+ remove_column :adoptions, :order_id
+ end
+end
15 db/migrate/20101128180606_create_users.rb
@@ -0,0 +1,15 @@
+class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table :users do |t|
+ t.string :name
+ t.string :hashed_password
+ t.string :salt
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :users
+ end
+end
57 db/schema.rb
@@ -0,0 +1,57 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20101128180606) do
+
+ create_table "adoptions", :force => true do |t|
+ t.integer "puppy_id"
+ t.integer "cart_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.integer "quantity", :default => 1
+ t.integer "order_id"
+ end
+
+ create_table "carts", :force => true do |t|
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ 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"
+ t.datetime "updated_at"
+ end
+
+ create_table "puppies", :force => true do |t|
+ t.string "name"
+ t.string "breed"
+ t.string "description"
+ t.string "gender"
+ t.string "image_url"
+ t.decimal "fees", :precision => 8, :scale => 2
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "users", :force => true do |t|
+ t.string "name"
+ t.string "hashed_password"
+ t.string "salt"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
60 db/seeds.rb
@@ -0,0 +1,60 @@
+# This file should contain all the record creation needed to seed the database with its default values.
+# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
+#
+# Examples:
+#
+# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
+# Mayor.create(:name => 'Daley', :city => cities.first)
+Puppy.delete_all
+
+Puppy.create(:name => 'Maggie Mae',
+ :breed => 'Border Colie Mix',
+ :description =>
+ %{<p>
+Very sweet and playful. She was dumped in a deserted area in Cleveland, one of our
+volunteers stopped to feed her and she ran after the van as she drove away. Needless
+to say she was crying for help! She was first timid and shy and now she has regained
+her trust in people. Maggie is very gentle and calm.
+</p>},
+ :gender => 'Female',
+ :image_url => '/images/MaggieMae.jpg',
+ :fees => 14.95)
+
+Puppy.create(:name => 'Spud',
+ :breed => 'Beagle/Hound Mix',
+ :description =>
+ %{<p>
+This 2 year old handsome boy is housetrained, and loves riding in the car. He is playful
+and friendly and would make a great addition to your family.
+</p>},
+ :gender => 'Male',
+ :image_url => '/images/Spud.jpg',
+ :fees => 18.75)
+
+Puppy.create(:name => 'Hanna',
+ :breed => 'Labrador Retriever Mix',
+ :description =>
+ %{<p>
+Little Hannah is 3 mos old. She is energetic, loves to play with other dogs and fetch! She
+is a lovable-playful, typical puppy personality. Hannah is a very confident little girl who
+is always ready to go go go! Hannah is working on basic manners with her forster mon, but
+would benefit from puppy obedience school. She is also crate trained. If you are interested
+in adding a new puppy to your family, please contact us to see if you are a good match.
+</p>},
+ :gender => 'Female',
+ :image_url => '/images/Hannah.jpg',
+ :fees => 22.99)
+
+Puppy.create(:name => 'Brook',
+ :breed => 'Golden Retriever',
+ :description =>
+ %{<p>
+This young lady is trying to put her shelter eperience behind her. She\'s only about 7
+months old, and as you can see from her picture, she loves her toys!! Basically a blank
+slate as far as training, she\'ll fit into a new home very quickly.
+</p>},
+ :gender => 'Female',
+ :image_url => '/images/Brook.jpg',
+ :fees => 34.95)
+
+
2  doc/README_FOR_APP
@@ -0,0 +1,2 @@
+Use this README file to introduce your application and point to useful places in the API for learning more.
+Run "rake doc:app" to generate API documentation for your models, controllers, helpers, and libraries.
0  lib/tasks/.gitkeep
No changes.
26 public/404.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>The page you were looking for doesn't exist (404)</title>
+ <style type="text/css">
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
+ div.dialog {
+ width: 25em;
+ padding: 0 4em;
+ margin: 4em auto 0 auto;
+ border: 1px solid #ccc;
+ border-right-color: #999;
+ border-bottom-color: #999;
+ }
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
+ </style>
+</head>
+
+<body>
+ <!-- This file lives in public/404.html -->
+ <div class="dialog">
+ <h1>The page you were looking for doesn't exist.</h1>
+ <p>You may have mistyped the address or the page may have moved.</p>
+ </div>
+</body>
+</html>
26 public/422.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>The change you wanted was rejected (422)</title>
+ <style type="text/css">
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
+ div.dialog {
+ width: 25em;
+ padding: 0 4em;
+ margin: 4em auto 0 auto;
+ border: 1px solid #ccc;
+ border-right-color: #999;