Skip to content

Commit

Permalink
Load the current address with a standard load_address before action
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Nov 21, 2023
1 parent eb9b70a commit 5f85b29
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<%= render component("ui/forms/checkbox").new(
name: "#{form.object_name}[#{use_attribute}]",
checked: form.object.send("#{@type}_address").new_record? || form.object.bill_address == form.object.ship_address,
checked: @address == (@type == 'ship' ? @order.bill_address : @order.ship_address),
value: '1'
) %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(order:, address:, user: nil, type: 'ship')
@order = order
@user = user
@address = address
@addresses = user&.addresses.to_a.reject(&:new_record?)
@type = validate_address_type(type)
end

Expand Down
17 changes: 6 additions & 11 deletions admin/app/controllers/solidus_admin/addresses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ module SolidusAdmin
class AddressesController < BaseController
include Spree::Core::ControllerHelpers::StrongParameters

before_action :load_order
before_action :load_order, :load_address
before_action :validate_address_type

def show
address = find_address || build_new_address

respond_to do |format|
format.html do
render component('orders/show/address').new(
order: @order,
user: @order.user,
address: address,
address: @address,
type: address_type,
)
end
Expand Down Expand Up @@ -48,17 +46,14 @@ def update

private

def find_address
def load_address
if params[:address_id].present? && @order.user
address = @order.user.addresses.find_by(id: params[:address_id])
@order.send("#{address_type}_address=", address) if address
@address = @order.user.addresses.find_by(id: params[:address_id])
else
@order.send("#{address_type}_address")
@address = @order.send("#{address_type}_address")
end
end

def build_new_address
@order.send("build_#{address_type}_address", country: default_country)
@address ||= @order.user.addresses.build(country: default_country)
end

def address_type
Expand Down

0 comments on commit 5f85b29

Please sign in to comment.