Skip to content

Commit c6efd71

Browse files
committed
Refactor address controller by introducing a show action
Revisited the address handling in the controller by removing the `new` action and consolidating its functionality into the `show` action. The show action now manages rendering of addresses by checking for: 1. an existing `address_id` parameter 2. using the order's address 3. or building a new, non-persistent address.
1 parent 57ec0b8 commit c6efd71

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

admin/app/components/solidus_admin/orders/show/component.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
<%= page_with_sidebar_aside do %>
1717
<%= render component('ui/panel').new do |panel| %>
1818
<% panel.with_menu t(".edit_email"), solidus_admin.order_customer_path(@order) %>
19-
<% panel.with_menu t(".edit_shipping"), solidus_admin.new_order_ship_address_path(@order) %>
20-
<% panel.with_menu t(".edit_billing"), solidus_admin.new_order_bill_address_path(@order) %>
19+
<% panel.with_menu t(".edit_shipping"), solidus_admin.edit_order_ship_address_path(@order) %>
20+
<% panel.with_menu t(".edit_billing"), solidus_admin.edit_order_bill_address_path(@order) %>
2121
<% panel.with_menu t(".remove_customer"), solidus_admin.order_customer_path(@order), method: :delete, class: "text-red-500" if @order.user %>
2222

2323
<% panel.with_section(class: 'flex flex-col gap-6') do %>
@@ -49,7 +49,7 @@
4949
<% if @order.ship_address %>
5050
<%= format_address @order.ship_address %>
5151
<% else %>
52-
<%= link_to t(".add_shipping"), solidus_admin.new_order_ship_address_path(@order), class: 'body-link' %>
52+
<%= link_to t(".add_shipping"), solidus_admin.edit_order_ship_address_path(@order), class: 'body-link' %>
5353
<% end %>
5454
</div>
5555
</div>
@@ -58,7 +58,7 @@
5858
<span class="body-small-bold"><%= @order.class.human_attribute_name(:bill_address) %></span>
5959
<div class="body-small">
6060
<% if @order.bill_address.blank? %>
61-
<%= link_to t(".add_billing"), solidus_admin.new_order_bill_address_path(@order), class: 'body-link' %>
61+
<%= link_to t(".add_billing"), solidus_admin.edit_order_bill_address_path(@order), class: 'body-link' %>
6262
<% elsif @order.bill_address == @order.ship_address %>
6363
<span class="text-gray-500"><%= t('.same_as_shipping') %></span>
6464
<% else %>

admin/app/controllers/solidus_admin/addresses_controller.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@ class AddressesController < BaseController
77
before_action :load_order
88
before_action :validate_address_type
99

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?
10+
def show
11+
address = find_address || build_new_address
1512

1613
respond_to do |format|
17-
format.html { render component('orders/show/address').new(order: @order, address: address, type: address_type) }
14+
format.html do
15+
render component('orders/show/address').new(
16+
order: @order,
17+
address: address,
18+
type: address_type,
19+
)
20+
end
1821
end
1922
end
2023

24+
def edit
25+
redirect_to action: :show
26+
end
27+
2128
def update
2229
if @order.contents.update_cart(order_params)
2330
redirect_to order_path(@order), status: :see_other, notice: t('.success')
@@ -39,6 +46,20 @@ def update
3946

4047
private
4148

49+
def find_address
50+
if params[:address_id].present? && @order.user
51+
@order.user.addresses.find(params[:address_id])
52+
else
53+
@order.send("#{address_type}_address")
54+
end
55+
end
56+
57+
def build_new_address
58+
@order.send("build_#{address_type}_address", country_id: default_country_id).tap do |address|
59+
address.country_id ||= default_country_id if address.country.nil?
60+
end
61+
end
62+
4263
def address_type
4364
params[:type].presence_in(%w[bill ship])
4465
end

admin/config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
resources :orders, only: [:index, :show, :edit, :update] do
2222
resources :line_items, only: [:destroy, :create, :update]
2323
resource :customer
24-
resource :ship_address, only: [:new, :update], controller: "addresses", type: "ship"
25-
resource :bill_address, only: [:new, :update], controller: "addresses", type: "bill"
24+
resource :ship_address, only: [:show, :edit, :update], controller: "addresses", type: "ship"
25+
resource :bill_address, only: [:show, :edit, :update], controller: "addresses", type: "bill"
2626

2727
member do
2828
get :variants_for

0 commit comments

Comments
 (0)