Skip to content

Commit 55891cc

Browse files
committed
Use modal UI for legacy_promotions index/add/edit
Introduces changes to legacy promotions UI based on solidusio#6046
1 parent ec1c421 commit 55891cc

File tree

16 files changed

+300
-46
lines changed

16 files changed

+300
-46
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Lint/AmbiguousBlockAssociation:
134134
Exclude:
135135
- "*/spec/**/*"
136136
- "spec/**/*" # For the benefit of apps that inherit from this config
137+
- "**/shared_examples/**/*"
137138

138139
# We use eval to add common_spree_dependencies into the Gemfiles of each of our gems
139140
Security/Eval:
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+
RSpec.shared_examples_for 'promotion categories features' do
4+
before { sign_in create(:admin_user, email: "[email protected]") }
5+
6+
it "lists promotion categories" do
7+
create(factory_name, name: "test1", code: "code1")
8+
create(factory_name, name: "test2", code: "code2")
9+
10+
visit index_path
11+
expect(page).to have_content("test1")
12+
expect(page).to have_content("test2")
13+
14+
expect(page).to be_axe_clean
15+
end
16+
17+
it 'allows to create new promo category' do
18+
visit index_path
19+
20+
click_on "Add new"
21+
expect(turbo_frame_modal).to have_content("New Promotion Category")
22+
23+
fill_in "Code", with: "ste.1"
24+
click_on "Add Promotion Category"
25+
26+
expect(turbo_frame_modal).to have_content("can't be blank")
27+
28+
fill_in "Name", with: "Soon to expire"
29+
click_on "Add Promotion Category"
30+
31+
expect(page).to have_content("Promotion Category was successfully created.")
32+
expect(page).to have_content("Soon to expire")
33+
expect(page).to have_content("ste.1")
34+
expect(model_class.count).to eq(1)
35+
end
36+
37+
it 'allows to update promo category' do
38+
create(factory_name, name: "Soon to expire", code: "ste.1")
39+
40+
visit index_path
41+
42+
click_on "Soon to expire"
43+
expect(turbo_frame_modal).to have_content("Edit Promotion Category")
44+
45+
fill_in "Name", with: "Expired"
46+
fill_in "Code", with: "exp.2"
47+
click_on "Update Promotion Category"
48+
49+
expect(page).to have_content("Promotion Category was successfully updated.")
50+
expect(page).to have_content("Expired")
51+
expect(page).to have_content("exp.2")
52+
end
53+
54+
it 'allows to delete promo category' do
55+
create(factory_name, name: "Soon to expire", code: "ste.1")
56+
create(factory_name, name: "Expired", code: "exp.2")
57+
58+
visit index_path
59+
60+
select_row("Expired")
61+
click_on "Delete"
62+
expect(page).to have_content("Promotion Categories were successfully removed.")
63+
expect(page).not_to have_content("Expired")
64+
expect(model_class.count).to eq(1)
65+
end
66+
end
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_examples_for 'promotion categories requests' do
4+
let(:admin_user) { create(:admin_user) }
5+
6+
before do
7+
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
8+
end
9+
10+
describe "GET /index" do
11+
it "renders the index template with a 200 OK status" do
12+
get url_helpers.promotion_categories_path
13+
expect(response).to have_http_status(:ok)
14+
end
15+
end
16+
17+
describe "GET /new" do
18+
it "renders the new template with a 200 OK status" do
19+
get url_helpers.new_promotion_category_path
20+
expect(response).to have_http_status(:ok)
21+
end
22+
end
23+
24+
describe "POST /create" do
25+
context "with valid parameters" do
26+
let(:valid_attributes) { { name: "Expired", code: "exp.1" } }
27+
let(:run_request) { post url_helpers.promotion_categories_path, params: { promotion_category: valid_attributes } }
28+
29+
it "creates a new promotion category" do
30+
expect { run_request }.to change(model_class, :count).by(1)
31+
end
32+
33+
it "redirects to the index page with a 303 See Other status" do
34+
run_request
35+
expect(response).to redirect_to(url_helpers.promotion_categories_path)
36+
expect(response).to have_http_status(:see_other)
37+
end
38+
39+
it "displays a success flash message" do
40+
run_request
41+
follow_redirect!
42+
expect(response.body).to include("Promotion Category was successfully created.")
43+
end
44+
end
45+
46+
context "with invalid parameters" do
47+
let(:invalid_attributes) { { name: "", code: "" } }
48+
let(:run_request) { post url_helpers.promotion_categories_path, params: { promotion_category: invalid_attributes } }
49+
50+
it "does not create a new promotion category" do
51+
expect { run_request }.not_to change(model_class, :count)
52+
end
53+
54+
it "renders the new template with unprocessable_entity status" do
55+
run_request
56+
expect(response).to have_http_status(:unprocessable_entity)
57+
end
58+
end
59+
end
60+
61+
describe "GET /edit" do
62+
it "renders the edit template with a 200 OK status" do
63+
get url_helpers.edit_promotion_category_path(promotion_category)
64+
expect(response).to have_http_status(:ok)
65+
end
66+
end
67+
68+
describe "PATCH /update" do
69+
context "with valid parameters" do
70+
let(:valid_attributes) { { name: "Updated", code: "upd.1" } }
71+
let(:run_request) { patch url_helpers.promotion_category_path(promotion_category), params: { promotion_category: valid_attributes } }
72+
73+
it "updates the promotion category" do
74+
run_request
75+
promotion_category.reload
76+
expect(promotion_category.name).to eq("Updated")
77+
expect(promotion_category.code).to eq("upd.1")
78+
end
79+
80+
it "redirects to the index page with a 303 See Other status" do
81+
run_request
82+
expect(response).to redirect_to(url_helpers.promotion_categories_path)
83+
expect(response).to have_http_status(:see_other)
84+
end
85+
86+
it "displays a success flash message" do
87+
run_request
88+
follow_redirect!
89+
expect(response.body).to include("Promotion Category was successfully updated.")
90+
end
91+
end
92+
93+
context "with invalid parameters" do
94+
let(:invalid_attributes) { { name: "", code: "" } }
95+
let(:run_request) { patch url_helpers.promotion_category_path(promotion_category), params: { promotion_category: invalid_attributes } }
96+
97+
it "does not update the promotion category" do
98+
expect { run_request }.not_to change { promotion_category.reload.name }
99+
end
100+
101+
it "renders the edit template with unprocessable_entity status" do
102+
run_request
103+
expect(response).to have_http_status(:unprocessable_entity)
104+
end
105+
end
106+
end
107+
108+
describe "DELETE /destroy" do
109+
before { promotion_category }
110+
111+
let(:run_request) { delete url_helpers.promotion_category_path(promotion_category) }
112+
113+
it "deletes the promotion category and redirects to the index page with a 303 See Other status" do
114+
expect { run_request }.to change(model_class, :count).by(-1)
115+
116+
expect(response).to redirect_to(url_helpers.promotion_categories_path)
117+
expect(response).to have_http_status(:see_other)
118+
end
119+
120+
it "displays a success flash message after deletion" do
121+
run_request
122+
follow_redirect!
123+
expect(response.body).to include("Promotion Categories were successfully removed.")
124+
end
125+
end
126+
end

core/lib/spree/testing_support/capybara_ext.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ def find_label_by_text(text)
110110
# find the original.
111111
find('label:not(.select2-offscreen)', text: /#{Regexp.escape(text)}/i, match: :one)
112112
end
113+
114+
def dialog(parent: 'body', **options)
115+
within(parent) do
116+
find('dialog', visible: :all, **options)
117+
end
118+
end
119+
120+
def turbo_frame_modal
121+
dialog(parent: find('turbo-frame', visible: :all))
122+
end
113123
end
114124
end
115125
end

legacy_promotions/config/locales/promotion_categories.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ en:
22
solidus_admin:
33
promotion_categories:
44
title: "Promotion Categories"
5+
create:
6+
success: "Promotion Category was successfully created."
7+
update:
8+
success: "Promotion Category was successfully updated."
59
destroy:
610
success: "Promotion Categories were successfully removed."

legacy_promotions/config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
extend SolidusAdmin::AdminResources
2424

2525
admin_resources :promotions, only: [:index, :destroy]
26-
admin_resources :promotion_categories, only: [:index, :destroy]
26+
admin_resources :promotion_categories, except: [:show]
2727
end
2828
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
2+
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3+
<%= form_for @promotion_category, url: form_url, 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, :name, class: "required") %>
6+
<%= render component("ui/forms/field").text_field(f, :code, class: "required") %>
7+
</div>
8+
<% modal.with_actions do %>
9+
<form method="dialog">
10+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
11+
</form>
12+
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
13+
<% end %>
14+
<% end %>
15+
<% end %>
16+
<% end %>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::PromotionCategories::Edit::Component < SolidusAdmin::Resources::Edit::Component
4+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
en:
2+
title: "Edit Promotion Category"
3+
cancel: "Cancel"
4+
submit: "Update Promotion Category"

legacy_promotions/lib/components/admin/solidus_admin/promotion_categories/index/component.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ def model_class
55
Spree::PromotionCategory
66
end
77

8-
def row_url(promotion_category)
9-
spree.edit_admin_promotion_category_path(promotion_category)
8+
def title
9+
t('solidus_admin.promotion_categories.title')
10+
end
11+
12+
def edit_path(record)
13+
solidus_admin.edit_promotion_category_path(record, **search_filter_params)
14+
end
15+
16+
def turbo_frames
17+
%w[resource_modal]
1018
end
1119

1220
def page_actions
1321
render component("ui/button").new(
1422
tag: :a,
1523
text: t('.add'),
16-
href: spree.new_admin_promotion_category_path,
24+
href: solidus_admin.new_promotion_category_path(**search_filter_params),
25+
data: { turbo_frame: :resource_modal },
1726
icon: "add-line",
1827
)
1928
end
@@ -22,7 +31,7 @@ def batch_actions
2231
[
2332
{
2433
label: t('.batch_actions.delete'),
25-
action: solidus_admin.promotion_categories_path,
34+
action: solidus_admin.promotion_categories_path(**search_filter_params),
2635
method: :delete,
2736
icon: 'delete-bin-7-line',
2837
},
@@ -39,17 +48,21 @@ def columns
3948
def name_column
4049
{
4150
header: :name,
42-
data: ->(promotion_category) do
43-
content_tag :div, promotion_category.name
51+
data: ->(record) do
52+
link_to record.name, edit_path(record),
53+
data: { turbo_frame: :resource_modal },
54+
class: 'body-link'
4455
end
4556
}
4657
end
4758

4859
def code_column
4960
{
5061
header: :code,
51-
data: ->(promotion_category) do
52-
content_tag :div, promotion_category.code
62+
data: ->(record) do
63+
link_to record.code, edit_path(record),
64+
data: { turbo_frame: :resource_modal },
65+
class: 'body-link'
5366
end
5467
}
5568
end

0 commit comments

Comments
 (0)