From ae2357d309d5602d0c69dafabcc6560a9d2892c3 Mon Sep 17 00:00:00 2001 From: Bing Xie Date: Wed, 24 Aug 2016 15:00:51 +1000 Subject: [PATCH] Edit customer address with first name and last name --- .../templates/admin/edit_address_dialog.html.haml | 12 ++++++++++++ app/models/customer.rb | 6 ------ app/serializers/api/admin/customer_serializer.rb | 4 ++++ spec/features/admin/customers_spec.rb | 4 ++++ spec/models/customer_spec.rb | 8 +++++--- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/templates/admin/edit_address_dialog.html.haml b/app/assets/javascripts/templates/admin/edit_address_dialog.html.haml index 369a3e2ae6..9bc14692c4 100644 --- a/app/assets/javascripts/templates/admin/edit_address_dialog.html.haml +++ b/app/assets/javascripts/templates/admin/edit_address_dialog.html.haml @@ -11,6 +11,18 @@ %table.no-borders %tr %td{style: 'width: 30%'} + = t('spree.firstname') + %span.required * + %td + %input{ type: 'text', name: 'firstname', required: true, ng: { model: 'address.firstname'} } + %tr + %td + = t('spree.lastname') + %span.required * + %td + %input{ type: 'text', name: 'lastname', required: true, ng: { model: 'address.lastname'} } + %tr + %td = t('spree.street_address') %span.required * %td diff --git a/app/models/customer.rb b/app/models/customer.rb index 3003acb848..efa8b5e096 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -16,7 +16,6 @@ class Customer < ActiveRecord::Base before_validation :downcase_email before_validation :empty_code - before_validation :set_unused_address_fields validates :code, uniqueness: { scope: :enterprise_id, allow_nil: true } validates :email, presence: true, uniqueness: { scope: :enterprise_id, message: I18n.t('validation_msg_is_associated_with_an_exising_customer') } @@ -36,11 +35,6 @@ class Customer < ActiveRecord::Base self.code = nil if code.blank? end - def set_unused_address_fields - bill_address.firstname = bill_address.lastname = 'unused' if bill_address.present? - ship_address.firstname = ship_address.lastname = 'unused' if ship_address.present? - end - def associate_user self.user = user || Spree::User.find_by_email(email) end diff --git a/app/serializers/api/admin/customer_serializer.rb b/app/serializers/api/admin/customer_serializer.rb index 1c9d19306d..bfe2afc940 100644 --- a/app/serializers/api/admin/customer_serializer.rb +++ b/app/serializers/api/admin/customer_serializer.rb @@ -8,6 +8,10 @@ class Api::Admin::CustomerSerializer < ActiveModel::Serializer object.tag_list.join(",") end + def name + object.name.blank? ? object.bill_address.andand.full_name : object.name + end + def tags object.tag_list.map do |tag| tag_rule_map = options[:tag_rule_mapping][tag] diff --git a/spec/features/admin/customers_spec.rb b/spec/features/admin/customers_spec.rb index 07352ad151..7a7fa2ba4e 100644 --- a/spec/features/admin/customers_spec.rb +++ b/spec/features/admin/customers_spec.rb @@ -176,6 +176,8 @@ feature 'Customers' do first('#ship-address-link').click expect(page).to have_content 'Edit Shipping Address' + fill_in 'firstname', with: "First" + fill_in 'lastname', with: "Last" fill_in 'address1', with: "New Address1" fill_in 'phone', with: "12345678" fill_in 'city', with: "Melbourne" @@ -190,6 +192,8 @@ feature 'Customers' do ship_address = customer4.reload.ship_address + expect(ship_address.firstname).to eq 'First' + expect(ship_address.lastname).to eq 'Last' expect(ship_address.address1).to eq 'New Address1' expect(ship_address.phone).to eq '12345678' expect(ship_address.city).to eq 'Melbourne' diff --git a/spec/models/customer_spec.rb b/spec/models/customer_spec.rb index 37134e9574..106ec3bcf7 100644 --- a/spec/models/customer_spec.rb +++ b/spec/models/customer_spec.rb @@ -24,7 +24,9 @@ describe Customer, type: :model do it 'updates the shipping address' do expect(customer.shipping_address).to be_nil - ship_address = {zipcode: "3127", + ship_address = {firstname: 'fname', + lastname: 'lname', + zipcode: "3127", city: "Melbourne", state_id: 1, phone: "455500146", @@ -33,8 +35,8 @@ describe Customer, type: :model do customer.update_attributes!(ship_address_attributes: ship_address) expect(customer.ship_address.city).to eq 'Melbourne' - expect(customer.ship_address.firstname).to eq 'unused' - expect(customer.ship_address.lastname).to eq 'unused' + expect(customer.ship_address.firstname).to eq 'fname' + expect(customer.ship_address.lastname).to eq 'lname' end end