From 19350eeade487dc05474dcc063271d90119ec3dc Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 5 Mar 2014 12:29:49 +1100 Subject: [PATCH] Adding the 'same as billing address' JS --- .../controllers/checkout_controller.js.coffee | 1 + app/views/shop/checkout/_form.html.haml | 73 ++++++++++++------- .../consumer/shopping/checkout_spec.rb | 28 ++++++- 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee index 6c4ec5d2cb..3b6e56f679 100644 --- a/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee @@ -2,6 +2,7 @@ angular.module("Checkout").controller "CheckoutCtrl", ($scope, $rootScope) -> $scope.require_ship_address = false $scope.shipping_method = -1 $scope.payment_method = -1 + $scope.same_as_billing = true $scope.shippingMethodChanged = -> $scope.require_ship_address = $("#order_shipping_method_id_" + $scope.shipping_method).attr("data-require-ship-address") diff --git a/app/views/shop/checkout/_form.html.haml b/app/views/shop/checkout/_form.html.haml index 2add9a5c39..5b0d377071 100644 --- a/app/views/shop/checkout/_form.html.haml +++ b/app/views/shop/checkout/_form.html.haml @@ -21,20 +21,27 @@ = f.fields_for :bill_address do |ba| .row .large-12.columns - = ba.text_field :address1, label: "Billing Address" + = ba.text_field :address1, label: "Billing Address", + "ng-model" => "billing_address1" .row .large-12.columns - = ba.text_field :address2 + = ba.text_field :address2, + "ng-model" => "billing_address2" .row .large-6.columns - = ba.text_field :city + = ba.text_field :city, + "ng-model" => "billing_city" + .large-6.columns - = ba.select :country_id, Spree::Country.order(:name).select([:id, :name]).map{|c| [c.name, c.id]} + = ba.select :country_id, Spree::Country.order(:name).select([:id, :name]).map{|c| [c.name, c.id]}, + "ng-model" => "billing_country_id" .row .large-6.columns - = ba.select :state_id, Spree::State.order(:name).select([:id, :name]).map{|c| [c.name, c.id]} + = ba.select :state_id, Spree::State.order(:name).select([:id, :name]).map{|c| [c.name, c.id]}, + "ng-model" => "billing_state_id" .large-6.columns.right - = ba.text_field :zipcode + = ba.text_field :zipcode, + "ng-model" => "billing_zipcode" %fieldset#shipping @@ -52,26 +59,42 @@ = f.fields_for :ship_address do |sa| #ship_address{"ng-show" => "require_ship_address"} - .row - .large-12.columns - = sa.text_field :address1 - .row - .large-12.columns - = sa.text_field :address2 + %label + = check_box_tag :same_as_billing, true, true, + "ng-model" => "same_as_billing" + Shipping address same as billing address? + + %div.visible{"ng-show" => "!same_as_billing"} + .row + .large-12.columns + = sa.text_field :address1 + .row + .large-12.columns + = sa.text_field :address2 + + .row + .large-6.columns + = sa.text_field :city + .large-6.columns + = sa.select :country_id, Spree::Country.order(:name).select([:id, :name]).map{|c| [c.name, c.id]} + .row + .large-6.columns.right + = sa.text_field :zipcode + .row + .large-6.columns + = sa.text_field :firstname + .large-6.columns + = sa.text_field :lastname + + #ship_address_hidden{"ng-show" => "same_as_billing"} + = sa.hidden_field :address1, "ng-value" => "billing_address1" + = sa.hidden_field :address2, "ng-value" => "billing_address2" + = sa.hidden_field :city, "ng-value" => "billing_city" + = sa.hidden_field :country_id, "ng-value" => "billing_country_id" + = sa.hidden_field :zipcode, "ng-value" => "billing_zipcode" + = sa.hidden_field :firstname, "ng-value" => "billing_firstname" + = sa.hidden_field :lastname, "ng-value" => "billing_lastname" - .row - .large-6.columns - = sa.text_field :city - .large-6.columns - = sa.select :country_id, Spree::Country.order(:name).select([:id, :name]).map{|c| [c.name, c.id]} - .row - .large-6.columns.right - = sa.text_field :zipcode - .row - .large-6.columns - = sa.text_field :firstname - .large-6.columns - = sa.text_field :lastname %fieldset#payment %legend Payment Details - current_order.available_payment_methods.each do |method| diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index 4e08b9d5e4..6c33dde5d5 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -133,15 +133,35 @@ feature "As a consumer I want to check out my cart", js: true do page.should have_content "Donkeys" end - it "doesn't show ship address forms " do + it "doesn't show ship address forms when a shipping method wants no address" do choose(sm2.name) find("#ship_address").visible?.should be_false end - it "shows ship address forms when selected shipping method requires one" do + 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?.should be_false + 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?.should be_false + find("#ship_address > div.visible").visible?.should be_true + end + end + + it "copies billing address to hidden shipping address fields" do choose(sm1.name) - save_and_open_page - find("#ship_address").visible?.should be_true + check "Shipping address same as billing address?" + fill_in "Billing Address", with: "testy" + within "#ship_address_hidden" do + find("#order_ship_address_attributes_address1").value.should == "testy" + end end describe "with payment methods" do