Reworking checkout to use f_form_for, show validation, better controller layout

This commit is contained in:
Will Marshall
2014-02-21 14:31:03 +11:00
parent adb58bc059
commit b6b5c2889e
8 changed files with 75 additions and 39 deletions

View File

@@ -51,6 +51,7 @@ group :assets do
gem 'turbo-sprockets-rails3'
gem 'zurb-foundation', :github => 'zurb/foundation'
end
gem 'foundation_rails_helper', github: 'willrjmarshall/foundation_rails_helper', branch: "rails3"
gem 'jquery-rails'

View File

@@ -93,6 +93,16 @@ GIT
i18n (~> 0.5)
spree (~> 1.1)
GIT
remote: git://github.com/willrjmarshall/foundation_rails_helper.git
revision: 61ca053cea97c611dfd546bf7fcd846b98abd401
branch: rails3
specs:
foundation_rails_helper (0.4)
actionpack (>= 3.0)
activemodel (>= 3.0)
railties (>= 3.0)
GIT
remote: git://github.com/zurb/foundation.git
revision: a81d639847ba5fc2e324b749b8e409e31e8d83c9
@@ -292,7 +302,7 @@ GEM
httparty (0.11.0)
multi_json (~> 1.0)
multi_xml (>= 0.5.2)
i18n (0.6.5)
i18n (0.6.9)
journey (1.0.4)
jquery-rails (2.2.2)
railties (>= 3.0, < 5.0)
@@ -325,7 +335,7 @@ GEM
money (5.0.0)
i18n (~> 0.4)
json
multi_json (1.8.2)
multi_json (1.8.4)
multi_xml (0.5.5)
net-scp (1.1.2)
net-ssh (>= 2.6.5)
@@ -383,7 +393,7 @@ GEM
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
raindrops (0.9.0)
rake (10.1.0)
rake (10.1.1)
ransack (0.7.2)
actionpack (~> 3.0)
activerecord (~> 3.0)
@@ -512,6 +522,7 @@ DEPENDENCIES
eaterprises_feature!
factory_girl_rails
faker
foundation_rails_helper!
geocoder
gmaps4rails
guard

View File

@@ -1,37 +1,70 @@
class Shop::CheckoutController < BaseController
class Shop::CheckoutController < Spree::CheckoutController
layout 'darkswarm'
before_filter :set_distributor
before_filter :require_order_cycle
before_filter :require_line_items
prepend_before_filter :require_order_cycle
prepend_before_filter :require_distributor_chosen
skip_before_filter :check_registration
include EnterprisesHelper
def edit
current_order.bill_address ||= Spree::Address.new
end
def update
if @order.update_attributes(params[:order])
fire_event('spree.checkout.update')
while @order.state != "complete"
if @order.next
state_callback(:after)
else
flash[:error] = t(:payment_processing_failed)
respond_with @order, location: shop_checkout_path
break
end
end
if @order.state == "complete" || @order.completed?
flash.notice = t(:order_processed_successfully)
flash[:commerce_tracking] = "nothing special"
respond_with(@order, :location => order_conf)
else
respond_with @order, location: shop_checkout_path
end
else
respond_with @order, location: shop_checkout_path
end
end
private
def skip_state_validation?
true
end
def set_distributor
unless @distributor = current_distributor
redirect_to root_path
redirect_to main_app.root_path
end
end
def require_order_cycle
unless current_order_cycle
redirect_to shop_path
redirect_to main_app.shop_path
end
end
# Y U LOOK AT CART? CART IS EMPTY!
# NO CAN HAZ!
def require_line_items
if current_order.line_items.empty?
redirect_to shop_path
end
def load_order
@order = current_order
redirect_to main_app.shop_path and return unless @order and @order.checkout_allowed?
raise_insufficient_quantity and return if @order.insufficient_stock_lines.present?
redirect_to main_app.shop_path and return if @order.completed?
before_address
state_callback(:before)
end
# Overriding Spree's methods
def raise_insufficient_quantity
flash[:error] = t(:spree_inventory_error_flash_for_insufficient_quantity)
redirect_to main_app.shop_path
end
end

View File

@@ -1,4 +1,6 @@
module ApplicationHelper
include FoundationRailsHelper::FlashHelper
def home_page_cms_content
if controller.controller_name == 'home' && controller.action_name == 'index'
cms_page_content(:content, Cms::Page.find_by_full_path('/'))

View File

@@ -14,6 +14,7 @@
%body.off-canvas
= render partial: "shared/menu"
= display_flash_messages
%section{ role: "main" }
= yield

View File

@@ -1,24 +1,20 @@
%checkout{"ng-controller" => "CheckoutCtrl"}
= form_for current_order, url: shop_update_checkout_path, html: {name: "checkout", id: "checkout_form"} do |f|
= f_form_for current_order, url: main_app.shop_update_checkout_path, html: {name: "checkout", id: "checkout_form"} do |f|
{{ checkout.$valid }}
.large-12.columns
%fieldset#details
%legend Customer Details
.row
.large-6.columns
= f.label :email, "Email Address"
= f.text_field :email
= f.fields_for current_order.bill_address do |ba|
.large-6.columns
= ba.label :phone
= ba.text_field :phone
= f.fields_for current_order.bill_address do |ba|
.row
.large-6.columns
= ba.label :firstname
= ba.text_field :firstname
.large-6.columns
= ba.label :lastname
= ba.text_field :lastname
%fieldset
@@ -26,22 +22,17 @@
= f.fields_for current_order.bill_address do |ba|
.row
.large-12.columns
= ba.label :address1
= ba.text_field :address1
.row
.large-12.columns
= ba.label :address2
= ba.text_field :address2
.row
.large-6.columns
= ba.label :city
= ba.text_field :city
.large-6.columns
= ba.label :country
= ba.text_field :country
.row
.large-6.columns.right
= ba.label :zipcode
= ba.text_field :zipcode
%fieldset#shipping
@@ -61,30 +52,23 @@
#ship_address{"ng-show" => "require_ship_address"}
.row
.large-12.columns
= sa.label :address1
= sa.text_field :address1
.row
.large-12.columns
= sa.label :address2
= sa.text_field :address2
.row
.large-6.columns
= sa.label :city
= sa.text_field :city
.large-6.columns
= sa.label :country
= sa.text_field :country
.row
.large-6.columns.right
= sa.label :zipcode
= sa.text_field :zipcode
.row
.large-6.columns
= sa.label :firstname
= sa.text_field :firstname
.large-6.columns
= sa.label :lastname
= sa.text_field :lastname
%fieldset#payment
%legend Payment Details

View File

@@ -4,6 +4,10 @@ describe Shop::CheckoutController do
let(:distributor) { double(:distributor) }
let(:order_cycle) { create(:order_cycle) }
let(:order) { create(:order) }
before do
order.stub(:checkout_allowed?).and_return true
controller.stub(:check_authorization).and_return true
end
it "redirects home when no distributor is selected" do
get :edit
response.should redirect_to root_path
@@ -19,7 +23,7 @@ describe Shop::CheckoutController do
controller.stub(:current_distributor).and_return(distributor)
controller.stub(:current_order_cycle).and_return(order_cycle)
controller.stub(:current_order).and_return(order)
order.stub_chain(:line_items, :empty?).and_return true
order.stub_chain(:insufficient_stock_lines, :present?).and_return true
get :edit
response.should redirect_to shop_path
end
@@ -28,8 +32,7 @@ describe Shop::CheckoutController do
controller.stub(:current_distributor).and_return(distributor)
controller.stub(:current_order_cycle).and_return(order_cycle)
controller.stub(:current_order).and_return(order)
order.stub_chain(:line_items, :empty?).and_return false
order.stub_chain(:insufficient_stock_lines, :present?).and_return false
get :edit
response.should be_success
end

View File

@@ -158,11 +158,12 @@ feature "As a consumer I want to check out my cart", js: true do
page.should have_content pm2.name
end
describe "Purchase" do
describe "Purchasing" do
it "re-renders with errors when we submit the incomplete form" do
click_button "Purchase"
current_path.should == "/shop/checkout"
page.should have_content "We could not process your order"
save_and_open_page
page.should have_content "can't be blank"
end
end
end