Skip to content

Commit

Permalink
add order checkout review
Browse files Browse the repository at this point in the history
  • Loading branch information
trosborn committed Mar 24, 2016
1 parent 30f7c35 commit bffe5b9
Show file tree
Hide file tree
Showing 24 changed files with 111 additions and 33 deletions.
Binary file modified app/.DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions app/controllers/order/cart_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Order::CartController < Order::BaseController
before_action :set_order

def checkout
end

def review
@order.attributes = order_params
end

private
def set_order
@order = User.active_order
end

def order_params
params.require(:order).permit(:location_id)
end
end
6 changes: 2 additions & 4 deletions app/controllers/order/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ def create
@item = Item.new(item_params)
authorize @item
respond_to do |format|
if @item.save && params[:item][:size_selection] != nil
format.html { redirect_to order_home_path, notice: "#{view_context.add_item_to_order @item}" }
elsif @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
if @item.save
format.html { redirect_to order_added_item_path }
else
format.html { render action: 'new' }
end
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/order/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Order::OrdersController < Order::BaseController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :set_item, only: [:add_item]
after_action :verify_authorized, except: [:home, :add_item, :create]
after_action :verify_authorized, except: [:home]

def home
@menu_pizzas = Item.find_by_kind 'Pizza'
Expand All @@ -22,8 +22,16 @@ def add_item
@whole_pie = @item
@toppings = Item.toppings
@order = current_user.orders.find_or_create_by completed: false
authorize @order
end

def added_item
@item = User.active_order.items.last
authorize @item
end

def

def edit
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module FlashNoticeHelper
def add_item_to_order item
module AddedItemHelper
def added_item_to_order item
side_1_topping_names = item.sides.first.toppings.pluck(:name).join(' ').downcase
side_2 = item.sides.try(:second)
if side_2 != nil
Expand Down
6 changes: 5 additions & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Item < ActiveRecord::Base
belongs_to :order
belongs_to :whole_pie, class_name: 'Item', foreign_key: 'whole_pie_id'
has_many :sides, class_name: 'Item', foreign_key: 'whole_pie_id'
has_many :sizes
has_many :item_toppings
has_many :sides, class_name: 'Item', foreign_key: 'whole_pie_id'
has_many :toppings, through: :item_toppings
has_many :inverse_item_toppings, class_name: 'ItemTopping', foreign_key: 'item_id'
has_many :inverse_toppings, through: :inverse_item_toppings, source: :item
Expand All @@ -22,6 +22,10 @@ def self.toppings
where kind: 'Topping'
end

def self.whole_pies
where whole_pie: nil, kind: 'Pizza'
end

def size_selection= val
sizes.build name: Size.find_by_id(val).name
end
Expand Down
1 change: 1 addition & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Location < ActiveRecord::Base
has_one :address, dependent: :destroy
has_many :orders
accepts_nested_attributes_for :address
end
1 change: 1 addition & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Order < ActiveRecord::Base
belongs_to :user
has_many :items
belongs_to :location
accepts_nested_attributes_for :items
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class User < ActiveRecord::Base
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def self.active_order
Order.last
end

def customer?
if admin == false
true
Expand Down
4 changes: 4 additions & 0 deletions app/policies/item_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ def update?
def destroy?
user.customer
end

def added_item?
user.customer?
end
end
7 changes: 7 additions & 0 deletions app/policies/order_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class OrderPolicy < ApplicationPolicy
attr_reader :user, :order

def add_item?
user.customer?
end
end
8 changes: 8 additions & 0 deletions app/views/layouts/_head.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%head
%meta{name: 'viewport', content: 'width=device-width, initial-scale=1', chatset: 'utf-8'}
%title= content_for?(:title) ? yield(:title) : 'Pagliacci Pizza – Seattle Area Pizza Delivery and Catering'
= javascript_include_tag 'vendor/modernizr'
= stylesheet_link_tag :application, media: 'all'
= javascript_include_tag :application
= csrf_meta_tags
= favicon_link_tag 'favicon.ico'
13 changes: 5 additions & 8 deletions app/views/layouts/_order_top_bar.html.haml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#order-top-bar
#logo-subsection
= image_tag 'pag-banner-logo-half-size.png'
#phone-subsection
= link_to '206.727.1717', 'tel:+12067261717'
#login-subsection
- if user_signed_in?
= link_to 'Log out', destroy_user_session_path, method: :delete
- else
= link_to 'Sign in or create an account', new_user_session_path
#numbers
.notice
= notice
.alert
= alert
= link_to 'Sign in or Sign up', new_user_session_path
.notice
= notice
.alert
= alert
.large-12.columns
= yield
9 changes: 1 addition & 8 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
!!! 5
%html{html_attrs}
%head
%meta{name: 'viewport', content: 'width=device-width, initial-scale=1', chatset: 'utf-8'}
%title= content_for?(:title) ? yield(:title) : 'Pagliacci Pizza – Seattle Area Pizza Delivery and Catering'
= javascript_include_tag 'vendor/modernizr'
= stylesheet_link_tag :application, media: 'all'
= javascript_include_tag :application
= csrf_meta_tags
= favicon_link_tag 'favicon.ico'
= render 'layouts/head'
%body
- if user_signed_in?
= link_to 'Log out', destroy_user_session_path, method: :delete
Expand Down
9 changes: 1 addition & 8 deletions app/views/layouts/order.html.haml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
!!! 5
%html{html_attrs}
%head
%meta{name: 'viewport', content: 'width=device-width, initial-scale=1', chatset: 'utf-8'}
%title= content_for?(:title) ? yield(:title) : 'Pagliacci Pizza – Seattle Area Pizza Delivery and Catering'
= javascript_include_tag 'vendor/modernizr'
= stylesheet_link_tag :application, media: 'all'
= javascript_include_tag :application
= csrf_meta_tags
= favicon_link_tag 'favicon.ico'
= render 'layouts/head'
%body
.row#paper
= render 'layouts/order_top_bar'
3 changes: 3 additions & 0 deletions app/views/order/cart/checkout.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= form_tag 'review'
= collection_select :order, :location_id, Location.all, :id, :name, prompt: true
= submit_tag 'Review Order'
4 changes: 4 additions & 0 deletions app/views/order/cart/review.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- @order.items.whole_pies.each do |i|
= "#{i.sizes.first.name} #{i.name}"

= @order.location.name
1 change: 1 addition & 0 deletions app/views/order/orders/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
= form_tag order_items_path
= hidden_field :order, :id, name: 'item[order_id]'
= hidden_field :item, :kind, name: 'item[kind]'
= hidden_field :item, :name, name: 'item[name]'
= collection_select :item, :sizes, @item.sizes, :id, :name, {include_blank: 'Select Size', prompt: 'Select Size'}, name: 'item[size_selection]'
- if @item.sides.size != 0
#side_fields.large-12-columns
Expand Down
4 changes: 4 additions & 0 deletions app/views/order/orders/added_item.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.small-12.columns
= added_item_to_order @item
= link_to 'Continue Shopping', order_home_path
= link_to 'Checkout', order_checkout_path
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
get 'menu', to: 'menu#index'

namespace :order do
get '', to: 'orders#home'
get 'home', to: 'orders#home'
get 'checkout', to: 'cart#checkout'
get 'add_item', to: 'orders#add_item'
get 'added_item', to: 'orders#added_item'
post 'review', to: 'cart#review'
resources :items
end

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160322190624_add_location_to_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLocationToOrder < ActiveRecord::Migration
def change
add_column :orders, :location_id, :integer
end
end
3 changes: 2 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: 20160301214722) do
ActiveRecord::Schema.define(version: 20160322190624) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -67,6 +67,7 @@
t.boolean "completed", default: false
t.integer "user_id"
t.string "special_instructions"
t.integer "location_id"
end

create_table "sizes", force: :cascade do |t|
Expand Down
19 changes: 19 additions & 0 deletions test/features/orders/can_checkout_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'helpers/test_helper'

feature 'customer can complete order' do
scenario 'customer wants to complete order' do
sign_in :customer
visit ''

click_on 'ORDER ONLINE'
click_on 'EXTRA PEPPERONI'
select 'Large', from: 'item_sizes'
click_on 'Add to Order'
click_on 'Checkout'
select 'Lake City Way', from: 'order[location_id]'
click_on 'Review Order'

page.must_have_content 'Lake City Way'
page.must_have_content 'Large Extra Pepperoni'
end
end
File renamed without changes.

0 comments on commit bffe5b9

Please sign in to comment.