Skip to content

Commit 19b0d8e

Browse files
committed
Add SolidusAdmin::AddressesController
Introduce a new `AddressesController` to handle the creation and updating of billing and shipping addresses for orders. Includes new and update actions with strong parameter support and address type validation.
1 parent 064546b commit 19b0d8e

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusAdmin
4+
class AddressesController < BaseController
5+
include Spree::Core::ControllerHelpers::StrongParameters
6+
7+
before_action :load_order
8+
before_action :validate_address_type
9+
10+
def new
11+
address = @order.send("#{address_type}_address")
12+
@order.send("build_#{address_type}_address", country_id: default_country_id) if address.nil?
13+
address ||= @order.send("#{address_type}_address")
14+
address.country_id ||= default_country_id if address.country.nil?
15+
16+
respond_to do |format|
17+
end
18+
end
19+
20+
def update
21+
if @order.contents.update_cart(order_params)
22+
flash[:notice] = t('.success')
23+
redirect_to edit_order_url(@order)
24+
else
25+
flash.now[:error] = @order.errors.full_messages.join(", ")
26+
27+
respond_to do |format|
28+
end
29+
end
30+
end
31+
32+
private
33+
34+
def address_type
35+
params[:type].presence_in(%w[bill ship])
36+
end
37+
38+
def validate_address_type
39+
unless address_type
40+
flash[:error] = t('.errors.address_type_invalid')
41+
redirect_to spree.admin_order_url(@order)
42+
end
43+
end
44+
45+
def default_country_id
46+
@default_country_id ||= begin
47+
country = Spree::Country.default
48+
country.id if Spree::Country.available.exists?(id: country.id)
49+
end
50+
end
51+
52+
def load_order
53+
@order = Spree::Order.find_by!(number: params[:order_id])
54+
authorize! action_name, @order
55+
end
56+
57+
def order_params
58+
params.require(:order).permit(
59+
:use_billing,
60+
:use_shipping,
61+
bill_address_attributes: permitted_address_attributes,
62+
ship_address_attributes: permitted_address_attributes
63+
)
64+
end
65+
end
66+
end

admin/config/locales/orders.en.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ en:
22
solidus_admin:
33
orders:
44
title: "Orders"
5+
addresses:
6+
title: "Addresses"
7+
update:
8+
success: "The address has been successfully updated."
9+
errors:
10+
address_type_invalid: "Invalid address type. Please select either billing or shipping address."

0 commit comments

Comments
 (0)