Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add addresses table to customer page in admin dashboard (#14352)
Kacper Mękarskibb8aad1d094f
diff --git a/spree/admin/app/controllers/spree/admin/user_addresses_controller.rb b/spree/admin/app/controllers/spree/admin/user_addresses_controller.rbnew file mode 100644index 0000000000..5f373949e9--- /dev/null+++ b/spree/admin/app/controllers/spree/admin/user_addresses_controller.rb@@ -0,0 +1,40 @@+module Spree+ module Admin+ class UserAddressesController < ResourceController+ use_table :addresses++ before_action :redirect_to_full_page, only: :index, unless: :turbo_frame_request?++ private++ def model_class+ Spree::Address+ end++ def edit_object_url(_object, _options = {})+ nil+ end++ def parent+ @parent ||= Spree.user_class.accessible_by(current_ability, :show).find_by_prefix_id!(params[:user_id])+ @user = @parent+ end++ def scope+ parent.addresses.accessible_by(current_ability, :index).includes(collection_includes)+ end++ def collection_includes+ %i[country state]+ end++ def collection_url(_options = {})+ spree.admin_users_path+ end++ def redirect_to_full_page+ redirect_to spree.admin_user_path(parent)+ end+ end+ end+enddiff --git a/spree/admin/app/views/spree/admin/tables/columns/_address_name.html.erb b/spree/admin/app/views/spree/admin/tables/columns/_address_name.html.erbnew file mode 100644index 0000000000..9a58f26962--- /dev/null+++ b/spree/admin/app/views/spree/admin/tables/columns/_address_name.html.erb@@ -0,0 +1,9 @@+<div><%= record.full_name %></div>+<div class="flex flex-col items-start gap-1 mt-1">+ <% if record.is_default_billing? %>+ <span class="badge badge-info"><%= Spree.t(:default_billing_address) %></span>+ <% end %>+ <% if record.is_default_shipping? %>+ <span class="badge badge-info"><%= Spree.t(:default_shipping_address) %></span>+ <% end %>+</div>diff --git a/spree/admin/app/views/spree/admin/user_addresses/index.html.erb b/spree/admin/app/views/spree/admin/user_addresses/index.html.erbnew file mode 100644index 0000000000..da6d12d070--- /dev/null+++ b/spree/admin/app/views/spree/admin/user_addresses/index.html.erb@@ -0,0 +1 @@+<%= render_table @collection, :addresses, frame_name: :addresses %>diff --git a/spree/admin/app/views/spree/admin/users/_tabs.html.erb b/spree/admin/app/views/spree/admin/users/_tabs.html.erbindex e44731fade..8278fe48db 100644--- a/spree/admin/app/views/spree/admin/users/_tabs.html.erb+++ b/spree/admin/app/views/spree/admin/users/_tabs.html.erb@@ -26,22 +26,28 @@<a class="nav-link" id="pills-store-credits-tab" data-tabs-target="tab" data-action="click->tabs#select" role="tab" aria-controls="pills-store-credits" aria-selected="false"><%= Spree.t(:store_credits) %></a></li><% end %>++ <% if can?(:read, Spree::Address) %>+ <li class="nav-item" role="presentation">+ <a class="nav-link" id="pills-addresses-tab" data-tabs-target="tab" data-action="click->tabs#select" role="tab" aria-controls="pills-addresses" aria-selected="false"><%= Spree.t(:addresses) %></a>+ </li>+ <% end %></ul><% if spree.respond_to?(:admin_user_visits_path) %>- <div data-tabs-target="panel" id="pills-overview" role="tabpanel" aria-labelledby="pills-overview" class="pt-4 animate-fade-in">+ <div data-tabs-target="panel" id="pills-overview" role="tabpanel" aria-labelledby="pills-overview-tab" class="pt-4 animate-fade-in"><%= turbo_frame_tag :user_visits, src: spree.admin_user_visits_path(@user), target: '_top' %></div><% end %><% if can?(:read, Spree::Order) %>- <div data-tabs-target="panel" id="pills-orders2" role="tabpanel" aria-labelledby="pills-orders2" class="animate-fade-in" <%= 'hidden' if spree.respond_to?(:admin_user_visits_path) %>>+ <div data-tabs-target="panel" id="pills-orders2" role="tabpanel" aria-labelledby="pills-orders2-tab" class="animate-fade-in" <%= 'hidden' if spree.respond_to?(:admin_user_visits_path) %>><%= turbo_frame_tag :orders, src: spree.admin_user_orders_path(@user), loading: :lazy, autoscroll: true, data: { autoscroll_block: :nearest, autoscroll_behavior: :smooth } do %><%= render 'spree/admin/shared/spinner' %><% end %></div>- <div data-tabs-target="panel" id="pills-checkouts" role="tabpanel" aria-labelledby="pills-checkouts" hidden class="animate-fade-in">+ <div data-tabs-target="panel" id="pills-checkouts" role="tabpanel" aria-labelledby="pills-checkouts-tab" hidden class="animate-fade-in"><%= turbo_frame_tag :checkouts, src: spree.admin_user_checkouts_path(@user), loading: :lazy, autoscroll: true, data: { autoscroll_block: :nearest, autoscroll_behavior: :smooth } do %><%= render 'spree/admin/shared/spinner' %><% end %>@@ -49,7 +55,7 @@<% end %><% if spree.respond_to?(:admin_user_gift_cards_path) && can?(:read, Spree::GiftCard) %>- <div data-tabs-target="panel" id="pills-gift-cards" role="tabpanel" aria-labelledby="pills-gift-cards" hidden class="animate-fade-in">+ <div data-tabs-target="panel" id="pills-gift-cards" role="tabpanel" aria-labelledby="pills-gift-cards-tab" hidden class="animate-fade-in"><%= turbo_frame_tag :gift_cards, src: spree.admin_user_gift_cards_path(@user), loading: :lazy, autoscroll: true, data: { autoscroll_block: :nearest, autoscroll_behavior: :smooth } do %><%= render 'spree/admin/shared/spinner' %><% end %>@@ -57,7 +63,7 @@<% end %><% if can?(:read, Spree::StoreCredit) %>- <div data-tabs-target="panel" id="pills-store-credits" role="tabpanel" aria-labelledby="pills-store-credits" hidden class="animate-fade-in">+ <div data-tabs-target="panel" id="pills-store-credits" role="tabpanel" aria-labelledby="pills-store-credits-tab" hidden class="animate-fade-in"><div class="card-lg"><%= turbo_frame_tag :store_credits_list, src: spree.admin_user_store_credits_path(@user, frame_name: :store_credits_list), loading: :lazy, autoscroll: true, data: { autoscroll_block: :nearest, autoscroll_behavior: :smooth } do %><%= render 'spree/admin/shared/spinner' %>@@ -65,4 +71,12 @@</div></div><% end %>++ <% if can?(:read, Spree::Address) %>+ <div data-tabs-target="panel" id="pills-addresses" role="tabpanel" aria-labelledby="pills-addresses-tab" hidden class="animate-fade-in">+ <%= turbo_frame_tag :addresses, src: spree.admin_user_addresses_path(@user), loading: :lazy, autoscroll: true, data: { autoscroll_block: :nearest, autoscroll_behavior: :smooth } do %>+ <%= render 'spree/admin/shared/spinner' %>+ <% end %>+ </div>+ <% end %></div>diff --git a/spree/admin/config/initializers/spree_admin_tables.rb b/spree/admin/config/initializers/spree_admin_tables.rbindex a6a258294c..adee896f4e 100644--- a/spree/admin/config/initializers/spree_admin_tables.rb+++ b/spree/admin/config/initializers/spree_admin_tables.rb@@ -2173,4 +2173,67 @@ Rails.application.config.after_initialize dofilterable: true,default: true,position: 20++ # Register Addresses table (customer address book)+ Spree.admin.tables.register(:addresses, model_class: Spree::Address, search_param: :firstname_or_lastname_or_address1_or_address2_or_city_or_zipcode_cont, row_actions: false, new_resource: false)++ Spree.admin.tables.addresses.add :name,+ label: :name,+ type: :custom,+ sortable: false,+ filterable: false,+ default: true,+ position: 10,+ partial: 'spree/admin/tables/columns/address_name'++ Spree.admin.tables.addresses.add :address,+ label: :address,+ type: :string,+ sortable: false,+ filterable: false,+ default: true,+ position: 20,+ method: ->(address) { [address.address1, address.address2].reject(&:blank?).join(', ') }++ Spree.admin.tables.addresses.add :city,+ label: :city,+ type: :string,+ sortable: true,+ filterable: true,+ default: true,+ position: 30++ Spree.admin.tables.addresses.add :state,+ label: :state,+ type: :string,+ sortable: false,+ filterable: false,+ default: true,+ position: 40,+ method: ->(address) { address.state_text }++ Spree.admin.tables.addresses.add :zipcode,+ label: :zipcode,+ type: :string,+ sortable: true,+ filterable: true,+ default: true,+ position: 50++ Spree.admin.tables.addresses.add :country,+ label: :country,+ type: :string,+ sortable: false,+ filterable: false,+ default: true,+ position: 60,+ method: ->(address) { localized_country_name(address.country) if address.country }++ Spree.admin.tables.addresses.add :phone,+ label: :phone,+ type: :string,+ sortable: false,+ filterable: true,+ default: true,+ position: 70enddiff --git a/spree/admin/config/routes.rb b/spree/admin/config/routes.rbindex 8b1c3c2339..3ef1244543 100644--- a/spree/admin/config/routes.rb+++ b/spree/admin/config/routes.rb@@ -133,6 +133,7 @@ Spree::Core::Engine.add_routes doresources :store_creditsresources :orders, only: [:index]resources :checkouts, only: [:index]+ resources :addresses, only: [:index], controller: 'user_addresses'resources :gift_cardscollection dodiff --git a/spree/admin/spec/controllers/spree/admin/user_addresses_controller_spec.rb b/spree/admin/spec/controllers/spree/admin/user_addresses_controller_spec.rbnew file mode 100644index 0000000000..723023eb80--- /dev/null+++ b/spree/admin/spec/controllers/spree/admin/user_addresses_controller_spec.rb@@ -0,0 +1,41 @@+require 'spec_helper'++RSpec.describe Spree::Admin::UserAddressesController, type: :controller do+ stub_authorization!+ render_views++ let(:user) { create(:user) }+ let!(:address) { create(:address, user: user) }+ let!(:other_address) { create(:address, user: create(:user)) }++ describe 'GET #index' do+ context 'as a Turbo Frame request' do+ before { request.headers['Turbo-Frame'] = 'addresses' }++ it 'renders the addresses table scoped to the user' do+ get :index, params: { user_id: user.to_param }++ expect(response).to have_http_status(:ok)+ expect(response).to render_template(:index)+ expect(assigns(:collection)).to contain_exactly(address)+ expect(assigns(:user)).to eq(user)+ end+ end++ context 'as a full-page request (not a Turbo Frame)' do+ it 'redirects to the user page' do+ get :index, params: { user_id: user.to_param }++ expect(response).to redirect_to(spree.admin_user_path(user))+ end+ end++ context 'when the user cannot be found' do+ it 'redirects to the users list' do+ get :index, params: { user_id: 'cus_nonexistent' }++ expect(response).to redirect_to(spree.admin_users_path)+ end+ end+ end+end