From 77f43e3ca7755ae52a82be4462690332ba21dfd6 Mon Sep 17 00:00:00 2001 From: Bing Xie Date: Fri, 16 Sep 2016 18:32:03 +1000 Subject: [PATCH] Refactor setting default addresses --- app/controllers/checkout_controller.rb | 20 ++++++++++++++------ app/models/spree/user_decorator.rb | 21 ++++----------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 643af0063e..63149ded6c 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -63,18 +63,26 @@ class CheckoutController < Spree::CheckoutController def set_default_bill_address if params[:order][:default_bill_address] == 'YES' - new_bill_address = @order.bill_address.clone - spree_current_user.set_bill_address(new_bill_address) - @order.customer.bill_address.andand.update_attributes(new_bill_address.attributes) + new_bill_address = @order.bill_address.clone.attributes + + user_bill_address_id = spree_current_user.bill_address.andand.id + spree_current_user.update_attributes(bill_address_attributes: new_bill_address.merge('id' => user_bill_address_id)) + + customer_bill_address_id = @order.customer.bill_address.andand.id + @order.customer.update_attributes(bill_address_attributes: new_bill_address.merge('id' => customer_bill_address_id)) end end def set_default_ship_address if params[:order][:default_ship_address] == 'YES' - new_ship_address = @order.ship_address.clone - spree_current_user.set_ship_address(new_ship_address) - @order.customer.ship_address.andand.update_attributes(new_ship_address.attributes) + new_ship_address = @order.ship_address.clone.attributes + + user_ship_address_id = spree_current_user.ship_address.andand.id + spree_current_user.update_attributes!(ship_address_attributes: new_ship_address.merge('id' => user_ship_address_id)) + + customer_ship_address_id = @order.customer.ship_address.andand.id + @order.customer.update_attributes(ship_address_attributes: new_ship_address.merge('id' => customer_ship_address_id)) end end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 26bf0a7459..b16e082f87 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -14,7 +14,10 @@ Spree.user_class.class_eval do accepts_nested_attributes_for :enterprise_roles, :allow_destroy => true - attr_accessible :enterprise_ids, :enterprise_roles_attributes, :enterprise_limit + accepts_nested_attributes_for :bill_address + accepts_nested_attributes_for :ship_address + + attr_accessible :enterprise_ids, :enterprise_roles_attributes, :enterprise_limit, :bill_address_attributes, :ship_address_attributes after_create :send_signup_confirmation validate :limit_owned_enterprises @@ -76,22 +79,6 @@ Spree.user_class.class_eval do data_array.sort! { |a, b| b.distributed_orders.length <=> a.distributed_orders.length } end - def set_bill_address(address) - if self.bill_address - self.bill_address.update_attributes(address.clone.attributes) - else - self.update_attribute(:bill_address, address.clone) - end - end - - def set_ship_address(address) - if self.ship_address - self.ship_address.update_attributes(address.clone.attributes) - else - self.update_attribute(:ship_address, address.clone) - end - end - private def limit_owned_enterprises