Files
openfoodnetwork/app/services/user_default_address_setter.rb
Matt-Yorkley 87cd936c94 Blacklist timestamp attributes when assigning for update
Doing #clone or #dup of an object's attributes then passing it to an #update/#update_attributes call means we are manually passing values for created_at and updated_at, which can cause problems, especially if the object being duped hasn't been persisted yet: in this case we would be manually attempting to save timestamps with nil values, which is not a good idea. Here they are blacklisted from the attributes hash.
2020-06-30 11:30:16 +02:00

40 lines
1.2 KiB
Ruby

# frozen_string_literal: true
# Sets the order addresses as the user default addresses
class UserDefaultAddressSetter
def initialize(order, current_user)
@order = order
@current_user = current_user
end
# Sets the order bill address as the user default bill address
def set_default_bill_address
new_bill_address = @order.bill_address.dup.attributes.except!('created_at', 'updated_at')
set_bill_address_attributes(@current_user, new_bill_address)
set_bill_address_attributes(@order.customer, new_bill_address)
end
# Sets the order ship address as the user default ship address
def set_default_ship_address
new_ship_address = @order.ship_address.dup.attributes.except!('created_at', 'updated_at')
set_ship_address_attributes(@current_user, new_ship_address)
set_ship_address_attributes(@order.customer, new_ship_address)
end
private
def set_bill_address_attributes(object, new_address)
object.update(
bill_address_attributes: new_address.merge('id' => object.bill_address.andand.id)
)
end
def set_ship_address_attributes(object, new_address)
object.update(
ship_address_attributes: new_address.merge('id' => object.ship_address.andand.id)
)
end
end