diff --git a/app/assets/javascripts/darkswarm/controllers/checkout/shipping_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout/shipping_controller.js.coffee index 1d0280842d..cbb90b4352 100644 --- a/app/assets/javascripts/darkswarm/controllers/checkout/shipping_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/checkout/shipping_controller.js.coffee @@ -1,5 +1,6 @@ -Darkswarm.controller "ShippingCtrl", ($scope, $timeout) -> +Darkswarm.controller "ShippingCtrl", ($scope, $timeout, ShippingMethods) -> angular.extend(this, new FieldsetMixin($scope)) + $scope.ShippingMethods = ShippingMethods $scope.name = "shipping" $scope.nextPanel = "payment" diff --git a/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee b/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee index 820d5bde9c..ff7db62f4a 100644 --- a/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee +++ b/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee @@ -1,4 +1,5 @@ Darkswarm.factory "AuthenticationService", (Navigation, $modal, $location, Redirections)-> + new class AuthenticationService selectedPath: "/login" diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index d02d2bf5c1..6a91911472 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -1,5 +1,5 @@ -Darkswarm.factory 'Cart', (Order)-> +Darkswarm.factory 'Cart', (CurrentOrder)-> # Handles syncing of current cart/order state to server new class Cart - order: Order.order + order: CurrentOrder.order diff --git a/app/assets/javascripts/darkswarm/services/checkout.js.coffee b/app/assets/javascripts/darkswarm/services/checkout.js.coffee index 603230358b..4d0a6de5ec 100644 --- a/app/assets/javascripts/darkswarm/services/checkout.js.coffee +++ b/app/assets/javascripts/darkswarm/services/checkout.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashLoader, Loading)-> +Darkswarm.factory 'Checkout', (Order, ShippingMethods, PaymentMethods, $http, Navigation, CurrentHub, RailsFlashLoader, Loading)-> new class Checkout errors: {} secrets: {} @@ -8,7 +8,7 @@ Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashL submit: -> Loading.message = "Submitting your order: please wait" $http.put('/checkout', {order: @preprocess()}).success (data, status)=> - Navigation.go data.pat + Navigation.go data.path .error (response, status)=> Loading.clear() @errors = response.errors @@ -47,7 +47,7 @@ Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashL munged_order shippingMethod: -> - @order.shipping_methods[@order.shipping_method_id] if @order.shipping_method_id + ShippingMethods.shipping_methods_by_id[@order.shipping_method_id] if @order.shipping_method_id requireShipAddress: -> @shippingMethod()?.require_ship_address @@ -56,7 +56,7 @@ Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashL @shippingMethod()?.price || 0.0 paymentMethod: -> - @order.payment_methods[@order.payment_method_id] + PaymentMethods.payment_methods_by_id[@order.payment_method_id] cartTotal: -> @shippingPrice() + @order.display_total diff --git a/app/assets/javascripts/darkswarm/services/current_order.js.coffee b/app/assets/javascripts/darkswarm/services/current_order.js.coffee index 03c9e2819c..a0714dc005 100644 --- a/app/assets/javascripts/darkswarm/services/current_order.js.coffee +++ b/app/assets/javascripts/darkswarm/services/current_order.js.coffee @@ -1,8 +1,5 @@ Darkswarm.factory 'CurrentOrder', (currentOrder) -> new class CurrentOrder - constructor: -> - @[k] = v for k, v of currentOrder - @cart_count ?= 0 - + order: currentOrder empty: => - @line_items.length == 0 + @order.line_items.length == 0 diff --git a/app/assets/javascripts/darkswarm/services/order.js.coffee b/app/assets/javascripts/darkswarm/services/order.js.coffee index 06472b07cc..17933b6a4c 100644 --- a/app/assets/javascripts/darkswarm/services/order.js.coffee +++ b/app/assets/javascripts/darkswarm/services/order.js.coffee @@ -1,5 +1,4 @@ Darkswarm.factory 'Order', (order)-> new class Order - errors: {} order: order diff --git a/app/helpers/enterprises_helper.rb b/app/helpers/enterprises_helper.rb index ed22831589..89555d3b4f 100644 --- a/app/helpers/enterprises_helper.rb +++ b/app/helpers/enterprises_helper.rb @@ -3,6 +3,10 @@ module EnterprisesHelper @current_distributor ||= current_order(false).andand.distributor end + def available_shipping_methods + current_distributor.shipping_methods.uniq + end + def managed_enterprises Enterprise.managed_by(spree_current_user) end diff --git a/app/helpers/shared_helper.rb b/app/helpers/shared_helper.rb index ff4310a8f0..a1e1f84ec2 100644 --- a/app/helpers/shared_helper.rb +++ b/app/helpers/shared_helper.rb @@ -1,21 +1,4 @@ module SharedHelper - def inject_enterprises - inject_json_ams "enterprises", Enterprise.all, Api::EnterpriseSerializer, active_distributors: @active_distributors - end - - def inject_taxons - inject_json_ams "taxons", Spree::Taxon.all, Api::TaxonSerializer - end - - def inject_json(name, partial, opts = {}) - render partial: "json/injection", locals: {name: name, partial: partial}.merge(opts) - end - - def inject_json_ams(name, data, serializer, opts = {}) - json = ActiveModel::ArraySerializer.new(data, {each_serializer: serializer}.merge(opts)).to_json - render partial: "json/injection_ams", locals: {name: name, json: json} - end - def distributor_link_class(distributor) cart = current_order(true) @active_distributors ||= Enterprise.distributors_with_active_order_cycles diff --git a/app/serializers/api/address_serializer.rb b/app/serializers/api/address_serializer.rb index 98344e0315..e121cfde38 100644 --- a/app/serializers/api/address_serializer.rb +++ b/app/serializers/api/address_serializer.rb @@ -5,6 +5,6 @@ class Api::AddressSerializer < ActiveModel::Serializer attributes :id, :zipcode, :city, :state def state - object.state.abbr + object.state.andand.abbr end end diff --git a/app/views/checkout/_form.html.haml b/app/views/checkout/_form.html.haml index df86915847..68aaf2d781 100644 --- a/app/views/checkout/_form.html.haml +++ b/app/views/checkout/_form.html.haml @@ -4,6 +4,8 @@ novalidate: true, name: "checkout"} do |f| + = inject_available_shipping_methods + = inject_available_payment_methods :javascript angular.module('Darkswarm').value('order', #{render "checkout/order"}) diff --git a/app/views/checkout/_order.rabl b/app/views/checkout/_order.rabl index a4a7673c62..778f4428d0 100644 --- a/app/views/checkout/_order.rabl +++ b/app/views/checkout/_order.rabl @@ -1,5 +1,5 @@ #NOTE: when adding new fields for user input, it may want to be cached in localStorage -# If so, make sure to add it to Order.attributes_to_cache +# If so, make sure to add it to controller attribute caching object current_order attributes :id, :email, :shipping_method_id, :user_id @@ -20,6 +20,9 @@ child current_order.ship_address => :ship_address do attributes :phone, :firstname, :lastname, :address1, :address2, :city, :country_id, :state_id, :zipcode end +# This is actually totally decoupled data and should be injected separately into their +# own services + node :shipping_methods do Hash[current_distributor.shipping_methods.uniq.collect { |method| [method.id, { diff --git a/app/views/checkout/_payment.html.haml b/app/views/checkout/_payment.html.haml index bf1c20cac5..c3c2ad8d4c 100644 --- a/app/views/checkout/_payment.html.haml +++ b/app/views/checkout/_payment.html.haml @@ -16,7 +16,7 @@ .small-11.columns %em %small - {{ Order.paymentMethod().name }} + {{ Checkout.paymentMethod().name }} .small-1.columns.right %span.accordion-up.right %i.ofn-i_006-caret-up diff --git a/app/views/checkout/_shipping.html.haml b/app/views/checkout/_shipping.html.haml index 410fef7e2d..c7ab628c68 100644 --- a/app/views/checkout/_shipping.html.haml +++ b/app/views/checkout/_shipping.html.haml @@ -16,7 +16,7 @@ .small-11.columns %em %small - {{ Order.shippingMethod().name }} + {{ Checkout.shippingMethod().name }} .small-1.columns.right %span.accordion-up.right %i.ofn-i_006-caret-up @@ -24,20 +24,20 @@ %i.ofn-i_005-caret-down .small-12.columns.medium-6.columns.large-6.columns - - for ship_method, i in current_distributor.shipping_methods.uniq - %label - -#= radio_button_tag "order[shipping_method_id]", ship_method.id, false, - -#"ng-model" => "order.shipping_method_id" - %input{type: :radio, value: ship_method.id, - required: true, - "ng-model" => "order.shipping_method_id"} - = ship_method.name - %label{"ng-if" => "Order.requireShipAddress()"} - %input{type: :checkbox, "ng-model" => "Order.ship_address_same_as_billing"} + %label{"ng-repeat" => "method in ShippingMethods.shipping_methods"} + %input{type: :radio, + required: true, + "ng-value" => "method.id", + "ng-model" => "order.shipping_method_id"} + {{ method.name }} + + %label{"ng-if" => "Checkout.requireShipAddress()"} + %input{type: :checkbox, "ng-model" => "Checkout.ship_address_same_as_billing"} Shipping address same as billing address? + .small-12.columns.medium-6.columns.large-6.columns - #distributor_address.panel{"ng-show" => "Order.shippingMethod().description"} - %span{ style: "white-space: pre-wrap;" }{{ Order.shippingMethod().description }} + #distributor_address.panel{"ng-show" => "Checkout.shippingMethod().description"} + %span{ style: "white-space: pre-wrap;" }{{ Checkout.shippingMethod().description }} %br/ %br/ = 'Ready for:' if @order.order_cycle.pickup_time_for(@order.distributor) @@ -45,8 +45,8 @@ = f.fields_for :ship_address, @order.ship_address do |sa| .small-12.columns - #ship_address{"ng-if" => "Order.requireShipAddress()"} - %div.visible{"ng-if" => "!Order.ship_address_same_as_billing"} + #ship_address{"ng-if" => "Checkout.requireShipAddress()"} + %div.visible{"ng-if" => "!Checkout.ship_address_same_as_billing"} .row .small-12.columns = validated_input "Address", "order.ship_address.address1", "ofn-focus" => "accordion['shipping']" diff --git a/app/views/checkout/_summary.html.haml b/app/views/checkout/_summary.html.haml index 47f9239839..60fb8ca80a 100644 --- a/app/views/checkout/_summary.html.haml +++ b/app/views/checkout/_summary.html.haml @@ -13,10 +13,10 @@ %td= adjustment.display_amount.to_html %tr %th Shipping - %td {{ Order.shippingPrice() | currency }} + %td {{ Checkout.shippingPrice() | currency }} %tr %th Cart total - %td {{ Order.cartTotal() | currency }} + %td {{ Checkout.cartTotal() | currency }} - if current_order.price_adjustment_totals.present? - current_order.price_adjustment_totals.each do |label, total| %tr diff --git a/app/views/layouts/darkswarm.html.haml b/app/views/layouts/darkswarm.html.haml index f831d93d17..51e141257b 100644 --- a/app/views/layouts/darkswarm.html.haml +++ b/app/views/layouts/darkswarm.html.haml @@ -25,10 +25,10 @@ = javascript_include_tag "iehack" = inject_json "currentHub", "current_hub" - = inject_json "currentOrder", "current_order" = inject_json "user", "current_user" = inject_json "railsFlash", "flash" = inject_taxons + = inject_current_order .off-canvas-wrap{offcanvas: true} .inner-wrap diff --git a/spec/helpers/shared_helper_spec.rb b/spec/helpers/shared_helper_spec.rb index 4e7208c8fc..92c688d1ae 100644 --- a/spec/helpers/shared_helper_spec.rb +++ b/spec/helpers/shared_helper_spec.rb @@ -23,28 +23,4 @@ describe SharedHelper do helper.stub(:current_order) { order } helper.distributor_link_class(d1).should =~ /empties-cart/ end - - describe "injecting json" do - let!(:enterprise) { create(:distributor_enterprise, facebook: "roger") } - - it "will inject via AMS" do - helper.inject_json_ams("test", [enterprise], Api::EnterpriseSerializer).should match enterprise.name - end - - it "injects enterprises" do - helper.inject_enterprises.should match enterprise.name - helper.inject_enterprises.should match enterprise.facebook - end - - it "injects taxons" do - taxon = create(:taxon) - helper.inject_taxons.should match taxon.name - end - - it "injects taxons" do - taxon = create(:taxon) - helper.inject_taxons.should match taxon.name - end - end - end diff --git a/spec/javascripts/unit/darkswarm/services/checkout_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/checkout_spec.js.coffee index 63e29d4ed2..73f6b9a342 100644 --- a/spec/javascripts/unit/darkswarm/services/checkout_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/checkout_spec.js.coffee @@ -7,6 +7,25 @@ describe 'Checkout service', -> scope = null FlashLoaderMock = loadFlash: (arg)-> + paymentMethods = [{ + id: 99 + test: "foo" + method_type: "gateway" + }, { + id: 123 + test: "bar" + method_type: "check" + }] + shippingMethods = [ + { + id: 7 + require_ship_address: true + price: 0.0 + }, { + id: 25 + require_ship_address: false + price: 13 + }] beforeEach -> orderData = @@ -19,26 +38,13 @@ describe 'Checkout service', -> lastname: "Harrington" ship_address: {test: "bar"} user_id: 901 - shipping_methods: - 7: - require_ship_address: true - price: 0.0 - - 25: - require_ship_address: false - price: 13 - payment_methods: - 99: - test: "foo" - method_type: "gateway" - 123: - test: "bar" - method_type: "check" angular.module('Darkswarm').value('order', orderData) module 'Darkswarm' module ($provide)-> $provide.value "RailsFlashLoader", FlashLoaderMock + $provide.value "shippingMethods", shippingMethods + $provide.value "paymentMethods", paymentMethods null inject ($injector, _$httpBackend_, $rootScope)-> @@ -74,7 +80,7 @@ describe 'Checkout service', -> it 'Gets the current payment method', -> expect(Checkout.paymentMethod()).toEqual null Checkout.order.payment_method_id = 99 - expect(Checkout.paymentMethod()).toEqual {test: "foo", method_type: "gateway"} + expect(Checkout.paymentMethod()).toEqual paymentMethods[0] it "Posts the Checkout to the server", -> $httpBackend.expectPUT("/checkout", {order: Checkout.preprocess()}).respond 200, {path: "test"}