Skip to content

Commit 9f8a0a5

Browse files
Add new admin store credits edit_memo flow
1 parent 53741da commit 9f8a0a5

File tree

12 files changed

+179
-31
lines changed

12 files changed

+179
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
en:
2-
title: Edit Store Credit
2+
title: Edit Store Credit Amount
33
cancel: Cancel
44
submit: Update Store Credit
55
choose_reason: Choose Reason For Changing Amount
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%= turbo_frame_tag :edit_memo_modal do %>
2+
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3+
<%= form_for @store_credit, url: solidus_admin.update_memo_user_store_credit_path(@user, @store_credit), method: :put, html: { id: form_id } do |f| %>
4+
<div class="flex flex-col gap-6 pb-4">
5+
<%= render component("ui/forms/field").text_field(f, :memo) %>
6+
</div>
7+
<% modal.with_actions do %>
8+
<form method="dialog">
9+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
10+
</form>
11+
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
12+
<% end %>
13+
<% end %>
14+
<% end %>
15+
<% end %>
16+
<%= render component("users/store_credits/show").new(user: @user, store_credit: @store_credit, events: @store_credit_events) %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::Users::StoreCredits::EditMemo::Component < SolidusAdmin::BaseComponent
4+
def initialize(user:, store_credit:, events:)
5+
@user = user
6+
@store_credit = store_credit
7+
@store_credit_events = events
8+
end
9+
10+
def form_id
11+
dom_id(@store_credit, "#{stimulus_id}_edit_memo_form")
12+
end
13+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
en:
2+
title: Edit Store Credit Memo
3+
cancel: Cancel
4+
submit: Update Store Credit

admin/app/components/solidus_admin/users/store_credits/show/component.html.erb

+23-20
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,29 @@
2424
) %>
2525
<% end %>
2626

27-
<% if @store_credit.editable? || @store_credit.invalidateable? %>
28-
<% panel.with_section do %>
29-
<div class="w-[100%] text-right">
30-
<% if @store_credit.invalidateable? %>
31-
<%= render component("ui/button").new(
32-
scheme: :danger,
33-
tag: :a,
34-
text: t(".invalidate"),
35-
href: spree.edit_validity_admin_user_store_credit_path(@user, @store_credit)
36-
)%>
37-
<% end %>
38-
<% if @store_credit.editable? %>
39-
<%= render component("ui/button").new(
40-
"data-action": "click->#{stimulus_id}#actionButtonClicked",
41-
"data-#{stimulus_id}-url-param": solidus_admin.edit_amount_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_amount_modal),
42-
text: t(".edit"),
43-
)%>
44-
<% end %>
45-
</div>
46-
<% end %>
27+
<% panel.with_section do %>
28+
<div class="w-[100%] text-right">
29+
<% if @store_credit.invalidateable? %>
30+
<%= render component("ui/button").new(
31+
scheme: :danger,
32+
tag: :a,
33+
text: t(".invalidate"),
34+
href: spree.edit_validity_admin_user_store_credit_path(@user, @store_credit)
35+
)%>
36+
<% end %>
37+
<%= render component("ui/button").new(
38+
"data-action": "click->#{stimulus_id}#actionButtonClicked",
39+
"data-#{stimulus_id}-url-param": solidus_admin.edit_memo_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_memo_modal),
40+
text: t(".edit_memo"),
41+
)%>
42+
<% if @store_credit.editable? %>
43+
<%= render component("ui/button").new(
44+
"data-action": "click->#{stimulus_id}#actionButtonClicked",
45+
"data-#{stimulus_id}-url-param": solidus_admin.edit_amount_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_amount_modal),
46+
text: t(".edit_amount"),
47+
)%>
48+
<% end %>
49+
</div>
4750
<% end %>
4851
<% end %>
4952

admin/app/components/solidus_admin/users/store_credits/show/component.rb

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def tabs
5151
def turbo_frames
5252
%w[
5353
edit_amount_modal
54+
edit_memo_modal
5455
]
5556
end
5657

admin/app/components/solidus_admin/users/store_credits/show/component.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ en:
77
store_credit: Store Credit
88
last_active: Last Active
99
add_store_credit: Add Store Credit
10-
edit: Edit Amount
10+
edit_amount: Edit Amount
11+
edit_memo: Edit Memo
1112
invalidate: Invalidate
1213
store_credit_history: Store Credit History
1314
credited: Credited

admin/app/controllers/solidus_admin/store_credits_controller.rb

+38-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module SolidusAdmin
44
class StoreCreditsController < SolidusAdmin::BaseController
55
before_action :set_user
6-
before_action :set_store_credit, only: [:show, :edit_amount, :update_amount]
6+
before_action :set_store_credit, only: [:show, :edit_amount, :update_amount, :edit_memo, :update_memo, ]
77
before_action :set_store_credit_reasons, only: [:edit_amount, :update_amount]
88

99
def index
@@ -54,7 +54,40 @@ def update_amount
5454
end
5555
end
5656
else
57-
render_edit_page_with_errors and return
57+
render_edit_amount_with_errors and return
58+
end
59+
end
60+
61+
def edit_memo
62+
@store_credit_events = @store_credit.store_credit_events.chronological
63+
64+
respond_to do |format|
65+
format.html {
66+
render component("users/store_credits/edit_memo").new(
67+
user: @user,
68+
store_credit: @store_credit,
69+
events: @store_credit_events,
70+
)
71+
}
72+
end
73+
end
74+
75+
def update_memo
76+
if @store_credit.update(memo: permitted_store_credit_params[:memo])
77+
flash[:notice] = t('.success')
78+
else
79+
# Memo update failures are nearly impossible to trigger due to lack of validation.
80+
flash[:error] = t('.failure')
81+
end
82+
83+
respond_to do |format|
84+
format.html do
85+
redirect_to solidus_admin.user_store_credit_path(@user, @store_credit), status: :see_other
86+
end
87+
88+
format.turbo_stream do
89+
render turbo_stream: '<turbo-stream action="refresh" />'
90+
end
5891
end
5992
end
6093

@@ -79,7 +112,7 @@ def permitted_store_credit_params
79112
params.require(:store_credit).permit(permitted_params).merge(created_by: spree_current_user)
80113
end
81114

82-
def render_edit_page_with_errors
115+
def render_edit_amount_with_errors
83116
@store_credit_events = @store_credit.store_credit_events.chronological
84117

85118
respond_to do |format|
@@ -98,7 +131,7 @@ def render_edit_page_with_errors
98131
def ensure_amount
99132
if permitted_store_credit_params[:amount].blank?
100133
@store_credit.errors.add(:amount, :greater_than, count: 0, value: permitted_store_credit_params[:amount])
101-
render_edit_page_with_errors
134+
render_edit_amount_with_errors
102135
return false
103136
end
104137
true
@@ -109,7 +142,7 @@ def ensure_store_credit_reason
109142

110143
if @store_credit_reason.blank?
111144
@store_credit.errors.add(:store_credit_reason_id, "Store Credit reason must be provided")
112-
render_edit_page_with_errors
145+
render_edit_amount_with_errors
113146
return false
114147
end
115148
true

admin/config/locales/store_credits.en.yml

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ en:
88
success: "Store credit was successfully created."
99
update_amount:
1010
success: "Store credit was successfully updated."
11+
update_memo:
12+
success: "Store credit was successfully updated."
13+
failure: "Something went wrong. Store credit could not be updated."

admin/config/routes.rb

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
member do
5858
get :edit_amount
5959
put :update_amount
60+
get :edit_memo
61+
put :update_memo
6062
end
6163
end
6264
end

admin/spec/features/store_credits_spec.rb

+19-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
it "shows the appropriate error message" do
9393
click_on "Edit Amount"
9494
expect(page).to have_selector("dialog", wait: 5)
95-
expect(page).to have_content("Edit Store Credit")
95+
expect(page).to have_content("Edit Store Credit Amount")
9696

9797
within("dialog") do
9898
fill_in "Amount", with: ""
@@ -107,7 +107,7 @@
107107
it "shows the appropriate error message" do
108108
click_on "Edit Amount"
109109
expect(page).to have_selector("dialog", wait: 5)
110-
expect(page).to have_content("Edit Store Credit")
110+
expect(page).to have_content("Edit Store Credit Amount")
111111

112112
within("dialog") do
113113
fill_in "Amount", with: "100"
@@ -122,9 +122,8 @@
122122
it "allows editing of the store credit amount" do
123123
click_on "Edit Amount"
124124
expect(page).to have_selector("dialog", wait: 5)
125-
expect(page).to have_content("Edit Store Credit")
125+
expect(page).to have_content("Edit Store Credit Amount")
126126

127-
# Invalid amount
128127
within("dialog") do
129128
fill_in "Amount", with: "666"
130129
select "credit given in error", from: "store_credit[store_credit_reason_id]"
@@ -137,6 +136,22 @@
137136
end
138137
end
139138
end
139+
140+
context "when editing the store credit memo" do
141+
it "allows editing of the store credit memo" do
142+
click_on "Edit Memo"
143+
expect(page).to have_selector("dialog", wait: 5)
144+
expect(page).to have_content("Edit Store Credit Memo")
145+
146+
within("dialog") do
147+
fill_in "Memo", with: "dogtown"
148+
click_on "Update Store Credit"
149+
end
150+
151+
expect(page).to have_content("Store credit was successfully updated.")
152+
expect(page).to have_content("dogtown")
153+
end
154+
end
140155
end
141156
end
142157
end

admin/spec/requests/solidus_admin/store_credits_spec.rb

+57
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
let!(:store_credit_event) { create(:store_credit_adjustment_event, store_credit:, amount_remaining: 50) }
1010
let(:valid_params) { { amount: 100, store_credit_reason_id: create(:store_credit_reason).id } }
1111
let(:invalid_params) { { amount: nil } }
12+
let(:valid_memo_params) { { memo: "Updated memo text" } }
13+
let(:invalid_memo_params) { { memo: nil } }
1214

1315
before do
1416
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
@@ -78,6 +80,61 @@
7880
end
7981
end
8082

83+
describe "GET /edit_memo" do
84+
it "renders the edit_memo template with a 200 OK status" do
85+
get solidus_admin.edit_memo_user_store_credit_path(user, store_credit)
86+
expect(response).to have_http_status(:ok)
87+
expect(response.body).to include(store_credit.memo.to_s)
88+
end
89+
end
90+
91+
describe "PUT /update_memo" do
92+
context "with valid parameters" do
93+
it "updates the store credit memo" do
94+
expect {
95+
put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params }
96+
}.to change { store_credit.reload.memo }.to("Updated memo text")
97+
end
98+
99+
it "redirects to the store credit show page with a 303 See Other status" do
100+
put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params }
101+
expect(response).to redirect_to(solidus_admin.user_store_credit_path(user, store_credit))
102+
expect(response).to have_http_status(:see_other)
103+
end
104+
105+
it "displays a success flash message" do
106+
put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params }
107+
follow_redirect!
108+
expect(response.body).to include("Store credit was successfully updated.")
109+
end
110+
end
111+
112+
context "when the database update fails" do
113+
before do
114+
# Memo update failures are nearly impossible to trigger due to lack of validation.
115+
allow_any_instance_of(Spree::StoreCredit).to receive(:update).and_return(false)
116+
end
117+
118+
it "does not update the store credit memo" do
119+
expect {
120+
put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params }
121+
}.not_to change { store_credit.reload.memo }
122+
end
123+
124+
it "redirects to the store credit show page with a 303 See Other status" do
125+
put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params }
126+
expect(response).to redirect_to(solidus_admin.user_store_credit_path(user, store_credit))
127+
expect(response).to have_http_status(:see_other)
128+
end
129+
130+
it "displays a failure flash message" do
131+
put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params }
132+
follow_redirect!
133+
expect(response.body).to include("Something went wrong. Store credit could not be updated.")
134+
end
135+
end
136+
end
137+
81138
describe "private methods" do
82139
describe "#ensure_amount" do
83140
it "adds an error when the amount is blank" do

0 commit comments

Comments
 (0)