Adding some SUPER clever magic and fixing some regression issues

This commit is contained in:
Will Marshall
2014-04-07 18:23:37 +10:00
parent 577c91aca5
commit fbcf06f5f5
7 changed files with 109 additions and 81 deletions

View File

@@ -1,32 +1,10 @@
Darkswarm.controller "CheckoutCtrl", ($scope, $rootScope, order) ->
Darkswarm.controller "CheckoutCtrl", ($scope, $rootScope, Order) ->
$scope.require_ship_address = false
$scope.order = order
$scope.initialize = ->
# Our shipping_methods comes through as a hash like so: {id: requires_shipping_address}
# Here we default to the first shipping method if none is selected
$scope.order.shipping_method_id ||= Object.keys(order.shipping_methods)[0]
$scope.order.ship_address_same_as_billing = true if $scope.order.ship_address_same_as_billing == null
$scope.shippingMethodChanged()
$scope.shippingPrice = ->
$scope.shippingMethod().price
$scope.cartTotal = ->
$scope.shippingPrice() + $scope.order.display_total
$scope.shippingMethod = ->
$scope.order.shipping_methods[$scope.order.shipping_method_id]
$scope.paymentMethod = ->
$scope.order.payment_methods[$scope.order.payment_method_id]
$scope.order = $scope.Order = Order
$scope.shippingMethodChanged = ->
$scope.require_ship_address = $scope.shippingMethod().require_ship_address if $scope.shippingMethod()
Order.shippingMethodChanged()
$scope.purchase = (event)->
event.preventDefault()
checkout.submit()
$scope.initialize()

View File

@@ -0,0 +1,32 @@
Darkswarm.factory 'Order', ($resource, Product, order)->
## I am being clever here
## order is a JSON object generated in shop/checkout/order.rabl
## We're extending this to add methods while retaining the data!
new class Order
constructor: ->
@[name] = method for name, method of order # Clone all data from the order JSON object
# Our shipping_methods comes through as a hash like so: {id: requires_shipping_address}
# Here we default to the first shipping method if none is selected
@shipping_method_id ||= Object.keys(@shipping_methods)[0]
@ship_address_same_as_billing = true if @ship_address_same_as_billing == null
@shippingMethodChanged()
shippingMethod: ->
@shipping_methods[@shipping_method_id]
shippingMethodChanged: =>
@require_ship_address = @shippingMethod().require_ship_address if @shippingMethod()
shippingPrice: ->
@shippingMethod().price
paymentMethod: ->
@payment_methods[@payment_method_id]
cartTotal: ->
@shippingPrice() + @display_total

View File

@@ -5,7 +5,7 @@
.large-6.columns
Payment Details
.large-6.columns.text-right
{{ paymentMethod().name }}
{{ Order.paymentMethod().name }}
- current_order.available_payment_methods.each do |method|
.row
.large-12.columns

View File

@@ -5,7 +5,7 @@
.large-6.columns
Shipping
.large-6.columns.text-right
{{ shippingMethod().name }}
{{ Order.shippingMethod().name }}
- for ship_method, i in current_distributor.shipping_methods.uniq
.row
.large-12.columns
@@ -15,18 +15,18 @@
-#"ng-model" => "order.shipping_method_id"
%label
= radio_button_tag "order[shipping_method_id]", ship_method.id, false,
"ng-change" => "shippingMethodChanged()",
"ng-change" => "order.shippingMethodChanged()",
"ng-model" => "order.shipping_method_id"
= ship_method.name
#distributor_address.panel{"ng-show" => "!require_ship_address"}
#distributor_address.panel{"ng-show" => "!order.require_ship_address"}
= @order.distributor.distributor_info.andand.html_safe
= @order.order_cycle.pickup_time_for(@order.distributor)
= @order.order_cycle.pickup_instructions_for(@order.distributor)
= f.fields_for :ship_address, @order.ship_address do |sa|
#ship_address{"ng-show" => "require_ship_address"}
#ship_address{"ng-show" => "order.require_ship_address"}
%label
= hidden_field_tag "order[ship_address_same_as_billing]", "false"
= check_box_tag "order[ship_address_same_as_billing]", true, @order.ship_address_same_as_billing,

View File

@@ -13,10 +13,10 @@
%td= adjustment.display_amount.to_html
%tr
%th Shipping
%td {{ shippingPrice() | currency }}
%td {{ Order.shippingPrice() | currency }}
%tr
%th Cart total
%td {{ cartTotal() | currency }}
%td {{ Order.cartTotal() | currency }}
- if current_order.price_adjustment_totals.present?
- current_order.price_adjustment_totals.each do |label, total|
%tr
@@ -25,3 +25,4 @@
= f.submit "Purchase", class: "button"
%a.button.secondary{href: cart_url} Back to Cart

View File

@@ -35,53 +35,60 @@ feature "As a consumer I want to check out my cart", js: true do
before do
distributor.shipping_methods << sm1
distributor.shipping_methods << sm2
visit "/shop/checkout"
click_link "USER"
click_link "CUSTOMER DETAILS"
click_link "BILLING"
click_link "SHIPPING"
click_link "PAYMENT DETAILS"
end
it "shows all shipping methods, but doesn't show ship address when not needed" do
page.should have_content "Frogs"
page.should have_content "Donkeys"
choose(sm2.name)
find("#ship_address", visible: false).visible?.should be_false
end
context "When shipping method requires an address" do
context "on the checkout page" do
before do
visit "/shop/checkout"
toggle_accordion "User"
toggle_accordion "Customer Details"
toggle_accordion "Billing"
toggle_accordion "Shipping"
toggle_accordion "Payment Details"
end
it "shows all shipping methods, but doesn't show ship address when not needed" do
page.should have_content "Frogs"
page.should have_content "Donkeys"
choose(sm2.name)
find("#ship_address", visible: false).visible?.should be_false
end
context "When shipping method requires an address" do
before do
choose(sm1.name)
end
it "shows the hidden ship address fields by default" do
check "Shipping address same as billing address?"
find("#ship_address_hidden").visible?.should be_true
find("#ship_address > div.visible", visible: false).visible?.should be_false
# Check it keeps state
click_button "Purchase"
toggle_accordion "Shipping"
find_field("Shipping address same as billing address?").should be_checked
end
it "shows ship address forms when 'same as billing address' is unchecked" do
uncheck "Shipping address same as billing address?"
find("#ship_address_hidden", visible: false).visible?.should be_false
find("#ship_address > div.visible").visible?.should be_true
# Check it keeps state
click_button "Purchase"
toggle_accordion "Shipping"
find_field("Shipping address same as billing address?").should_not be_checked
end
end
it "copies billing address to hidden shipping address fields" do
choose(sm1.name)
end
it "shows the hidden ship address fields by default" do
check "Shipping address same as billing address?"
find("#ship_address_hidden").visible?.should be_true
find("#ship_address > div.visible", visible: false).visible?.should be_false
# Check it keeps state
click_button "Purchase"
find_field("Shipping address same as billing address?").should be_checked
end
it "shows ship address forms when 'same as billing address' is unchecked" do
uncheck "Shipping address same as billing address?"
find("#ship_address_hidden", visible: false).visible?.should be_false
find("#ship_address > div.visible").visible?.should be_true
# Check it keeps state
click_button "Purchase"
find_field("Shipping address same as billing address?").should_not be_checked
end
end
it "copies billing address to hidden shipping address fields" do
choose(sm1.name)
check "Shipping address same as billing address?"
within "#billing" do
fill_in "Address", with: "testy"
end
within "#ship_address_hidden" do
find("#order_ship_address_attributes_address1", visible: false).value.should == "testy"
within "#billing" do
fill_in "Address", with: "testy"
end
within "#ship_address_hidden" do
find("#order_ship_address_attributes_address1", visible: false).value.should == "testy"
end
end
end
@@ -94,6 +101,11 @@ feature "As a consumer I want to check out my cart", js: true do
pm1 # Lazy evaluation of ze create()s
pm2
visit "/shop/checkout"
toggle_accordion "User"
toggle_accordion "Customer Details"
toggle_accordion "Billing"
toggle_accordion "Shipping"
toggle_accordion "Payment Details"
end
it "shows all available payment methods" do
@@ -106,6 +118,7 @@ feature "As a consumer I want to check out my cart", js: true do
choose sm2.name
click_button "Purchase"
current_path.should == "/shop/checkout"
toggle_accordion "Customer Details"
page.should have_content "can't be blank"
end
@@ -117,11 +130,13 @@ feature "As a consumer I want to check out my cart", js: true do
within "#details" do
fill_in "First Name", with: "Will"
fill_in "Last Name", with: "Marshall"
fill_in "Customer E-Mail", with: "test@test.com"
fill_in "Phone", with: "0468363090"
end
within "#billing" do
fill_in "Address", with: "123 Your Face"
select "Australia", from: "Country"
select "Victoria", from: "State"
fill_in "Customer E-Mail", with: "test@test.com"
fill_in "Phone", with: "0468363090"
fill_in "City", with: "Melbourne"
fill_in "Postcode", with: "3066"
end
@@ -135,13 +150,15 @@ feature "As a consumer I want to check out my cart", js: true do
within "#details" do
fill_in "First Name", with: "Will"
fill_in "Last Name", with: "Marshall"
fill_in "Customer E-Mail", with: "test@test.com"
fill_in "Phone", with: "0468363090"
end
within "#billing" do
fill_in "City", with: "Melbourne"
fill_in "Postcode", with: "3066"
fill_in "Address", with: "123 Your Face"
select "Australia", from: "Country"
select "Victoria", from: "State"
fill_in "Customer E-Mail", with: "test@test.com"
fill_in "Phone", with: "0468363090"
fill_in "City", with: "Melbourne"
fill_in "Postcode", with: "3066"
end
check "Shipping address same as billing address?"
click_button "Purchase"

View File

@@ -18,6 +18,6 @@ module ShopWorkflow
end
def toggle_accordion(name)
find("dd[heading='#{name}'] > a").click
find("dd a", text: name.upcase).click
end
end