From 3f8d470d23d84b5e76ec1b03a3b346a80637f3af Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 11 Dec 2015 12:39:08 +1100 Subject: [PATCH] When admin associates user with an order, look up last used address --- .../admin/search_controller_decorator.rb | 9 +++++- app/serializers/api/admin/user_serializer.rb | 15 ++++++++++ spec/features/admin/orders_spec.rb | 30 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/app/controllers/spree/admin/search_controller_decorator.rb b/app/controllers/spree/admin/search_controller_decorator.rb index f268f95820..6c5942934e 100644 --- a/app/controllers/spree/admin/search_controller_decorator.rb +++ b/app/controllers/spree/admin/search_controller_decorator.rb @@ -12,7 +12,14 @@ Spree::Admin::SearchController.class_eval do :bill_address_lastname_start => params[:q] }).result.limit(10) end - render :users + render :users end + + + def users_with_ams + users_without_ams + render json: @users, each_serializer: Api::Admin::UserSerializer + end + alias_method_chain :users, :ams end diff --git a/app/serializers/api/admin/user_serializer.rb b/app/serializers/api/admin/user_serializer.rb index 501cd75674..c080a8fd6a 100644 --- a/app/serializers/api/admin/user_serializer.rb +++ b/app/serializers/api/admin/user_serializer.rb @@ -1,3 +1,18 @@ +require 'open_food_network/last_used_address' + class Api::Admin::UserSerializer < ActiveModel::Serializer attributes :id, :email + + has_one :ship_address, serializer: Api::AddressSerializer + has_one :bill_address, serializer: Api::AddressSerializer + + def ship_address + object.ship_address || + OpenFoodNetwork::LastUsedAddress.new(object.email).last_used_ship_address + end + + def bill_address + object.bill_address || + OpenFoodNetwork::LastUsedAddress.new(object.email).last_used_bill_address + end end diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb index 12e218bc81..29afad24d2 100644 --- a/spec/features/admin/orders_spec.rb +++ b/spec/features/admin/orders_spec.rb @@ -113,6 +113,36 @@ feature %q{ page.should have_selector 'p', text: "Order cycle: None" end + scenario "filling customer details" do + # Given a customer with an order, which includes their shipping and billing address + @order.ship_address = create(:address, lastname: 'Ship') + @order.bill_address = create(:address, lastname: 'Bill') + @order.shipping_method = create(:shipping_method, require_ship_address: true) + @order.save! + + # When I create a new order + login_to_admin_section + visit '/admin/orders' + click_link 'New Order' + select @distributor.name, from: 'order_distributor_id' + select2_select @order_cycle.name, from: 'order_order_cycle_id' + targetted_select2_search @product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' + click_link 'Add' + page.has_selector? "table.index tbody[data-hook='admin_order_form_line_items'] tr" # Wait for JS + click_button 'Update' + within('h1.page-title') { page.should have_content "Customer Details" } + + # And I select that customer's email address and save the order + targetted_select2_search @order.user.email, from: '#customer_search', dropdown_css: '.select2-drop' + click_button 'Continue' + within('h1.page-title') { page.should have_content "Shipments" } + + # Then their addresses should be associated with the order + order = Spree::Order.last + order.ship_address.lastname.should == 'Ship' + order.bill_address.lastname.should == 'Bill' + end + scenario "capture multiple payments from the orders index page" do # d.cook: could also test for an order that has had payment voided, then a new check payment created but not yet captured. But it's not critical and I know it works anyway.