Skip to content

Commit

Permalink
Add a customer controller to allow editing the contact email
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Nov 7, 2023
1 parent eab114b commit 19f4bea
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<%= page_with_sidebar_aside do %>
<%= render component('ui/panel').new(title: panel_title_with_more_links(t(".customer"), [
link_to(t(".edit_email"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"),
link_to(t(".edit_email"), solidus_admin.order_customer_path(@order), class: "p-2 hover:bg-gray-25 rounded-sm text-black"),
link_to(t(".edit_shipping"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"),
link_to(t(".edit_billing"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"),
link_to(t(".remove_customer"), "#", 'data-turbo-method': :delete, class: "p-2 hover:bg-gray-25 rounded-sm text-red-500"),
link_to(t(".remove_customer"), solidus_admin.order_customer_path(@order), 'data-turbo-method': :delete, class: "p-2 hover:bg-gray-25 rounded-sm text-red-500"),
])) do %>
<div class="flex flex-col -m-6 p-6 gap-6 border-t border-gray-100 mt-0">
<%# CUSTOMER %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="<%= stimulus_id %>">
<%= render component("orders/show").new(order: @order) %>
<%= render component("ui/modal").new(title: t(".title"), close_path: solidus_admin.order_path(@order)) do |modal| %>
<%= form_for @order, html: { id: form_id} do |f| %>
<%= render component("ui/forms/field").text_field(f, :email) %>
<% end %>
<% modal.with_actions do %>
<%= render component("ui/button").new(form: form_id, type: :submit, text: "Update") %>
<% end %>
<% end %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Customer::Component < SolidusAdmin::BaseComponent
def initialize(order:)
@order = order
end

Check warning on line 6 in admin/app/components/solidus_admin/orders/show/customer/component.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/components/solidus_admin/orders/show/customer/component.rb#L3-L6

Added lines #L3 - L6 were not covered by tests

def form_id
dom_id(@order, "#{stimulus_id}_email_form")
end
end

Check warning on line 11 in admin/app/components/solidus_admin/orders/show/customer/component.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/components/solidus_admin/orders/show/customer/component.rb#L8-L11

Added lines #L8 - L11 were not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
en:
title: "Edit customer"
19 changes: 19 additions & 0 deletions admin/app/controllers/solidus_admin/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class SolidusAdmin::CustomersController < SolidusAdmin::BaseController
before_action :load_order, only: :show

Check warning on line 4 in admin/app/controllers/solidus_admin/customers_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/customers_controller.rb#L3-L4

Added lines #L3 - L4 were not covered by tests

def show
render component('orders/show/customer').new(order: @order)
end

Check warning on line 8 in admin/app/controllers/solidus_admin/customers_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/customers_controller.rb#L6-L8

Added lines #L6 - L8 were not covered by tests

private

Check warning on line 10 in admin/app/controllers/solidus_admin/customers_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/customers_controller.rb#L10

Added line #L10 was not covered by tests

def load_order
@order = Spree::Order.find_by!(number: params[:order_id])
end

Check warning on line 14 in admin/app/controllers/solidus_admin/customers_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/customers_controller.rb#L12-L14

Added lines #L12 - L14 were not covered by tests

def authorization_subject
@order || Spree::Order
end
end

Check warning on line 19 in admin/app/controllers/solidus_admin/customers_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/customers_controller.rb#L16-L19

Added lines #L16 - L19 were not covered by tests
21 changes: 21 additions & 0 deletions admin/app/controllers/solidus_admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module SolidusAdmin
class OrdersController < SolidusAdmin::BaseController
include Spree::Core::ControllerHelpers::StrongParameters

def index
orders = Spree::Order
.order(created_at: :desc, id: :desc)
Expand All @@ -26,6 +28,21 @@ def show
end
end

def update
load_order

Check warning on line 32 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L32

Added line #L32 was not covered by tests

@order.assign_attributes(order_params)
@order.email ||= @order.user.email if @order.user && @order.user.changed?

Check warning on line 35 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L34-L35

Added lines #L34 - L35 were not covered by tests

if @order.save
flash[:notice] = t('.success')

Check warning on line 38 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L37-L38

Added lines #L37 - L38 were not covered by tests
else
flash[:error] = t('.error')

Check warning on line 40 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L40

Added line #L40 was not covered by tests
end

redirect_to spree.edit_admin_order_path(@order)

Check warning on line 43 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L43

Added line #L43 was not covered by tests
end

def edit
redirect_to action: :show
end
Expand Down Expand Up @@ -63,5 +80,9 @@ def load_order
@order = Spree::Order.find_by!(number: params[:id])
authorize! action_name, @order
end

def order_params
params.require(:order).permit(:user_id, permitted_order_attributes)

Check warning on line 85 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L85

Added line #L85 was not covered by tests
end
end
end
2 changes: 2 additions & 0 deletions admin/config/locales/orders.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ en:
solidus_admin:
orders:
title: "Orders"
update:
success: "Order was updated successfully"
3 changes: 2 additions & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
get 'states', to: 'countries#states'
end

resources :orders, only: [:index, :show, :edit] do
resources :orders, only: [:index, :show, :edit, :update] do
resources :line_items, only: [:destroy, :create, :update]
resource :customer

member do
get :variants_for
Expand Down

0 comments on commit 19f4bea

Please sign in to comment.